Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between md5sum output and Python hashlib output?

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.

like image 401
Mark C Avatar asked May 25 '18 16:05

Mark C


People also ask

What is Python MD5?

Tags:cryptography | hashing | md5 | python. The MD5, defined in RFC 1321, is a hash algorithm to turn inputs into a fixed 128-bit (16 bytes) length of the hash value. Note. MD5 is not collision-resistant – Two different inputs may producing the same hash value.

What is Hexdigest Python?

hexdigest() : the digest is returned as a string object of double length, containing only hexadecimal digits. This may be used to exchange the value safely in email or other non-binary environments.


1 Answers

echo adds an implicit newline by default.

$ echo -n "test string" | openssl dgst -md5
(stdin)= 6f8db599de986fab7a21625b7916589c
like image 174
Ignacio Vazquez-Abrams Avatar answered Sep 29 '22 01:09

Ignacio Vazquez-Abrams