【发布时间】:2020-01-18 15:27:59
【问题描述】:
我想用 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:testrealm@host.com:pwd123".md5
test 的值是:4ec2086d6f09366e4683dbdc5809444a 但它应该有 939e7578ed9e3c518a452acee763bce9(遵循摘要认证文档)。所以我的摘要总是以错误的方式计算。 谢谢 阿诺德
【问题讨论】:
-
根据blog.wildix.com/basic-digest-authentication 是“939e7578ed9e3c518a452acee763bce9”“Mufasa:testrealm@host.com:Circle Of Life”的 MD5 摘要
标签: swift md5 digest-authentication