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.
我尝试在我的一个函数中使用CC_SHA256_DIGEST_LENGTH,它会抛出一个错误,因为它找不到那个符号。我已经尝试了所有方法,在桥头中导入CommonCrypto,并尝试这个。map解决方案。没有什么工作。
How can I use CC_SHA256_DIGEST_LENGTH in Swift? All the solutions seems to have stopped working. Thank you!
如何在Swift中使用CC_SHA256_DIGEST_LENGTH ?所有的解决方案似乎都失效了。谢谢你!
1 个解决方案
#1
22
Add the following line to your bridging header:#import <CommonCrypto/CommonHMAC.h>
向桥接头添加以下行:#import
Swift 2.x example:
斯威夫特2。x的例子:
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:
斯威夫特3.0的例子:
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:
输出:
clearData:
636c6561724461746130313233343536
hash:aabc766b6b357564e41f4f912d494bccbfa16924b574abbdba9e3e9da0c8920a
clearData:636 c6561724461746130313233343536散列:aabc766b6b357564e41f4f912d494bccbfa16924b574abbdba9e3e9da0c8920a
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.
我在目标构建阶段没有添加任何框架。你确定桥头设置正确吗?我添加了一个.m文件,让系统自动添加连接头,并更新任何目标设置。
General hash method moved from the sunsetted documentation section:
通用哈希方法从分解文档部分移动:
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.
注意:MD2、MD4、MD5和SHA1不应该在新工作中使用,它们对于消息摘要的使用不再安全。
#1
22
Add the following line to your bridging header:#import <CommonCrypto/CommonHMAC.h>
向桥接头添加以下行:#import
Swift 2.x example:
斯威夫特2。x的例子:
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:
斯威夫特3.0的例子:
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:
输出:
clearData:
636c6561724461746130313233343536
hash:aabc766b6b357564e41f4f912d494bccbfa16924b574abbdba9e3e9da0c8920a
clearData:636 c6561724461746130313233343536散列:aabc766b6b357564e41f4f912d494bccbfa16924b574abbdba9e3e9da0c8920a
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.
我在目标构建阶段没有添加任何框架。你确定桥头设置正确吗?我添加了一个.m文件,让系统自动添加连接头,并更新任何目标设置。
General hash method moved from the sunsetted documentation section:
通用哈希方法从分解文档部分移动:
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.
注意:MD2、MD4、MD5和SHA1不应该在新工作中使用,它们对于消息摘要的使用不再安全。