in reply to MD5 -- not digest::md5

I wrote a simple script for encoding a string in md5
It is strange to say that the MD5 algorithm can be used as a form of encoding. MD5 is a one-way hashing function, with no simple way of decoding the result. In essence it can only be used as a digest.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Replies are listed 'Best First'.
Re^2: MD5 -- not digest::md5
by Oromis92 (Sexton) on Aug 15, 2009 at 15:39 UTC
    ok, i followed all your hints...
    #!/usr/bin/perl use Digest::MD5 qw(md5 md5_hex md5_base64); use strict; use warnings; use diagnostics; use integer; my @r = ((7,12,17,22) x 4 , (5,9,14,20) x 4 , (4,11,16,23) x 4 , (6,10 +,15,21) x 4); my @k; $k[$_] = (int(abs(sin($_+1))*(2**32))) for 0..63; my $H0 = 0x67452301; my $H1 = 0xefcdab89; my $H2 = 0x98badcfe; my $H3 = 0x10325476; # my $H0 = 19088743; # my $H1 = 2300300783; # my $H2 = 267242392; # my $H3 = 124076833; print "> "; my $msg = <>; my @msg = map(ord, split // , $msg); pop @msg; my $msgbin; $msgbin .= 0 . sprintf('%b',$msg[$_]) for 0..$#msg;; my $length = sprintf('%b',length $msgbin); $length = 0 . $length while length $length < 64; $msgbin .= 1; $msgbin .= 0 until length $msgbin == 448 % 512; $msgbin .= $length; my @w; for (my $i=0,my $j=0;$j<16;$i+=32,$j++) { $w[$j] = (unpack("V", pack("B32", substr("0" x 32 . (substr($msgbin +,$i,32)), -32)))) % (2**32); } my $a = $H0; my $b = $H1; my $c = $H2; my $d = $H3; my $f; my $g; for my $i (0..63) { if ($i >= 0 && $i <= 15) { $f = (($b & $c) | ((~$b) & $d)) % (2**32); $g = $i; } elsif ($i >= 16 && $i <= 31) { $f = (($d & $b) | ((~$d) & $c)) % (2**32); $g = (5*$i + 1) % 16; } elsif ($i >= 32 && $i <= 47) { $f = ($b ^ $c ^ $d) % (2**32); $g = (3*$i + 1) % 16; } elsif ($i >= 48 && $i <= 63) { $f = ($c ^ ($b | (~$d))) % (2**32); $g = (7*$i + 1) % 16; } my $temp = $d % (2**32); $d = $c % (2**32); $c = $b % (2**32); $b = ($b + (($a + $f + $k[$i] + $w[$g]) << $r[$i])) % (2**32); $a = $temp % (2**32); } $H0 = unpack("H8", pack("V", ($H0 + $a))); $H1 = unpack("H8", pack("V", ($H1 + $b))); $H2 = unpack("H8", pack("V", ($H2 + $c))); $H3 = unpack("H8", pack("V", ($H3 + $d))); my $digest = md5_hex($msg); print "> $H0$H1$H2$H3\n> $digest\n";
    but still doesn't work...
      my $H0 = 0x67452301; my $H1 = 0xefcdab89; my $H2 = 0x98badcfe; my $H3 = 0x10325476;
      i think error is there, this values must be other. Read http://www.faqs.org/rfcs/rfc1321.html

        From the the code in the RFC you cited:

        context->state[0] = 0x67452301; context->state[1] = 0xefcdab89; context->state[2] = 0x98badcfe; context->state[3] = 0x10325476;

        These numbers look pretty much identical to the ones in the Perl code in the OP to me.