[英]SHA256 on Terminal vs. Node.js giving two different hashes
我想得到一个 SHA256 Hash,它给我的结果与我在终端上得到的结果相同
echo -n "hello world" | shasum -0 -a 256
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
我尝试了两种不同的脚本
const {Sha256} = require('@aws-crypto/sha256-js');
(async () => {
const hash = new Sha256();
hash.update('hello world');
const result = await hash.digest();
let hex = Buffer.from(result).toString('hex');
console.log(hex);
})()
和
var hash = require('hash.js')
console.log(hash.sha256().update('hello world').digest('hex'))
他们都给了我 hash b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
我如何从我的终端中的shasum
获得相同的 hash?
如果您询问 shasum 的手册页,您会看到;
-0, --01 read in BITS mode ASCII '0' interpreted as 0-bit, ASCII '1' interpreted as 1-bit, all other characters ignored
那只从文件中读取0
s 和1
s(在你的例子中是 pipe )。 现在删除参数-0
或更好地定义默认值
-t, --text 以文本模式读取(默认)
$ echo -n "你好世界" | shasum -t -a 256 | 剪切-c -64 b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
现在我们有相同的 output。
注意 1 : cut
用于删除最后的空格和破折号。
非 2: NIST 在密码算法验证程序中有测试向量,空字符串(长度为 0)的 SHA-256 值在文件SHA256ShortMsg.rsp
中给出;
Len = 0 Msg = 00 MD = e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
注 3 :对于 NIST 的 hash function,不应依赖第三方结果。 他们需要查看 NIST 向量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.