Reputation: 10843
I know the string "foobar" generates the SHA-256 hash c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2
using
http://hash.online-convert.com/sha256-generator
However the command line shell:
hendry@x201 ~$ echo foobar | sha256sum
aec070645fe53ee3b3763059376134f058cc337247c978add178b6ccdfb0019f -
Generates a different hash. What am I missing?
Upvotes: 385
Views: 566922
Reputation: 2991
If you have installed openssl
, you can use:
echo -n "foobar" | openssl dgst -sha256
For other algorithms you can replace -sha256
with:
-blake2b512
-blake2s256
-md4
-md5
-md5-sha1
-ripemd
-ripemd160
-rmd160
-sha1
-sha224
-sha256
-sha3-224
-sha3-256
-sha3-384
-sha3-512
-sha384
-sha512
-sha512-224
-sha512-256
-shake128
-shake256
-sm3
-ssl3-md5
-ssl3-sha1
-whirlpool
The list of all algorithms can be found from here:
openssl dgst -list
Upvotes: 141
Reputation: 89442
Use printf
instead of echo
to avoid adding an extra newline.
printf foobar | sha256sum
For an arbitrary string, the %s
format specifier should be used.
printf '%s' 'somestring' | sha256sum
Upvotes: 17
Reputation: 52768
For the sha256 hash in base64, use:
echo -n foo | openssl dgst -binary -sha256 | openssl base64
echo -n foo | openssl dgst -binary -sha256 | openssl base64
C+7Hteo/D9vJXQ3UfzxbwnXaijM=
Upvotes: 19
Reputation: 3034
If the command sha256sum is not available (on Mac OS X v10.9 (Mavericks) for example), you can use:
echo -n "foobar" | shasum -a 256
Upvotes: 57
Reputation: 28767
echo
produces a trailing newline character which is hashed too. Try:
/bin/echo -n foobar | sha256sum
Upvotes: 14
Reputation: 47114
echo
will normally output a newline, which is suppressed with -n
. Try this:
echo -n foobar | sha256sum
Upvotes: 603
Reputation: 16045
echo -n
works 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
".
Upvotes: 39
Reputation: 116187
I believe that echo
outputs a trailing newline. Try using -n
as a parameter to echo to skip the newline.
Upvotes: 9