How to get the MD5 hash of a string directly in the terminal?
Solution 1
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.
Solution 2
Very simple, it accepts stdin, so
md5sum <<<"my string"
To avoid the trailing newline added by the shell:
printf '%s' "my string" | md5sum
Solution 3
$ 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
Solution 4
openssl md5 filename
openssl sha1 filename
For string pipe the content using echo
echo -n 123456 | openssl md5
Solution 5
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.
Related videos on Youtube
Comments
-
2hamed almost 2 years
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?
-
raitisd over 6 years
md5 -s abcdefg
-
ivanleoncz about 4 yearsDon't know why this question is here on this forum. It should be on Unix & Linux, Super User, but here? I'm not sure. Anyway, it has value, wherever it should be.
-
-
Tom almost 13 yearsGiving both @messier and @enzotib a vote; both fall in my prized "elegant simplicity" category. I'd be apt to use the
<<<"
pipe in a script;echo string
wins for the commandline. Well done. -
Lekensteyn almost 13 years+1 for using
printf
correctly. If you want to have the sum without the-
, put| cut -d ' ' -f 1
. Example usage:sum=$(printf '%s' 'some string' | md5sum | cut -d ' ' -f 1)
-
2hamed almost 13 yearsit's weird but the
<<<
operator and theprintf
function are giving completely different results for md5 hash! the result of printf is correct though! -
2hamed almost 13 yearsit seems using the
<<<
operator sends a newline to the md5sum! -
enzotib almost 13 yearsYes, it does, as I said between first and second example
-
2hamed over 12 yearsyeah, you're right too. but
ctrl+d
needs to be pressed twice for it to work. -
psusi over 12 years@James, if it does not follow a newline, yes. If you hit it after hitting enter, it only needs once. When it does not follow a newline, it just forces all of the characters typed on the line so far to be processed immediately instead of waiting for a newline.
-
Xanthir about 9 yearsYou also want to update the example result, as 7803ffca... is the result with the added newline. The correct result for the command above is
83218ac34c1834c26781fe4bde918ee4 -
-
keune over 8 yearswhy are there spaces and a dash at the end?
-
jfmessier over 8 yearsPlease correct me if I'm wrong, but I think this is because the MD5sum was applied to a stream of data, as opposed to reading a file content, which has a name associated to it.
-
derFunk over 8 yearsDon't forget the
-n
parameter here to avoid outputting the trailing newline, which would lead to a wrong md5:echo -n $myvariable | md5sum | cut -d" " -f1
-
Pithikos over 8 yearsNotice that the
-n
is mandatory. Without it, your hash will be totally wrong since it includes the newline character. -
Gucho Ca about 8 yearsThis is just too long.
-
Edward J. Stembler about 7 yearsThis helped me on macOS too, though it's slightly different:
echo -n Welcome | md5
-
phil294 over 6 yearsalternative to awk:
cut -d ' ' -f 1
-
alper about 6 yearsHow could I ignore
-
at the end. @jfmessier -
Ruben Benjamin over 5 yearsI created a function
md5() { echo -n $1 | md5sum | awk '{print $1}'; }
in .bashrc and then I can use$ md5 test
in the command line. thanks for the answer -
Alex Stragies almost 5 yearsShorter
awk
command:'$0=$1'
-
Jonas Davidsson almost 5 years@AlexStragies yep
-
Macindows almost 4 years@GuchoCa Maybe long but he has done the work for you. Just create a script file and execute!
-
domdambrogia over 3 years@alper
echo -n Welcome | md5sum | awk '{print $1}'
will take the first "column" separated by spaces. You could also usecut
as well. -
yannis over 3 yearsOn Mac OS, instead of
md5sum
, writemd5
. -
Salem F about 2 yearsI got wrong result with the first command
b9bfa87a6a126911f2246c7a615bff27 -
instead of2ba81a47c5512d9e23c435c1f29373cb -
-
Admin about 2 yearsWhat's unusual is that stdin vs args has a slightly different behavior. As an arg, it will check if the file exists. For stdin it does not.