How to get the MD5 hash of a string directly in the terminal?

569,072

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.

Share:
569,072

Related videos on Youtube

2hamed
Author by

2hamed

Go/Java/Kotlin/PHP

Updated on September 18, 2022

Comments

  • 2hamed
    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
      raitisd over 6 years
      md5 -s abcdefg
    • ivanleoncz
      ivanleoncz about 4 years
      Don'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
    Tom almost 13 years
    Giving 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
    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
    2hamed almost 13 years
    it's weird but the <<< operator and the printf function are giving completely different results for md5 hash! the result of printf is correct though!
  • 2hamed
    2hamed almost 13 years
    it seems using the <<< operator sends a newline to the md5sum!
  • enzotib
    enzotib almost 13 years
    Yes, it does, as I said between first and second example
  • 2hamed
    2hamed over 12 years
    yeah, you're right too. but ctrl+d needs to be pressed twice for it to work.
  • psusi
    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
    Xanthir about 9 years
    You 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
    keune over 8 years
    why are there spaces and a dash at the end?
  • jfmessier
    jfmessier over 8 years
    Please 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
    derFunk over 8 years
    Don'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
    Pithikos over 8 years
    Notice that the -n is mandatory. Without it, your hash will be totally wrong since it includes the newline character.
  • Gucho Ca
    Gucho Ca about 8 years
    This is just too long.
  • Edward J. Stembler
    Edward J. Stembler about 7 years
    This helped me on macOS too, though it's slightly different: echo -n Welcome | md5
  • phil294
    phil294 over 6 years
    alternative to awk: cut -d ' ' -f 1
  • alper
    alper about 6 years
    How could I ignore - at the end. @jfmessier
  • Ruben Benjamin
    Ruben Benjamin over 5 years
    I 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
    Alex Stragies almost 5 years
    Shorter awk command: '$0=$1'
  • Jonas Davidsson
    Jonas Davidsson almost 5 years
    @AlexStragies yep
  • Macindows
    Macindows almost 4 years
    @GuchoCa Maybe long but he has done the work for you. Just create a script file and execute!
  • domdambrogia
    domdambrogia over 3 years
    @alper echo -n Welcome | md5sum | awk '{print $1}' will take the first "column" separated by spaces. You could also use cut as well.
  • yannis
    yannis over 3 years
    On Mac OS, instead of md5sum, write md5.
  • Salem F
    Salem F about 2 years
    I got wrong result with the first command b9bfa87a6a126911f2246c7a615bff27 - instead of 2ba81a47c5512d9e23c435c1f29373cb -
  • Admin
    Admin about 2 years
    What'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.