- 注意: 需要创建OC桥接文件, Bridging-Header.h
#import <CommonCrypto/CommonHMAC.h>
extension String {
//sha1加密算法
func sha1() -> String{
let data : Data = self.data(using: String.Encoding.utf8, allowLossyConversion: true)!
var digest = [UInt8](repeating:0,count:Int(CC_SHA1_DIGEST_LENGTH))
let dataBytes = data.withUnsafeBytes { (bytes: UnsafePointer<UInt8>) -> UnsafePointer<UInt8> in
return bytes
}
let dataLength = CC_LONG(data.count)
CC_SHA1(dataBytes, dataLength, &digest)
let output = NSMutableString(capacity: Int(CC_SHA1_DIGEST_LENGTH))
for byte in digest{
output.appendFormat("%02x", byte)
}
return output as String
}
}