Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Why applying MD5 hash twice?

by wufnik (Friar)
on Sep 09, 2003 at 16:27 UTC ( [id://290089]=note: print w/replies, xml ) Need Help??


in reply to Why applying MD5 hash twice?

some quick notes re: md5

dobbertin 1996 was the first, as far as i know, to show common values for the *compression algorithm* inside md5. RFC1828, security considerations section, gives a high level view of this.

in essence this would allow an attacker to exploit under *very* stringent (read peculiar) conditions digital signatures of files, for which md5 is most commonly used. still, Schneier counterpane does not seem to like it.

so yes, you might want to md5 twice. but - the general consensus is that SHA is more secure. here is a (yet another) wee gem from Adam Back's cypherspace site which computes it, instead.

#!/usr/bin/perl -iD9T4C`>_-JXF8NMS^$#)4=L/2X?!:@GF9;MGKH8\;O-S*8L'6 @A=unpack"N*",unpack u,$^I;@K=splice@A,5,4;sub M{($x=pop)-($m=1+~0)*in +t$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{$l+=$r=read STDIN,$_,64;$r++,$_.="\x +80"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;printf'%.8x'x5 ."\n",@A
hope that helps,

wufnik

...in the world of the mules there are no rules

Replies are listed 'Best First'.
Re: Re: Why applying MD5 hash twice?
by jryan (Vicar) on Sep 09, 2003 at 21:10 UTC

    Please don't ever use code like this, for several reasons:

    1. The code is obfuscated, and therefore the only point of it is to display the author's cleverness. It was not meant to be used as a library.
    2. Chances are, you don't understand it on a mere glance. Using code you don't understand can lead to "Cargo Cult Programming", which is a dangerous habit to fall into.
    3. Worse, since the code is (intentionally) difficult to read, it probably hasn't had any peer review. This code could have bugs or security holes. Encryption code that hasn't had some kind of intense peer evaluation should *NEVER EVER* be used.

    If you want to use the SHA algorithm, please use Digest::SHA2 or Digest::SHA1.

      I agree with what your saying, but to play devils advocate, 2. could apply to a lot of people who use CPAN.

      -Lee

      "To be civilized is to deny one's nature."

        Yeah, you're right. Luckily, the author is Gisle Aas, and that's an author I trust. :)

      I would just like to make sure we are all clear that md5 is NOT an encryption algorithm, it is a hashing algorithm, and yes there is a big difference. But I do agree with you that encryption algorithms that have not had years of review should never be used.
      my purpose in posting the code was mainly to draw attention to what i considered an ingenious piece of code, and possibly also to the cypherspace site.

      the code would not be appropriate for use in another script mostly because of it's obfuscated nature, but also because of the perl command line args that need to be used. apologies if this was unclear.

      Given this, I should say, the goal here is obviously art, sadly not mine. While i would not use the 3 line perl/bc RSA in anger, or the above SHA, or MD5 in 8 lines, more directly relevant, i still find them all a powerful demonstration of perl's beauty.

      thus the inclusion.

      ...wufnik

      -- in the world of the mules there are no rules --
Re: Re: Why applying MD5 hash twice?
by gwadej (Chaplain) on Sep 09, 2003 at 23:42 UTC

    I'd need to dig through through my notes to be sure, but I used a SHA very much like this years ago. (I had to support Perl 4 installations and could not require any modules.<shrug/>)

    Unfortunately, the one I used had a bug when dealing with long strings. At a glance, I couldn't tell if this one has the problem or not. In that case, I worked with the author of the code to fix the bug and we did use it for years.

    However, as mentioned by others, using the CPAN modules would be a much better idea. Believe me, you do not want to try to find a bug in an implementation of a cryptographic hash algorithm.

    G. Wade

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-25 06:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found