在 cocapod 安装 CryptoSwift 中出错
Getting error in cocapod installation CryptoSwift
我成功安装了一个 pod“cryptoSwift”以使用 swift2 在我的 xcode 项目中获取 sha-2。但是在我构建项目时安装 pod 后它显示 pod 文件中有很多错误。为什么我我收到这个错误。我做错了什么?
提前致谢。
因为没有 "sha-2" 之类的东西,所以不清楚真正需要哪个散列。假设 sha-2 表示 SHA-2 系列哈希函数,常见的选择是 SHA-256 和 SHA-512。
Example using Common Crypoto (Swift2)
func sha256(string: String) -> [UInt8] {
var digest = [UInt8](count: Int(CC_SHA256_DIGEST_LENGTH), repeatedValue: 0)
if let data = string.dataUsingEncoding(NSUTF8StringEncoding) {
CC_SHA256(data.bytes, CC_LONG(data.length), &digest)
}
return digest
}
测试
let testString = "testString"
let testHash = sha256(testString)
print("testHash: \(testHash)")
let testHashNSData = NSData(bytes:testHash, length:testHash.count)
print("testHashNSData: \(testHashNSData)")
let testHashBase64 = testHashNSData.base64EncodedStringWithOptions([])
print("testHashBase64: \(testHashBase64)")
输出:
testHash: [74, 207, 11, 57, 217, 196, 118, 103, 9, 163, 104, 159, 85, 58, 192, 26, 181, 80, 84, 95, 250, 69, 68, 223, 192, 178, 206, 168, 47, 186, 2, 163]
testHashNSData: <4acf0b39 d9c47667 09a3689f 553ac01a b550545f fa4544df c0b2cea8 2fba02a3>
testHashBase64: Ss8LOdnEdmcJo2ifVTrAGrVQVF/6RUTfwLLOqC+6AqM=
Example using Common Crypoto (Swift3)
func sha256(string: String) -> Data? {
let messageData = string.data(using:String.Encoding.utf8)!
var digestData = Data(count: Int(CC_SHA256_DIGEST_LENGTH))
_ = digestData.withUnsafeMutableBytes {digestBytes in
messageData.withUnsafeBytes {messageBytes in
CC_SHA256(messageBytes, CC_LONG(messageData.count), digestBytes)
}
}
return digestData
}
测试
let testString = "testString"
let testHash = sha256(string:testString)
print("testHash: \(testHash!.map { String(format: "%02hhx", [=14=]) }.joined())")
let testHashBase64 = testHash!.base64EncodedString()
print("testHash: \(testHasBase64)")
输出:
testHash: 4acf0b39d9c4766709a3689f553ac01ab550545ffa4544dfc0b2cea82fba02a3
testHashBase64: Ss8LOdnEdmcJo2ifVTrAGrVQVF/6RUTfwLLOqC+6AqM=
注意:添加到您的桥接头:
#import <CommonCrypto/CommonCrypto.h>
我成功安装了一个 pod“cryptoSwift”以使用 swift2 在我的 xcode 项目中获取 sha-2。但是在我构建项目时安装 pod 后它显示 pod 文件中有很多错误。为什么我我收到这个错误。我做错了什么?
提前致谢。
因为没有 "sha-2" 之类的东西,所以不清楚真正需要哪个散列。假设 sha-2 表示 SHA-2 系列哈希函数,常见的选择是 SHA-256 和 SHA-512。
Example using Common Crypoto (Swift2)
func sha256(string: String) -> [UInt8] {
var digest = [UInt8](count: Int(CC_SHA256_DIGEST_LENGTH), repeatedValue: 0)
if let data = string.dataUsingEncoding(NSUTF8StringEncoding) {
CC_SHA256(data.bytes, CC_LONG(data.length), &digest)
}
return digest
}
测试
let testString = "testString"
let testHash = sha256(testString)
print("testHash: \(testHash)")
let testHashNSData = NSData(bytes:testHash, length:testHash.count)
print("testHashNSData: \(testHashNSData)")
let testHashBase64 = testHashNSData.base64EncodedStringWithOptions([])
print("testHashBase64: \(testHashBase64)")
输出:
testHash: [74, 207, 11, 57, 217, 196, 118, 103, 9, 163, 104, 159, 85, 58, 192, 26, 181, 80, 84, 95, 250, 69, 68, 223, 192, 178, 206, 168, 47, 186, 2, 163]
testHashNSData: <4acf0b39 d9c47667 09a3689f 553ac01a b550545f fa4544df c0b2cea8 2fba02a3>
testHashBase64: Ss8LOdnEdmcJo2ifVTrAGrVQVF/6RUTfwLLOqC+6AqM=
Example using Common Crypoto (Swift3)
func sha256(string: String) -> Data? {
let messageData = string.data(using:String.Encoding.utf8)!
var digestData = Data(count: Int(CC_SHA256_DIGEST_LENGTH))
_ = digestData.withUnsafeMutableBytes {digestBytes in
messageData.withUnsafeBytes {messageBytes in
CC_SHA256(messageBytes, CC_LONG(messageData.count), digestBytes)
}
}
return digestData
}
测试
let testString = "testString"
let testHash = sha256(string:testString)
print("testHash: \(testHash!.map { String(format: "%02hhx", [=14=]) }.joined())")
let testHashBase64 = testHash!.base64EncodedString()
print("testHash: \(testHasBase64)")
输出:
testHash: 4acf0b39d9c4766709a3689f553ac01ab550545ffa4544dfc0b2cea82fba02a3
testHashBase64: Ss8LOdnEdmcJo2ifVTrAGrVQVF/6RUTfwLLOqC+6AqM=
注意:添加到您的桥接头:
#import <CommonCrypto/CommonCrypto.h>