Linux / Unix: Generate A MD5 String

by on June 4, 2012 · 6 comments· LAST UPDATED October 4, 2013

in , ,

I need to generate a md5 hash for given string. It will be used by shell script to generate keys for remote web service or cryptographic application. How do I generate a md5 hash based on any input string under Linux or Unix like operating systems?

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
RequirementsNone
Estimated completion time5m
You can use md5sum command to compute and check MD5 message digest. This is a default tool on most modern Linux distributions. It generate a md5 hash for given string or words or filenames.

Create a md5 string using md5sum command

Use the following syntax:

 
echo -n "Your-String-Here" | md5sum
 

In this example create a md5 hash for wpblog string that can be used by memcached server

 
 echo -n "wpblog" | md5sum
 

Sample outputs:

6afedb7a8348eb4ebdbe0c77ef92db4c -

You can store the same in a bash shell variable called hash as follows:

 
md5="set-string-here"
hash="$(echo -n "$md5" | md5sum )"
echo "$hash"
 
Recommended readings
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 6 comments… read them below or add one }

1 anton bors June 6, 2012 at 8:07 am

Reading from standard input:
$ md5sum -

a little script:

#!/bin/bash
echo $1
echo $1 | md5sum

$ ./md5 hallo
hallo
aee97cb3ad288ef0add6c6b5b5fae48a -

The “-” declares, that this message comes from standard input
greets

Reply

2 AtesComp February 21, 2014 at 1:45 am

Beware this script! You should use the “-n” option for echo as shown in the main article. Otherwise, a newline is appended to the output of the echo command and your hash will not be as advertised (unless you really want the newline).

#!/bin/bash
echo $1
echo -n $1 | md5sum

$ ./md5 hallo
hallo
598d4c200461b81522a3328565c25f7c -

Not “aee97cb3ad288ef0add6c6b5b5fae48a” as shown above.

Reply

3 Roy Bellingan June 12, 2012 at 9:32 am

Remember that md5 should only used for file hash now, in recent days linked_in had a major password leakage for using md5…
md5 and password leaked.

Reply

4 Drew H December 13, 2012 at 3:21 am

You’re an idiot mate

Reply

5 Roy Bellingan December 13, 2012 at 9:15 am

Thanks, i’ll write this on my tombstone!

Reply

6 Guy Merritt August 23, 2013 at 5:50 pm

I love this site – it’s got more simple, helpful tips for *Nix amateurs (like me!) than I’ve ever seen elsewhere. But, for the life of me, I can’t find a site search anywhere. I was looking for this page, because I’d forgotten the syntax to generate an encrypted password. What’s up with no site search…? If I wanted to search Google, I’d search Google. Anyway, thanks for all of the help. Great site.

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , ,

Previous Faq:

Next Faq: