Detailed example of swift MD5 encryption source code

Time:2021-11-26

Detailed example of swift MD5 encryption source code

Because MD5 encryption is irreversible, there is generally only MD5 encryption algorithm, but no MD5 decryption algorithm.

Create a sting + md5.swift string classification file (at the same time, create a bridge. H bridge file here and introduce this header file

#Import < commoncrypto / commondigest. H >, files required for MD5 encryption method)

1. Bridge. H bridging documents are as follows:


#ifndef bridge_h  
#define bridge_h  
  
#import <CommonCrypto/CommonDigest.h>  
  
#endif /* bridge_h */  

2. Sting + md5.swift string classification file is as follows

MD5 encryption algorithm is as follows


import Foundation  
  
extension String {  
  var md5 : String{  
    let str = self.cString(using: String.Encoding.utf8)  
    let strLen = CC_LONG(self.lengthOfBytes(using: String.Encoding.utf8))  
    let digestLen = Int(CC_MD5_DIGEST_LENGTH)  
    let result = UnsafeMutablePointer<CUnsignedChar>.allocate(capacity: digestLen)  
      
    CC_MD5(str!, strLen, result)  
      
    let hash = NSMutableString()  
    for i in 0 ..< digestLen {  
      hash.appendFormat("%02x", result[i])  
    }  
    result.deinitialize()  
      
    return String(format: hash as String)  
  }  
}  

If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!

Recommended Today

Next. Js+react+node system actual combat, get SSR server rendering Yun disk

problem Next ZY:https://www.sisuoit.com/2811….As long as you use cache, it may involve dual storage of cache and databaseDouble writing, you only need doubleIf you write, there must be a data consistency problem. How do you solve the consistency problem? analysis Let’s make an explanation first, from the point of viewIn theoryThere are two kinds of processing […]