【发布时间】:2018-03-05 20:29:41
【问题描述】:
所以我尝试从文件中获取哈希值。使用CryptoSwift 库。
truth 是我从 VLC 网站获得的带有哈希值的变量,所以这应该是真的。但是,我生成的散列与我知道的真实散列不同。
我错过了哪一步?
代码:
let filePath = "/Users/pjc/Desktop/vlc-3.0.0.dmg"
let fileURL = URL(fileURLWithPath: filePath)
let truth = "e6f7179cb06809b6101803da3ac4191edb72ecf82f31b8ae7dbf010e1a78ba26"
do {
let fileData = try Data.init(contentsOf: fileURL)
print(fileData)
let fileBytes = fileData.bytes
let hash = fileBytes.sha256()
print(hash.debugDescription)
} catch {
//handle error
print(error)
}
print(hash)
print(truth)
日志:
fileData: 46818658 bytes
hash.debugDescription: [230, 247, 23, 156, 176, 104, 9, 182, 16, 24, 3, 218, 58, 196, 25, 30, 219, 114, 236, 248, 47, 49, 184, 174, 125, 191, 1, 14, 26, 120, 186, 38]
hash: 105553117580736
truth: e6f7179cb06809b6101803da3ac4191edb72ecf82f31b8ae7dbf010e1a78ba26
【问题讨论】:
-
提示:230 = 0xE6, 247 = 0xF7, 23 = 0x17, ...
-
我不熟悉 CryptoSwift 库,但快速浏览一下 README,
.toHexString()可能已经是你要找的了。 -
@MartinR
.toHexString()确实可以解决问题。如果你把它作为一个答案,我会接受它。非常感谢! -
我已经回答了你的直接问题,但你可能会从比我更了解加密的人那里看到这个答案:stackoverflow.com/a/34474304/1187415,并改用 Apple 的 CommonCrypto。
-
最好避免使用 CryptoSwift,除此之外,它比基于 Common Crypto 的实现慢 500 到 1000 倍。 Apple 的 Common Crypto 已通过 FIPS 认证,因此经过了严格的审查,使用 CryptoSwift 在正确性和安全性(例如定时和功率攻击)方面是有机会的。
标签: swift hash cryptoswift