vmonteco vmonteco - 7 months ago 17
Bash Question

How to hash a string without adding a trailing '\n' to it?

I'm trying to use md5 in both python and shell (zsh on OSX).

I got this :

In python :

>>> hashlib.md5("zoo".encode('utf-8')).hexdigest()
'd2cbe65f53da8607e64173c1a83394fe'


with Zsh :

$ echo "zoo" | md5
ace34ff0a763d9f382cbe6b219fcac7c


Since I also have :

>>> hashlib.md5("zoo\n".encode('utf-8')).hexdigest()
'ace34ff0a763d9f382cbe6b219fcac7c'


in Python, I think that my zsh command adds a (unwanted) trailing '\n' to the string before hashing it.

Why does this happen, and how could I avoid this?

Answer

here you go:

kent$  echo -n 'zoo'|md5sum
d2cbe65f53da8607e64173c1a83394fe

kent$  echo  'zoo'|md5sum 
ace34ff0a763d9f382cbe6b219fcac7c 
Comments