Linux / Unix: Generate A MD5 String

by on June 4, 2012 · 4 comments· last updated at June 4, 2012

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?

You can use md5sum command to compute and check MD5 message digest. This is a default tool on most modern Linux distributions 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"
 


You should follow me on twitter here or grab rss feed to keep track of new changes.

Featured Articles:

{ 4 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 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

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

You’re an idiot mate

Reply

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

Thanks, i’ll write this on my tombstone!

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <kbd> <blockquote> <pre> <a href="" title="">

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

Previous Faq:

Next Faq: