Tuesday, August 19, 2008

Perl code to create and print SHA1 and MD5 hashes of passwords

#!/usr/bin/perl -I /usr/lib/perl5/site_perl/5.005/i386-linux

# Perl code to create and print SHA1 and MD5 hashes of passwords
# shamelessly stolen from the openLDAP Faq-O-Matic
# written 19-Jul-01 by Ed Truitt
# Updated 12-Oct-01 to include a {crypt} version of the password

$theGoodWord = $ARGV[0];
chomp($theGoodWord);

# First, print the clear text version

print "\n" ;
print "The Good Word is ==> $theGoodWord \n " ;
print "\n" ;

# Now generate and print the SHA1 hash

use Digest::SHA1;
use MIME::Base64;
$ctx = Digest::SHA1->new;
$ctx->add($theGoodWord);
$hashedSHAPasswd = '{SHA}' . encode_base64($ctx->digest,'');
print 'userPassword: ' . $hashedSHAPasswd . "\n";


# Now generate and print the MD5 hash

use Digest::MD5;
use MIME::Base64;
$ctx = Digest::MD5->new;
$ctx->add($theGoodWord);
$hashedMD5Passwd = '{MD5}' . encode_base64($ctx->digest,'');
print 'userPassword: ' . $hashedMD5Passwd . "\n";


# Now generate and print the CRYPT version

# first we need to generate the salt

@chars = ("A" .. "Z", "a" .. "z", 0 .. 9, qw(. /) );
$salt = join("", @chars[ map { rand @chars} ( 1 .. 4) ]);

# now to generate the password itself

$cryptPasswd = '{crypt}' . crypt($theGoodWord,$salt);
print 'userPassword: ' . $cryptPasswd . "\n";
print "\n";

2 comments:

mr_me said...

LOL:

[mr_me@pluto core]$ echo -n password | openssl sha1
(stdin)= 5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8

Unknown said...

Be careful here as openssl outputs the hex string representation of the binary output where as the above perl code is using base64 encoding.
i.e.:


echo -n password | openssl dgst -sha1
5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8

equivalent perl:

use Digest::SHA1 qw(sha1 sha1_hex sha1_base64);
$toHash = $ARGV[0]||'password';
print 'sha1'.sha1($toHash);
print "\n" ;
print 'sha1_base64'.sha1_base64($toHash);
print "\n" ;
print 'sha1_hex'.sha1_hex($toHash);
print "\n" ;