Get a md5 of a string on a command line

One of the web applications I maintain stores md5 hashes in the database, instead of the actual passwords. This is a good practice – passwords are not stored in plain text, and knowing the has still does not give you a direct access to the system.

I whipped up this tiny shell script in order to easily get md5 hashes of strings on the command line (for example in case I need to go in and change some password directly in the database).

#!/usr/bin/perl
use Digest::MD5 qw(md5 md5_hex md5_base64);
print(md5_hex($ARGV[0]) . "\n")

This script takes a parameter string and prints out the md5 hash of that string to the standard output stream.

[tags]md5, pearl, hash, passwords, hashing, scripting[/tags]

This entry was posted in sysadmin notes and tagged , . Bookmark the permalink.



5 Responses to Get a md5 of a string on a command line

  1. Craig Betts UNITED STATES Mozilla Firefox Solaris Terminalist says:

    Here is my version of the same thing . . .

    #!/bin/sh

    echo $* | openssl dgst -md5

    I always seem to have openssl, but not always Digest::MD5.

    Reply  |  Quote
  2. Luke UNITED STATES Mozilla Firefox Ubuntu Linux says:

    Heh – I didn’t even think of that. I like yours better than mine now!

    I think what we have here is programmer vs. admin way of thinking.

    Programmer automatically goes “hey, I bet there is a [some-language] module/library for that somewhere…” when admin goes “I bet there is a tool for that somewhere…” :P

    Reply  |  Quote
  3. Utku Can Ölmez TURKEY Mozilla Firefox Windows says:

    echo -n test | md5sum

    Reply  |  Quote
  4. Utku Can Ölmez TURKEY Mozilla Firefox Windows says:

    You can do it without openssl.

    #!/bin/sh
    echo -n $* | md5sum

    Reply  |  Quote
  5. Utku Can Ölmez TURKEY Mozilla Firefox Windows says:

    You can do it without openssl & dont forget to use ‘-n’ with echo.

    #!/bin/sh
    echo -n $* | md5sum

    Reply  |  Quote

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>