Difference in SHA512 between python hashlib and sha512sum tool
Asked Answered
O

3

9

I am getting different message digests from the linux 'sha512sum' tool and the python hashlib library.

Here is what I get on my Ubuntu 8.10:

$ echo test | sha512sum
0e3e75234abc68f4378a86b3f4b32a198ba301845b0cd6e50106e874345700cc6663a86c1ea125dc5e92be17c98f9a0f85ca9d5f595db2012f7cc3571945c123  -

$ python
Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49) 
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import hashlib
>>> hashlib.sha512("test").hexdigest()
'ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff'

Both should calculate the message digest of the string "test", why do you think I am getting different results?

Oligopoly answered 18/7, 2009 at 15:56 Comment(0)
S
21

I think the difference is that echo adds a newline character to its output. Try echo -n test | sha512sum

Seda answered 18/7, 2009 at 15:59 Comment(0)
I
11

echo is adding a newline:

$ python -c 'import hashlib; print hashlib.sha512("test\n").hexdigest()'
0e3e75234abc68f4378a86b3f4b32a198ba301845b0cd6e50106e874345700cc6663a86c1ea125dc5e92be17c98f9a0f85ca9d5f595db2012f7cc3571945c123

To avoid that, use echo -n.

Interstate answered 18/7, 2009 at 16:0 Comment(0)
S
2

Different input, different output. Try comparing like with like:

C:\junk>echo test| python -c "import sys, hashlib; x = sys.stdin.read(); print len(x), repr(x); print hashlib.sha512(x).hexdigest()"
5 'test\n'
0e3e75234abc68f4378a86b3f4b32a198ba301845b0cd6e50106e874345700cc6663a86c1ea125dc5e92be17c98f9a0f85ca9d5f595db2012f7cc3571945c123
Surfboarding answered 19/7, 2009 at 0:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.