Implementing HMAC and SHA1 encryption in swift

后端 未结 8 1342
借酒劲吻你
借酒劲吻你 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:23

    Using raw bytes for key and message and not encoding to utf8:

        static func getHmac_X_Sha1() -> [UInt8] {
    
            let msg:[UInt8] = message_uint8;
            let msgLen = message_uint8.count;
            let digestLen = Int(CC_SHA1_DIGEST_LENGTH)
            let digest = UnsafeMutablePointer.allocate(capacity: digestLen)
            let keyStr:[UInt8] = key_uint8
            let keyLen = key_uint8.count
    
            CCHmac(CCHmacAlgorithm(kCCHmacAlgSHA1), keyStr, keyLen, msg, msgLen, digest)
    
    
            //Build a hex string of result
            let hash_hex_string = NSMutableString()
            for i in 0.. [UInt8]? {
            let length = string.characters.count
            if length & 1 != 0 {
                return nil
            }
            var bytes = [UInt8]()
            bytes.reserveCapacity(length/2)
            var index = string.startIndex
            for _ in 0..

提交回复
热议问题