【问题标题】:Wy md5 hash gives me different result using swiftWy md5 hash 使用 swift 给我不同的结果
【发布时间】: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(遵循摘要认证文档)。所以我的摘要总是以错误的方式计算。 谢谢 阿诺德

【问题讨论】:

标签: swift md5 digest-authentication


【解决方案1】:

我的错误,它给了我正确的结果。我在计算哈希时出错。字符串扩展没问题。

【讨论】:

  • 最好删除这个问题,因为这个答案对未来的搜索者没有用处。
猜你喜欢
  • 1970-01-01
  • 2014-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-28
  • 1970-01-01
相关资源
最近更新 更多