What is the difference between md5sum output and Python hashlib output?
Asked Answered
S

1

6

Why is the output of hashlib.md5().hexdigest() different than md5sum and openssl output?

$ echo "test string" | md5sum
f299060e0383392ebeac64b714eca7e3  -
$ echo "test string" | openssl dgst -md5
(stdin)= f299060e0383392ebeac64b714eca7e3
$ python
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from hashlib import md5
>>> print(md5("test string").hexdigest())
6f8db599de986fab7a21625b7916589c

I noticed this while trying to generate an md5 digest for use with Gravatar. The Python hashlib output works but the md5sum and openssl outputs do not.

Stahl answered 25/5, 2018 at 16:11 Comment(0)
F
9

echo adds an implicit newline by default.

$ echo -n "test string" | openssl dgst -md5
(stdin)= 6f8db599de986fab7a21625b7916589c
Fiscal answered 25/5, 2018 at 16:13 Comment(2)
I knew I was missing something. Thanks! I'll accept as soon as it lets me.Stahl
printf '%s' "test string" if you are using a shell or echo that doesn't support -n.Quemoy

© 2022 - 2024 — McMap. All rights reserved.