sha256sum and other sha2 generators give a different hash

Chat about just about anything else
Post Reply
miclis
Level 1
Level 1
Posts: 19
Joined: Thu Nov 03, 2016 3:36 pm

sha256sum and other sha2 generators give a different hash

Post by miclis »

Hi

Today I realized that the same input given into a command sha256sum and an online sha256 calculator gives a different output.
For example:
$ echo "4321" | sha256sum
ff5a972ba33179c7ec67c73e00a362b629c489f9d7c86489644db2bcd8c62c61

While those websites http://www.xorbin.com/tools/sha256-hash-calculator and http://passwordsgenerator.net/sha256-hash-generator/ for the same input (4321) give a different hash:
fe2592b42a727e977f055947385b709cc82b16b9a87f88c6abf3900d65d0cdc3

I am curious why is it like that. Does sha256sum on Linux use not the same version of sha256 algorithm or is it just me who used a wrong command in order to get a hash?

Cosmo.
Level 23
Level 23
Posts: 17817
Joined: Sat Dec 06, 2014 7:34 am

Re: sha256sum and other sha2 generators give a different hash

Post by Cosmo. »

At first and in general: The same hash sum is and must be always the same, regardless which toll and which OS you used.
Your - at the first look mysterious - finding has the background, that you hashed 2 different things.
On the website you hashed 4321, but with your terminal code you hashed actually 4321 followed by a line feed; obviously there must be a different result.

Go to the first page that you linked, enter 4321 and press the Enter-key afterwards, now compute the hash again. You will find, that this matches perfectly with your terminal result.

miclis
Level 1
Level 1
Posts: 19
Joined: Thu Nov 03, 2016 3:36 pm

Re: sha256sum and other sha2 generators give a different hash

Post by miclis »

I see. Thank you for your response. I didn't know that the echo command adds a new line automatically. I've tried to calculate the hash with printf and echo -n commands and now the output is the same as on these websites.

$ echo -n "4321" | sha256sum
fe2592b42a727e977f055947385b709cc82b16b9a87f88c6abf3900d65d0cdc3

$ printf 4321 | sha256sum
fe2592b42a727e977f055947385b709cc82b16b9a87f88c6abf3900d65d0cdc3

Thanks once again!

Post Reply

Return to “Open chat”