2010-07-28 159 views
143

我知道字符串 “foobar的” 一个SHA256生成使用 http://hash.online-convert.com/sha256-generator生成从Linux命令行

然而命令行壳的SHA 256散列c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2

[email protected] ~$ echo foobar | sha256sum 
aec070645fe53ee3b3763059376134f058cc337247c978add178b6ccdfb0019f - 

生成一个不同的散列。我错过了什么?

+2

'sha256sum koppor 2016-12-17 20:10:29

+3

@koppor' mvds 2017-02-16 15:07:49

回答

210

echo通常会输出换行符,该换行符被-n抑制。试试这个:

echo -n foobar | sha256sum 
+0

这个命令'sha256sum'与Solaris中的digest命令相同吗? – user3153014 2014-05-09 07:22:24

+0

似乎是这样,http://www.opensolarisforum.org/man/man1/digest.html,'digest -a sha256'应该这样做。 – mvds 2014-05-09 07:46:24

+39

注意:在OS X(BSD)上,它是'echo -n foobar | shasum -a 256' – Olie 2014-10-16 15:26:31

7

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

7

echo会生成一个尾随的换行符,它也会被哈希。尝试:

/bin/echo -n foobar | sha256sum 
25

echo -n作品,也不可能永远消失,由于大量的历史使用情况,但是每最新版本的the POSIX standard,新遵从的应用程序被“鼓励使用printf”。

35

如果命令sha256sum不可用(对小牛为例),你可以使用:

echo -n "foobar" | shasum -a 256

+0

不错!我已经将这添加到我的.bash_profile 函数sha256(){echo -n“$ *”| shasum -a 256} 并且调用方式如下:〜$ sha256 foobar – rbento 2017-02-15 05:52:56

+0

与接受的答案不同,这适用于MacOS – weefwefwqg3 2017-09-17 04:18:46

49

如果您已经安装openssl,你可以使用:

echo -n "foobar" | openssl dgst -sha256 

对于其他您可以用-md4-md5-ripemd160,-sha,-sha1,替换,-sha384-sha512-whirlpool

+0

有没有方法可以指定sha512中需要的回合数?我看了一下,找不到它,并想知道你是否会知道? – f1lt3r 2017-06-08 23:57:14

+1

@AlistairMacDonald - 我不知道你在找什么。AFAIK,SHA512需要80轮;如果你想操纵这个功能,它将不再是sha512。顺便说一句,你可以在[crypto.stackexchange.com](https://crypto.stackexchange.com/“Cryptography Stack Exchange”)中搜索/询问你的问题。 – Farahmand 2017-06-09 12:27:30

+1

与接受的答案不同,这适用于MacOS。 – weefwefwqg3 2017-09-17 04:18:22