Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python hashlib Checksum is different from linux md5sum

I am trying to calculate the checksum of the string "test" using the python's hashlib module. I am using python3.

In [31]: hobj = hashlib.new('md5')
In [32]: hobj.update('test'.encode("UTF-8"))
In [33]: hobj.hexdigest()
Out[33]: '098f6bcd4621d373cade4e832627b4f6'

But when i am trying the same with the linux md5sum the checksum is totally different from the hashlib's output.

$ echo 'test' | md5sum 
d8e8fca2dc0f896fd7cb4cb0031ba249  -

Is there anything wrong with my python code?

like image 732
Fuji Komalan Avatar asked Oct 18 '25 03:10

Fuji Komalan


1 Answers

Use echo -n 'test' instead. echo will output a newline character ("test\n") otherwise.

$ echo -n 'test' | md5sum 
098f6bcd4621d373cade4e832627b4f6  -
like image 159
user2722968 Avatar answered Oct 19 '25 20:10

user2722968



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!