views:

111

answers:

5

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?

+5  A: 

-n

echo -n foobar | sha256sum

echo includes a newline!

mvds
>_< 32 seconds.
Thomas Owens
funny game isn't it
mvds
A: 

I believe that echo outputs a trailing newline. Try using -n as a parameter to echo to skip the newline.

Thomas Owens
A: 

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

/bin/echo -n foobar | sha256sum 
Luther Blissett
A: 

echo appends a newline to the argument so you should use

echo -n foobar | sha256sum 
Moritz
+4  A: 

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".

Nicholas Knight