Krystian Cybulski
Krystian Cybulski

Reputation: 11108

Is the md5sum linux command working right?

According to Wikipedia, the md5 sum of an empty string is d41d8cd98f00b204e9800998ecf8427e

I confirmed this with my md5 library

However, when I run

echo "" | md5sum

in my linux shell, I get 68b329da9893e34099c7d8ad5cb9c940 -

In fact, none of my hashes match the output of the md5sum command.

Any thoughts on this discrepancy?

Upvotes: 3

Views: 2013

Answers (5)

user8395964
user8395964

Reputation: 142

  • The <<< redirection operator in bash also appends newline.

  • So, the following will also yield incorrect result.

    $ md5sum - <<< 'hello'
    68b329da9893e34099c7d8ad5cb9c940 *-
    
  • The printable mode of octal dump utility od can diagnose both sources i.e. | pipe & <<< redirection:

    $ echo -n '' | od -c
    0000000
    
    $ echo 'hello' | od -c
    0000000  \n
    0000001
    
    $ od -c <<< ''
    0000000  \n
    0000001
    

The "octal dump" is what I think the name od stands for. Reasons:

  • od's help:
    $ od --help | sed -n '5p;'
    Write an unambiguous representation, octal bytes by default,
    
  • mankier od(1) says the title: "od: dump files in octal ..."
  • Other utils have similar names too like objdump

Coming here from the duplicates:

Upvotes: 1

ghostdog74
ghostdog74

Reputation: 342363

use the more portable printf

printf "" | md5sum

Upvotes: 2

divegeek
divegeek

Reputation: 5022

Try:

echo -n | md5sum

Without the '-n', echo outputs a newline, which md5sum duly processes.

Upvotes: 5

Thilo
Thilo

Reputation: 262504

You must eliminate the new line that echo produces

$ echo -n '' | md5
d41d8cd98f00b204e9800998ecf8427e

Upvotes: 7

caf
caf

Reputation: 239041

With that command, you are calculating the md5sum of a single newline character. Try instead:

echo -n "" | md5sum

Upvotes: 14

Related Questions