#!/usr/bin/perl # # pbkdf2_direct - generate a cryptographic hash of some text # # Direct Perl Implementation of pbkdf2 iterative hashing function # # As command... # pbkdf2_direct.pl salt_hex iterative_count < password > hex_key_and_iv # # OR as module... # # require "pbkdf2_direct.pl"; # $key = pbkdf2( $password, $salt, $iter, $keylen, $prf) # # # $iter is number of iterations # # $keylen is length of generated key in bytes # # $prf is the pseudo random function (e.g. hmac_sha1) # # returns the key. # ### # # Initial function Jochen Hoenicke from the # Palm::Keyring perl module. Found on the PerlMonks Forum # http://www.perlmonks.org/?node_id=631963 # # Modulized 2 Sept 2010 Anthony Thyssen # # Usage pbkdf2(prf, password, salt, iter, keylen) sub pbkdf2($$$$$) { my ($prf,$password, $salt, $iter, $keylen) = @_; my ($k, $t, $u, $ui, $i); $t = ""; for ($k = 1; length($t) < $keylen; $k++) { $u = $ui = &$prf($salt.pack('N', $k), $password); for ($i = 1; $i < $iter; $i++) { $ui = &$prf($ui, $password); $u ^= $ui; } $t .= $u; } return substr($t, 0, $keylen); } # # Direct call of module as a program.. # # pbkdf2.pl salt_hex iterative_count < password > hex_key_and_iv # if (!caller) { require Digest::HMAC_SHA1; import Digest::HMAC_SHA1 qw(hmac_sha1); my $salt = pack("H*",shift); # salt from hex input (typically 8 bytes) my $ic = shift; # iterative count my $password = ; $password =~ s/\n$//; print "Salt Length: ", length($salt), "\n"; my $key = pbkdf2(\&hmac_sha1, $password, $salt, $ic, 32+16); $hex=uc( unpack("H*",$key) ); # output key as hexadecimal print "$hex\n"; # output as a key and iv print "KEY=", substr($hex,0,64), "\nIV =", substr($hex,64), "\n"; exit 0; } 1;