iosswift2cocoapodsshacryptoswift

Getting error in cocapod installation CryptoSwift


i installed a pod successfully "cryptoSwift' to get sha-2 in my xcode project using swift2. but after pod installation when i build project it shows me a lot of error in pod files. why i am getting this error. what's i am doing wrong?

Thanks in advance.


Solution

  • Since there is no such thing as "sha-2" exaxctly which hash is really need is not clear. Assuming sha-2 means a SHA-2 series hash function the common choices are SHA-256 and 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
    }
    

    Test

    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)")
    

    Output:

    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
    }
    

    Test

    let testString = "testString"
    let testHash = sha256(string:testString)
    print("testHash: \(testHash!.map { String(format: "%02hhx", $0) }.joined())")
    
    let testHashBase64 = testHash!.base64EncodedString()
    print("testHash: \(testHasBase64)")
    

    Output:

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

    Note: Add to your Bridging Header:

    #import <CommonCrypto/CommonCrypto.h>