How do I get the MD5 hash of a string directly from the terminal?
For example, I want the string abcdefg hashed. Currently the md5sum command only accepts a filename as input. I want to simply enter the following line and everything be done with.
md5sum abcdefg output: ac54bcf346e578feb46888b3ecd2344f How can I achieve that?
29 Answers
You can also say something like this :
~$ echo -n Welcome | md5sum 83218ac34c1834c26781fe4bde918ee4 - It basically does the same thing as described by @enzotib, but is perhaps a bit simpler.
9Very simple, it accepts stdin, so
md5sum <<<"my string" To avoid the trailing newline added by the shell:
printf '%s' "my string" | md5sum 6$ echo -n 123456 | md5sum | awk '{print $1}' e10adc3949ba59abbe56e057f20f883e you can create a shell script.
For example,the script name is md5.sh:
#!/bin/bash echo -n $1 | md5sum | awk '{print $1}' permission execute:
chmod +x md5.sh Then:
$ md5.sh 123456 e10adc3949ba59abbe56e057f20f883e If your system is macOS. You need to modify this script:
$ echo -n 123456 | md5 | awk '{print $1}' e10adc3949ba59abbe56e057f20f883e 6openssl md5 filename openssl sha1 filename For string pipe the content using echo
echo -n 123456 | openssl md5 Running md5sum with no arguments at all will cause it to read input from the terminal. Type or paste whatever you want, and when you are done, press ctrl-d to end the input.
My quick poke at the --help for md5sum demonstrates that the command:
md5sum - will then give a prompt for simple input. Inputting some text and then using Enter and then Ctrl+D to signify end of file then causes md5sum to spit out the MD5 of the raw text you entered (including that Enter, it's a CR, IIRC).
Less to type and no piping! And avoiding your plaintext password being recorded in shell history! Woo!
If you do not want that trailing CR (which is usually the case if you want to hash a password), don't hit Enter before Ctrl+D, enter Ctrl+D twice instead.
There are many examples to do this, but some of them are not equivalent because some of them explicitly or implicitly include the newline, and some others do not.
I would like to clearly specify which of the popular methods includes the newline and which are not.
Here are some examples along to calculating the md5 hash WITHOUT trailing newline (CORRECT):
Using a file with text:
$ echo -n "test" > test.txt $ wc test.txt 0 1 4 test.txt $ md5sum test.txt 098f6bcd4621d373cade4e832627b4f6 test.txt Note: -n in echo means: "do not output the trailing newline".
Using echo with -n inline:
$ echo -n "test" | md5sum 098f6bcd4621d373cade4e832627b4f6 - Using printf:
$ printf "%s" "test" | md5sum 098f6bcd4621d373cade4e832627b4f6 - Using only md5sum command:
(Let's write md5sum, press Enter then write string test and then press double combination Ctrl+d)
$ md5sum test098f6bcd4621d373cade4e832627b4f6 - Using md5sum - command:
(Let's write md5sum -, press Enter then write string test and then press double combination Ctrl+d)
$ md5sum - test098f6bcd4621d373cade4e832627b4f6 - Here are some examples along to calculating the md5 hash WITH trailing newline (SO NOT CORRECT):
Using a file with text:
$ echo "test" > test_n.txt $ wc test_n.txt 1 1 5 test_n.txt $ md5sum test_n.txt d8e8fca2dc0f896fd7cb4cb0031ba249 test_n.txt Using echo WITHOUT -n inline:
echo "test" | md5sum d8e8fca2dc0f896fd7cb4cb0031ba249 - Using here strings:
$ md5sum <<< "test" d8e8fca2dc0f896fd7cb4cb0031ba249 - Using only md5sum command but with Enter key after writing the text:
(Let's write md5sum, press Enter then write string test and then press agaien Enter and once combination Ctrl+d)
$ md5sum test d8e8fca2dc0f896fd7cb4cb0031ba249 - Using md5sum - command but with Enter key after writing the text:
(Let's write md5sum -, press Enter then write string test and then press agaien Enter and once combination Ctrl+d)
$ md5sum - test d8e8fca2dc0f896fd7cb4cb0031ba249 - In my scripts I found that there are 2 things that you should know about this issue.
- It does not matter if you do
echo "$myvariable"orecho -n "$myvariable"but you should always use the doubleqoutes for strings and always use the same method. if not things won't match. in the output you get always a trailing space and a dash as shown in the example:
$ echo -n Welcome | md5sum 7803ffcaea43bb81a439fde13b29bc35 -
to get rid of that and stay only with the code 7803ffcaea43bb81a439fde13b29bc35, do: echo "$myvariable" | md5sum | cut -d" " -f1
For my case the best solution to hash a plain string is:
md5hash=$(echo -n "myhashedstring" | md5sum | head -c 32) will result in 2e8f8b94e488a2f442a55951862612aa