Wy md5 hash使用swift给了我不同的结果

问题描述 投票:0回答:1

我想用Swift实现摘要身份验证。不幸的是,经过数小时的测试,我发现使用这种创建md5哈希的方法给了我错误的结果。

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()
    }
}

使用此字符串

let test = "test:[email protected]:pwd123".md5 

测试的值:4ec2086d6f09366e4683dbdc5809444a,但它应该具有939e7578ed9e3c518a452acee763bce9(遵循摘要身份验证文档)。所以我的摘要总是以错误的方式计算。谢谢阿诺德

swift md5 digest-authentication
1个回答
0
投票

我的错误,它给了我正确的结果。我在计算哈希值时出错。字符串扩展名可以。

© www.soinside.com 2019 - 2024. All rights reserved.