终端上的 SHA256 与 Node.js 提供两种不同的哈希值
我想要得到一个 SHA256 哈希值,该哈希值与我在终端上得到的结果相同
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'))
它们都给了我哈希值b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
如何从终端中的 shasum
获取相同的哈希值?
I want to get a SHA256 Hash that gives me the same result as I get on the terminal
echo -n "hello world" | shasum -0 -a 256
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
I tried two different scripts
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);
})()
And
var hash = require('hash.js')
console.log(hash.sha256().update('hello world').digest('hex'))
They both give me the hash b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
How can I get the same hash from the shasum
in my terminal?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你询问 shasum 的手册页,你会看到;
仅从文件中读取
0
和1
(在您的情况下为管道)。现在删除参数-0
或更好地定义默认值现在我们有相同的输出。
注1:
cut
用于删除末尾的空格和破折号。不是 2: NIST 在 加密算法验证程序 和空字符串(长度 0)的 SHA-256 值如文件中所示
SHA256ShortMsg.rsp
;注3:对于NIST的哈希函数,不应依赖第三方结果。他们需要查看 NIST 矢量。
If you ask the man page of shasum you will see;
That reads only
0
s and1
s from the file ( pipe in your case). Now remove the parameter-0
or better define the defaultNow we have the same output.
Note 1: that the
cut
is used to remove the space and dash in the end.Not 2 : NIST has test vector in Cryptographic Algorithm Validation Program and SHA-256 value of empty string (length 0) is given as in the file
SHA256ShortMsg.rsp
;Note 3: For the NIST's hash function, one should not rely on the third-party result. They need to look at NIST vectors.