How to create md5 hash in bash in Mac OS X

WildBill picture WildBill · Jan 25, 2012 · Viewed 74.9k times · Source

How can you create an md5 hash for a string on a mac using bash? md5sum does not exist in my environment. I did a man for md5 but I'm confused about what that really does.

md5 "string"

does not return a hash.

Answer

jaypal singh picture jaypal singh · Jan 25, 2012

This should work -

[jaypal:~/Temp] echo "this will be encrypted" | md5
72caf9daf910b5ef86796f74c20b7e0b

or if you prefer here string notation then -

[jaypal:~/Temp] md5 <<< 'this will be encrypted'
72caf9daf910b5ef86796f74c20b7e0b

UPDATE:

Per the man page, you can play around with any of the following options

-s string
        Print a checksum of the given string.

-p      Echo stdin to stdout and append the checksum to stdout.

-q      Quiet mode - only the checksum is printed out.  Overrides the -r option.


[jaypal:~/Temp] md5 -s 'this will be encrypted'
MD5 ("this will be encrypted") = 502810f799de274ff7840a1549cd028a

[jaypal:~/Temp] md5 -qs 'this will be encrypted'
502810f799de274ff7840a1549cd028a

Note: MD5 always produces the same hash. The reason you find the output different from the example given above is due to a point that has been made in the comments. The first two examples use the trailing newline character to produce the hash. To avoid that, you can use:

[jaypal:~/Temp] echo -n "this will be encrypted" | md5
502810f799de274ff7840a1549cd028a