rimmern has asked for the wisdom of the Perl Monks concerning the following question:

I know there are modules to hash a file using SHA-1 or MD5 but I am looking for a pure perl version (instead of say Digest::MD5 that uses .xs).


I always seem to have problems with modules that use .xs so I would like to use a module (or just an example script) in pure perl.


I realise a pure perl version will be slower but it would stop me swearing at nmake so much ;-)


Thanks, Digsy

Replies are listed 'Best First'.
Re: Pure perl version of SHA-1 ? (or MD5)
by Joost (Canon) on Jul 09, 2002 at 12:45 UTC
    A quick CPAN search turns up Digest::Perl::MD5:
    NAME Digest::MD5::Perl - Perl implementation of Ron Rivests MD5 Algorithm DISCLAIMER This is not an interface (like `Digest::MD5') but a Perl implementation of MD5. It is written in perl only and because of this it is slow but it works without C-Code.

    Hope this helps :-)

    -- Joost downtime n. The period during which a system is error-free and immune from user input.
Re: Pure perl version of SHA-1 ? (or MD5)
by derby (Abbot) on Jul 09, 2002 at 13:51 UTC
    I've done SHA-1 before in pure perl (and it was slow). Unfortunately, I don't have access to the code anymore (nor the time right now to re-do it). All I really did was follow the standard (and use it's test cases) which can be found on nist's website. Trying to implement the standard my make you swear just as much but at least you'll have a greater reward at the end.

    -derby

Re: Pure perl version of SHA-1 ? (or MD5)
by zentara (Cardinal) on Jul 09, 2002 at 18:11 UTC
    Sorry, my earlier post had an error. How could that be?
    Here's one that works well.
    #!/usr/bin/perl $s = &SHA("squeamish ossifrage\n"); print "$s\n"; print '82055066 4cf29679 2b38d164 7a4d8c0e 1966af57',"\n"; #------------------------------------------------------------------# sub SHA { ### This algorithm is based on the implementation of SHA ### written by: John Allen (allen@grumman.com). ### &SHA("squeamish ossifrage\n"); ### Should return 82055066 4cf29679 2b38d164 7a4d8c0e 1966af57 my ($msg, $p, $l) = @_; #$p=0; $l=0 local $_; $temp = 'D9T4C`>_-JXF8NMS^$#)4=L/2X?!:@GF9;MGKH8\;O-S*8L\'6'; $m = 4294967296; ###$m=1+~0; @A=unpack"N*",unpack u,$temp; @K=splice@A,5,4; sub M{($x=pop)-($m)*int$x/$m}; sub L{$n=pop;($x=pop)<<$n|2**$n-1&$x>>32-$n} @F=(sub{$b&($c^$d)^$d},$S=sub{$b^$c^$d},sub{($b|$c)&$d|$b&$c},$S); do{ $msg=~s/.{0,64}//s;$_=$&; $l+=$r=length; $r++,$_.="\x80"if$r<64&&!$p++;@W=unpack N16,$_."\0"x7;$W[15]=$l*8 if$r<57; for(16..79){push@W,L$W[$_ -3]^$W[$_-8]^$W[$_-14]^$W[$_-16],1}($a,$b,$c,$d,$e)=@A; for(0..79){$t=M&{$F[$_/ 20]}+$e+$W[$_]+$K[$_/20]+L$a,5; $e=$d; $d=$c; $c=L$b,30; $b=$a; $a=$t}$v='a'; @A=map{ M$_+${$v++}}@A }while$r>56; return sprintf'%8x 'x4 . '%8x',@A; }