How to create md5 hash in bash in Mac OS X
Asked Answered
S

8

75

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.

Seppuku answered 25/1, 2012 at 1:28 Comment(0)
A
127

This should work -

[jaypal:~/Temp] echo "this will be hashed" | md5
2caf9daf910b5ef86796f74c20b7e0b

or if you prefer here string notation then -

[jaypal:~/Temp] md5 <<< 'this will be hashed'
55be2dc2df2c1cc7bad72a0ecb338841

UPDATE:

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

[jaypal:~/Temp] man md5
MD5(1)           General Commands Manual           MD5(1)
...
NAME
     md5 – calculate a message-digest fingerprint (checksum) for a file
-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

For example, if you use echo -n "string" | md5 (note the -n option), you get b45cffe084dd3d20d928bee85e7b0f21. But, if you use echo "string" | md5, you get b80fa55b1234f1935cea559d9efbc39a.

Or, verify it with the shell:

➜  [jaypal:~/Temp] [ $(echo "HOLA" | md5) = $(echo "HOLA" -n | md5) ]; echo "$?"
1
# 1 -> False. Hence, the result from echoing "HOLA" toggling the -n flag
# outputs different md5 checksums.
Auspicious answered 25/1, 2012 at 1:32 Comment(3)
+1 but with a correction: this will be *hashed*, encryption denotes bi-directional ;)Severable
Also, take care with the line terminators. You've hashed "this will be encrypted\n". The MD5 sum of "this will be encrypted" is 502810f799de274ff7840a1549cd028a, which you can get via echo -n "this will be encrypted" | md5.Deli
Unfortunately, echo -n isn't very portable -- on some platforms, it adds "-n " in front of the string to be printed (and then adds the newline as well). Use printf "%s" "this will be encrypted" | md5 for much better portability.Iodism
M
36

To achieve what you asked:

md5 -s string

outputs: MD5 ("string") = b45cffe084dd3d20d928bee85e7b0f21

Mercaptopurine answered 3/12, 2014 at 13:52 Comment(1)
If you get stuck with PHP md5, then this is the solution. The accepted answer did not rescue PHP md5 password. This helped me to allow to login again to my CMS.Belkisbelknap
D
17

OSX uses md5 but most unices use md5sum

Here is a section of rvm's rvmrc validation code which finds the correct md5 binary and wraps it.

__rvm_md5_for()
{
  if builtin command -v md5 > /dev/null; then
    echo "$1" | md5
  elif builtin command -v md5sum > /dev/null ; then
    echo "$1" | md5sum | awk '{print $1}'
  else
    rvm_error "Neither md5 nor md5sum were found in the PATH"
    return 1
  fi

  return 0
}

( Code from https://github.com/wayneeseguin/rvm/blob/master/scripts/functions/rvmrc )

Dramatics answered 25/1, 2012 at 1:46 Comment(0)
S
3

The correct way of doing that would be echo -n string | md5 instead of echo "string" | md5. (I am using zsh)

Convert the md5 hash given by echo -n string | md5 you will get back string.

md5 -s string also works which is already pointed out here.

λ [~] → echo "string" | md5
b80fa55b1234f1935cea559d9efbc39a

λ [~] → echo -n string | md5
b45cffe084dd3d20d928bee85e7b0f21

λ [~] → md5 -s string
MD5 ("string") = b45cffe084dd3d20d928bee85e7b0f21
Stochastic answered 22/12, 2018 at 20:19 Comment(0)
H
3

All the other answers are valid. I would like to also propose openssl as well:

➜ echo 'this will be hashed' | openssl md5
55be2dc2df2c1cc7bad72a0ecb338841

which is equivalent to the following

➜ echo 'this will be hashed' | openssl dgst -md5
# or
➜ openssl md5 <<< 'this will be hashed'
# or
➜ echo 'this will be hashed' | md5
Hakan answered 11/1, 2019 at 15:43 Comment(0)
W
2

From the command line:

md5 <<< "String to hash"
8a0a39505c5753ff64a0377ab0265509
Willwilla answered 8/12, 2014 at 1:20 Comment(1)
note: doing it this way includes a newline as part of what gets hashed. In other words, this gives a different result than md5 -s "String to hash" because by default, bash adds newlines to the end of here stringsAnimal
E
1

If you need to print md5 of a file you can call it directly

md5 file.txt

Outputs:

MD5 (file.txt) = 91a7644643c884ee00737db24e478156
Easting answered 11/4, 2023 at 12:37 Comment(0)
H
-5

You may want to use some randomness here, otherwise the password will always be the same. This should work:

dd if=/dev/random count=20|md5
Headstand answered 29/9, 2014 at 0:9 Comment(2)
I think this causes more confusion than it helps. Yes, a salt / random addition could help md5-ing a password (although this is not how you would use it). However, this doesn't answer the question asked.Dunfermline
Match the 128 bitspace: dd if=/dev/urandom count=16 bs=1 | md5Chesnut

© 2022 - 2024 — McMap. All rights reserved.