1

I want to implement digest authentication with Swift. Unfortunately after hours of testing I saw that using this method of creating the md5 hash gives me the wrong result.

extension String {
    var md5: String {
        let data = Data(self.utf8)
        let hash = data.withUnsafeBytes { (bytes: UnsafeRawBufferPointer) -> [UInt8] in
            var hash = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))
            CC_MD5(bytes.baseAddress, CC_LONG(data.count), &hash)
            return hash
        }
        return hash.map { String(format: "%02x", $0) }.joined()
    }
}

using this string

let test = "test:testrealm@host.com:pwd123".md5 

test has the value: 4ec2086d6f09366e4683dbdc5809444a but it should have 939e7578ed9e3c518a452acee763bce9 (following a digest auth. documentation). So me digest was always calculated in the wrong manner. Thanks Arnold

Arnold Schmid
  • 163
  • 12
  • Did you tried this ? https://stackoverflow.com/questions/32163848/how-can-i-convert-a-string-to-an-md5-hash-in-ios-using-swift – dgp Jan 18 '20 at 15:37
  • According to https://blog.wildix.com/basic-digest-authentication/ is "939e7578ed9e3c518a452acee763bce9" the MD5 digest of "Mufasa:testrealm@host.com:Circle Of Life" – Martin R Jan 18 '20 at 15:46

1 Answers1

1

My error, it gives me the right result. I had an error computing the hash. The string extension is ok.

Arnold Schmid
  • 163
  • 12