Trailing space when generating md5

hashsumopenssl

I apologize if this has already been answered, or if the answer is simpler than I realize, but I can't seem to figure out the following:

When I try to generate an md5 from a string, either with

echo -n "string" | md5sum | cut -f1 -d' '

or with

echo -n "string" | openssl md5

the result is not 32 characters, as I would expect, but rather 33 (using wc -c).

So, I have a few questions:

  1. Why do both md5sum and openssl add a trailing space?
  2. Is there another way to generate an md5 hash without a trailing newline or space?
  3. Does the trailing space really matter?

Thank you all in advance.

Best Answer

It's 32 characters! The md5sum is adding a linefeed to the end. You can get rid of it like this:

% echo -n string | md5sum|awk '{print $1}'|wc -c
33
% echo -n $(echo -n string | md5sum|awk '{print $1}')|wc -c
32

or you could do it like this:

% echo -n $(md5sum <<< 'string'|awk '{print $1}')|wc -c
32

You can tell when one of the commands is adding a newline because the 32 character string will show up on its own line. If no newline is present it should always show up like this:

[prompt %] echo -n $(md5sum <<< 'string'|awk '{print $1}')
b80fa55b1234f1935cea559d9efbc39a[prompt %]
Related Question