问题描述
我试图在我的 Swift 函数之一中使用 CC_SHA256_DIGEST_LENGTH 并且它抛出一个错误,因为它找不到那个符号.我已经尝试了所有方法,在桥头文件中导入 CommonCrypto 并尝试使用 .map 解决方案......没有任何效果.
如何在 Swift 中使用 CC_SHA256_DIGEST_LENGTH?所有的解决方案似乎都停止工作了.谢谢!
将以下行添加到您的桥接头:#import
Swift 2.x 示例:
func doSha256(#dataIn:NSData) ->NSData {var shaOut: NSMutableData!= NSMutableData(长度:Int(CC_SHA256_DIGEST_LENGTH));CC_SHA256(dataIn.bytes, CC_LONG(dataIn.length), UnsafeMutablePointer(shaOut.mutableBytes));返回 shaOut;}
Swift 3.0 示例:
func hashSHA256(data:Data) ->数据?{var hashData = Data(count: Int(CC_SHA256_DIGEST_LENGTH))_ = hashData.withUnsafeMutableBytes {digestBytes indata.withUnsafeBytes {messageBytes inCC_SHA256(messageBytes, CC_LONG(data.count),digestBytes)}}返回哈希数据}让 clearData = "clearData0123456".data(using:String.Encoding.utf8)!print("clearData: \(clearData.map { String(format: "%02hhx", $0) }.joined())")让 hash = hashSHA256(data:clearData)print("hash: \(hash!.map { String(format: "%02hhx", $0) }.joined())")
输出:
clearData:636c65617244617461303132333343536
哈希:aabc766b6b357564e41f4f912d494bccbfa16924b574abbdba9e3e9da0c8920a
我没有在目标构建阶段中添加任何框架.
您确定桥接头设置正确吗?我通过添加 .m 文件添加了我的文件,并让系统自动添加桥接头并更新任何目标设置.
从已弃用的文档部分移出的通用哈希方法:
这个函数需要一个散列名称和要散列的数据并返回一个数据:
名称:哈希函数的名称作为字符串数据:要散列的数据返回: 散列结果作为数据func hash(name:String, data:Data) ->数据?{让算法 = ["MD2": (CC_MD2, CC_MD2_DIGEST_LENGTH),MD4":(CC_MD4,CC_MD4_DIGEST_LENGTH),MD5":(CC_MD5,CC_MD5_DIGEST_LENGTH),SHA1":(CC_SHA1,CC_SHA1_DIGEST_LENGTH),SHA224":(CC_SHA224,CC_SHA224_DIGEST_LENGTH),SHA256":(CC_SHA256,CC_SHA256_DIGEST_LENGTH),SHA384":(CC_SHA384,CC_SHA384_DIGEST_LENGTH),SHA512":(CC_SHA512,CC_SHA512_DIGEST_LENGTH)]守卫让(hashAlgorithm,长度)= algos[name] else { return nil }var hashData = Data(count: Int(length))_ = hashData.withUnsafeMutableBytes {digestBytes indata.withUnsafeBytes {messageBytes inhashAlgorithm(messageBytes, CC_LONG(data.count),digestBytes)}}返回哈希数据}
注意:新作品中不应使用 MD2、MD4、MD5 和 SHA1,它们对于消息摘要的使用不再安全.
I'm trying to use CC_SHA256_DIGEST_LENGTH in one of my functions in Swift and it throws an error because it cannot find that symbol. I've tried everything, importing CommonCrypto in the bridge-header and trying that .map solution.. Nothing works.
How can I use CC_SHA256_DIGEST_LENGTH in Swift? All the solutions seems to have stopped working.Thank you!
Add the following line to your bridging header:#import <CommonCrypto/CommonHMAC.h>
Swift 2.x example:
func doSha256(#dataIn:NSData) -> NSData {
var shaOut: NSMutableData! = NSMutableData(length: Int(CC_SHA256_DIGEST_LENGTH));
CC_SHA256(dataIn.bytes, CC_LONG(dataIn.length), UnsafeMutablePointer<UInt8>(shaOut.mutableBytes));
return shaOut;
}
Swift 3.0 example:
func hashSHA256(data:Data) -> Data? {
var hashData = Data(count: Int(CC_SHA256_DIGEST_LENGTH))
_ = hashData.withUnsafeMutableBytes {digestBytes in
data.withUnsafeBytes {messageBytes in
CC_SHA256(messageBytes, CC_LONG(data.count), digestBytes)
}
}
return hashData
}
let clearData = "clearData0123456".data(using:String.Encoding.utf8)!
print("clearData: \(clearData.map { String(format: "%02hhx", $0) }.joined())")
let hash = hashSHA256(data:clearData)
print("hash: \(hash!.map { String(format: "%02hhx", $0) }.joined())")
Output:
I don't have any frameworks added in the target Build Phases.
Be are you sure that the bridging-header is set up correctly? I added mine by adding a .m file and let the system automatically add the bridging-header and update any target settings.
This function takes a hash name and Data to be hashed and returns a Data:
name: A name of a hash function as a String data: The Data to be hashed returns: the hashed result as Data
func hash(name:String, data:Data) -> Data? {
let algos = ["MD2": (CC_MD2, CC_MD2_DIGEST_LENGTH),
"MD4": (CC_MD4, CC_MD4_DIGEST_LENGTH),
"MD5": (CC_MD5, CC_MD5_DIGEST_LENGTH),
"SHA1": (CC_SHA1, CC_SHA1_DIGEST_LENGTH),
"SHA224": (CC_SHA224, CC_SHA224_DIGEST_LENGTH),
"SHA256": (CC_SHA256, CC_SHA256_DIGEST_LENGTH),
"SHA384": (CC_SHA384, CC_SHA384_DIGEST_LENGTH),
"SHA512": (CC_SHA512, CC_SHA512_DIGEST_LENGTH)]
guard let (hashAlgorithm, length) = algos[name] else { return nil }
var hashData = Data(count: Int(length))
_ = hashData.withUnsafeMutableBytes {digestBytes in
data.withUnsafeBytes {messageBytes in
hashAlgorithm(messageBytes, CC_LONG(data.count), digestBytes)
}
}
return hashData
}
Note: MD2, MD4, MD5 and SHA1 should not be used in new work, they are no longer secure for message digest usage.
这篇关于Swift 中的 SHA256 - 导入框架问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!