Subject: | error in encode_base16 |
Hello,
i wanted to code in Perl something i found in Python :
Show quoted text
>>> from base64 import b64decode, b16encode
>>> b16encode(b64decode('KdScezWFVZxY7rHb5C4X1w==')).lower()
'29d49c7b3585559c58eeb1dbe42e17d7'
because the MD5 hash {MD5}KdScezWFVZxY7rHb5C4X1w== is base64 encoded.
MD5 hashes in the rainbow tables are in hexadecimal, so i had to convert
the two as explain in
http://security.stackexchange.com/questions/12795/how-to-decrypt-ldap-passwords-hashed-as-md5
so i wrote a little script which contains :
$password =~ s/\{MD5\}//g;
my $decoded_password = MIME::Base64::decode($password);
my $encoded_password = MIME::Base16::encode($decoded_password);
print "$password:$encoded_password:\n";
but the results are wrong...
from "suJXz0cXiOegmojABNYGTQ==" we obtain
login:b2e257cf471788e7a09a88c04d664d:
instead of
login:b2e257cf471788e7a09a88c004d6064d:
according to http://tomeko.net/online_tools/base64.php?lang=en
some 0 are misses... i think that the function encode_base16 should not
forget any 0 !
i propose to use %02x instead of %x in the sprintf function, as following :
sub encode_base16 {
my $arg = shift;
my $ret = "";
for(my $i=0;$i<length($arg);$i+=1){
my $tmp = ord(substr($arg,$i,1));
# $ret .= sprintf "%x", $tmp;
$ret .= sprintf "%02x", $tmp;
}
return $ret;
}
is it a good idea ?
may you generalise it ?
My environment :
Linux Gentoo, kernel 2.3.1, and Linux Mageia1. same problems.
perl-5.12.4-r1
thanks.
Ernest.