从 Linux 命令行生成 sha256

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3358420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:19:11  来源:igfitidea点击:

Generating a sha256 from the Linux command line

linuxshellsha256

提问by hendry

I know the string "foobar" generates the SHA 256 hash c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2using http://hash.online-convert.com/sha256-generator

我知道字符串“foobar”c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2使用 http://hash.online-convert.com/sha256-generator生成 SHA 256 哈希

However the command line shell:

但是命令行外壳:

hendry@x201 ~$ echo foobar | sha256sum 
aec070645fe53ee3b3763059376134f058cc337247c978add178b6ccdfb0019f  -

Generates a different hash. What am I missing?

生成不同的哈希。我错过了什么?

采纳答案by mvds

echowill normally output a newline, which is suppressed with -n. Try this:

echo通常会输出一个换行符,用-n. 尝试这个:

echo -n foobar | sha256sum

回答by Thomas Owens

I believe that echooutputs a trailing newline. Try using -nas a parameter to echo to skip the newline.

我相信echo输出尾随换行符。尝试使用-n作为参数来回显以跳过换行符。

回答by Nordic Mainframe

echo produces a trailing newline character which is hashed too. try:

echo 生成一个尾随换行符,该字符也经过哈希处理。尝试:

/bin/echo -n foobar | sha256sum 

回答by Nicholas Knight

echo -nworks and is unlikely to ever disappear due to massive historical usage, however per recent versions of the POSIX standard, new conforming applications are "encouraged to use printf".

echo -n由于大量的历史使用,工作并且不太可能消失,但是根据POSIX 标准的最新版本,“鼓励使用printf”新的符合标准的应用程序。

回答by Sucrentheitroad

If the command sha256sum is not available (on mavericks for example), you can use :

如果命令 sha256sum 不可用(例如在小牛上),您可以使用:

echo -n "foobar" | shasum -a 256

echo -n "foobar" | shasum -a 256

回答by Farahmand

If you have installed openssl, you can use:

如果已安装openssl,则可以使用:

echo -n "foobar" | openssl dgst -sha256

For other algorithms you can replace -sha256with -md4, -md5, -ripemd160, -sha, -sha1, -sha224, -sha384, -sha512or -whirlpool.

对于其他算法,您可以替换-sha256-md4-md5-ripemd160-sha-sha1-sha224-sha384-sha512-whirlpool