Implementing HMAC and SHA1 encryption in swift

后端 未结 8 1250
借酒劲吻你
借酒劲吻你 2020-11-29 01:42

I am relatively new to Swift and i\'m stuck encrypting using HMAC and SHA1. I Found the following answer https://stackoverflow.com/a/24411522/4188344 but i can\'t work out h

相关标签:
8条回答
  • 2020-11-29 02:26

    Here is @David Wood's solution updated for Swift 3:

    enum HMACAlgorithm {
        case MD5, SHA1, SHA224, SHA256, SHA384, SHA512
    
        func toCCHmacAlgorithm() -> CCHmacAlgorithm {
            var result: Int = 0
            switch self {
            case .MD5:
                result = kCCHmacAlgMD5
            case .SHA1:
                result = kCCHmacAlgSHA1
            case .SHA224:
                result = kCCHmacAlgSHA224
            case .SHA256:
                result = kCCHmacAlgSHA256
            case .SHA384:
                result = kCCHmacAlgSHA384
            case .SHA512:
                result = kCCHmacAlgSHA512
            }
            return CCHmacAlgorithm(result)
        }
    
        func digestLength() -> Int {
            var result: CInt = 0
            switch self {
            case .MD5:
                result = CC_MD5_DIGEST_LENGTH
            case .SHA1:
                result = CC_SHA1_DIGEST_LENGTH
            case .SHA224:
                result = CC_SHA224_DIGEST_LENGTH
            case .SHA256:
                result = CC_SHA256_DIGEST_LENGTH
            case .SHA384:
                result = CC_SHA384_DIGEST_LENGTH
            case .SHA512:
                result = CC_SHA512_DIGEST_LENGTH
            }
            return Int(result)
        }
    }
    
    extension String {
        func hmac(algorithm: HMACAlgorithm, key: String) -> String {
            let cKey = key.cString(using: String.Encoding.utf8)
            let cData = self.cString(using: String.Encoding.utf8)
            var result = [CUnsignedChar](repeating: 0, count: Int(algorithm.digestLength()))
            CCHmac(algorithm.toCCHmacAlgorithm(), cKey!, Int(strlen(cKey!)), cData!, Int(strlen(cData!)), &result)
            let hmacData:NSData = NSData(bytes: result, length: (Int(algorithm.digestLength())))
            let hmacBase64 = hmacData.base64EncodedString(options: NSData.Base64EncodingOptions.lineLength76Characters)
            return String(hmacBase64)
        }
    }
    
    // usage:
    let hmacResult: String = "myStringToHMAC".hmac(algorithm: HMACAlgorithm.SHA1, key: "foo")
    
    0 讨论(0)
  • 2020-11-29 02:27

    If you want the same result in hexadecimal format, you can use the following extension:

    extension String {
        func hmac(algorithm: HMACAlgorithm, key: String) -> String {
            let cKey = key.cStringUsingEncoding(NSUTF8StringEncoding)
            let cData = self.cStringUsingEncoding(NSUTF8StringEncoding)
            var result = [CUnsignedChar](count: Int(algorithm.digestLength()), repeatedValue: 0)
            let length : Int = Int(strlen(cKey!))
            let data : Int = Int(strlen(cData!))
            CCHmac(algorithm.toCCHmacAlgorithm(), cKey!,length , cData!, data, &result)
    
            let hmacData:NSData = NSData(bytes: result, length: (Int(algorithm.digestLength())))
    
            var bytes = [UInt8](count: hmacData.length, repeatedValue: 0)
            hmacData.getBytes(&bytes, length: hmacData.length)
    
            var hexString = ""
            for byte in bytes {
                hexString += String(format:"%02hhx", UInt8(byte))
            }
            return hexString
        }
    }
    
    0 讨论(0)
提交回复
热议问题