11
$\begingroup$

Could you please tell me what is the difference of coreutils' md5sum and sha*sum tools (sha1sum, sha224sum, etc.) compared to Crypto++'s digest functions?

I've written a piece of code using Crypto++ hash functions but the result is different from the corresponding Unix tools'.

For example if I write in command line:

 echo test123 | md5sum

I get:

4a251a2ef9bbf4ccc35f97aba2c9cbda

but the Crypto++ MD5 function results with:

cc03e747a6afbbcbf8be7668acfebee5

Similar things happens when I use another digest functions (as mentioned earlier).

Both Unix and Crypto++ manuals says it simply calculates digest, but says nothing about output format.

I can see both have the same length, both seem to be in hexadecimal form (0-f) and moreover when I type the hashes into Google, in both cases it says that it is an MD5 hash of test123.

Do you have any ideas about it?

$\endgroup$
  • 1
    $\begingroup$ Try echo -n test123 | md5sum. The echo command appends a newline unless used with the -n flag. $\endgroup$ – yyyyyyy Feb 25 '15 at 11:56
14
$\begingroup$

The echo command appends a new line at the end, by default. The -n option omits this character. Compare these two executions:

> echo -n "test123" | md5sum
cc03e747a6afbbcbf8be7668acfebee5

> echo "test123" | md5sum
4a251a2ef9bbf4ccc35f97aba2c9cbda

So the difference between the hash values is simply caused by the new line character.

$\endgroup$
  • $\begingroup$ indeed, that's the case. So simple, that i feel ashamed of my question ;) $\endgroup$ – SP5RFD Feb 25 '15 at 12:10
  • $\begingroup$ @crooveck Don't worry — I guess most people have discovered this trap some time... $\endgroup$ – yyyyyyy Feb 25 '15 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.