前端之家收集整理的这篇文章主要介绍了
Swift3.0 - sha1加密算法,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
- 注意: 需要创建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
}
}