swift2 在cocapod安装CryptoSwift时发生错误

col17t5w  于 2022-11-06  发布在  Swift
关注(0)|答案(1)|浏览(237)

我成功地安装了一个pod“cryptoSwift”来使用swift 2在我的xcode项目中获得sha-2。但是在pod安装后,当我构建项目时,它在pod文件中显示了很多错误。为什么我会得到这个错误。我做错了什么?
先谢谢你。

hfwmuf9z

hfwmuf9z1#

由于没有“sha-2”这样的东西,所以到底需要哪种散列并不清楚。假设sha-2表示SHA-2系列散列函数,常见的选择是SHA-256和SHA-512。
使用普通Crypoto(Swift 2)的示例

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=

使用普通Crypoto(Swift 3)的示例

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", $0) }.joined())")

let testHashBase64 = testHash!.base64EncodedString()
print("testHash: \(testHasBase64)")

输出量:

testHash: 4acf0b39d9c4766709a3689f553ac01ab550545ffa4544dfc0b2cea82fba02a3  
testHashBase64: Ss8LOdnEdmcJo2ifVTrAGrVQVF/6RUTfwLLOqC+6AqM=

注:添加到桥接标题:


# import <CommonCrypto/CommonCrypto.h>

相关问题