The hidden newline
In many applications within our data repository, strings are converted to MD5 checksum hash values to preclude dealing with unsavory characters (e.g., non-UTF8, whitespace, etc...). It is not uncommon that these strings must be manually hashed when hunting down errors. For the longest time, I could never figure out why my command line-generated hashes never resulted in the same values as the embedded MD5 sums in our software. What a "doh" moment when I realized that using echo
on the command line always ended the string with a newline character (\n
), which was added to the computation of the hash, thereby making it differ from the embedded version. The solution, however, is simple: use the -n
with echo
or forego echo
and use printf
instead. For example
echo -n 'hello' | md5sum
Similarly, printf
works just as well and has more options (if required):
printf '%s' 'hello' | md5sum