我已经搜索了一些资料,但是只找到了使用Objective-C创建SHA256哈希的示例。是否有一种仅使用Swift4实现的方法呢?
func ccSha256(data: Data) -> Data {
var digest = Data(count: Int(CC_SHA256_DIGEST_LENGTH))
_ = digest.withUnsafeMutableBytes { (digestBytes) in
data.withUnsafeBytes { (stringBytes) in
CC_SHA256(stringBytes, CC_LONG(data.count), digestBytes)
}
}
return digest
}
let str = "givesomestringtoencode"
let data = ccSha256(data: str.data(using: .utf8)!)
print("sha256 String: \(data.map { String(format: "%02hhx", $0) }.joined())")
#import <CommonCrypto/CommonHMAC.h>
CommonCrypto
。 - Vini App