extension String {
//MARK: MD5加密
public func addMD5() -> String {
let str = self.cString(using: .utf8)
let strLen = CUnsignedInt(self.lengthOfBytes(using: .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])
}
free(result)//解决MD5加密造成的内存泄漏问题
return String(format: hash as String)
}
}
使用:let res = resStr.addMD5()