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

Vue、Three. JS implementation panorama

1、 First, we need to create a Vue project This paper mainly records the process of building panorama in detail, so building Vue project is not described too much. 2、 Install three js npm install three –save npm install three-trackballcontrols –save npm install three-orbit-controls –save npm i three-obj-mtl-loader –save npm i three-fbx-loader –save npm i […]