Swift md5加密
2018-09-03 本文已影响173人
独孤伊人_xie
public func md5(strs:String) ->String!{
let str = strs.cString(using: String.Encoding.utf8)
let strLen = CUnsignedInt(strs.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)
}