Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^3: MD5 -- not digest::md5

by Anonymous Monk
on Aug 15, 2009 at 10:52 UTC ( #788857=note: print w/ replies, xml ) Need Help??


in reply to Re^2: MD5 -- not digest::md5
in thread MD5 -- not digest::md5

Heres how i modify

#!/usr/bin/perl use strict; use warnings; use diagnostics; 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; use integer; my $H0 = 0x67452301; my $H1 = 0xefcdab89; my $H2 = 0x98badcfe; my $H3 = 0x10325476; print "> "; #~ my @msg = map(ord, split // , <>); #~ pop @msg; my @msg = map ord, split //, 'test'; # perl -e"binmode STDOUT; print 'test'" >test && md5sum test # 098f6bcd4621d373cade4e832627b4f6 *test # md5("test) = 098f6bcd4621d373cade4e832627b4f6 # myscript("test") = 56fb159ad06a6e220f579f97e5b2170f 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("N", 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("N", ($H0 + $a))); $H1 = unpack("H8", pack("N", ($H1 + $b))); $H2 = unpack("H8", pack("N", ($H2 + $c))); $H3 = unpack("H8", pack("N", ($H3 + $d))); my $digest = $H0.$H1.$H2.$H3; print "> $digest\n"; __END__
On my machine without use integer; sum is fffffffffffffffffffffffffc51043f but with use integer;, sum is 67452301efcdab8998badcfe10325476

I think you need to unpack more carefully

N An unsigned long in "network" (big-endian) order.
Pseudocode mentions little endian, so i think you want
V An unsigned long in "VAX" (little-endian) order.
Hope that helps


Comment on Re^3: MD5 -- not digest::md5
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://788857]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (18)
As of 2015-07-30 20:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (273 votes), past polls