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

I'm trying to get a checksum for a string. In Digest::SHA, there are a few options I can use. I would like to use SHA2 for the checksum. Here is the link of the module:
http://search.cpan.org/~mshelor/Digest-SHA-5.48/lib/Digest/SHA.pm
Which one is the best one to use? Thanks!

Replies are listed 'Best First'.
Re: Digest::SHA options
by ikegami (Patriarch) on Jan 19, 2010 at 20:26 UTC
    It depends on how you define better. Security is always a compromise. In this case, increasing the hash size
    • increases security (good)
    • lowers the chance of a collision (good)
    • increases the storage requirements (bad)
    • increases the time requirements (bad)
    • increases the energy requirements (bad)
Re: Digest::SHA options
by zentara (Cardinal) on Jan 20, 2010 at 12:42 UTC
    See Would Like Recommendation for an SHA256 module

    If you need real protection of things, you have to encrypt them, not digest them.

    If your security needs are minimal, try this old code from Perl4 days.... its fairly fast as needs no compiler to install.

    #!/usr/bin/perl use strict; use warnings; my $shasum= &SHA("squeamish ossifrage\n"); print "Should be 82055066 4cf29679 2b38d164 7a4d8c0e 1966af57\n"; print "Returned: $shasum\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 no strict 'refs'; no strict 'vars'; no warnings; local $_; $temp = 'D9T4C`>_-JXF8NMS^$#)4=L/2X?!:@GF9;MGKH8\;O-S*8L\'6'; $m =4294967296; @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; }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku