记录一下Swift下MD5的加密的实现,方便以后调用。RSA、AES、MD5整合github地址。
一、导入头文件
import CommonCrypto
二、String的Extension实现
internal extension String {
//MD5加密
func md5Encrypt() -> String {
let utf8_str = self.cString(using: .utf8)
let str_len = CC_LONG(self.lengthOfBytes(using: .utf8))
let digest_len = Int(CC_MD5_DIGEST_LENGTH)
let result = UnsafeMutablePointer<CUnsignedChar>.allocate(capacity: digest_len)
CC_MD5(utf8_str, str_len, result)
let str = NSMutableString()
for i in 0..<digest_len {
str.appendFormat("%02x", result[i])
}
result.deallocate()
return str as String
}
}
网友评论