Re: Perl MD5 and MIME/radix 64 encoding

New Message Reply About this list Date view Thread view Subject view Author view

Adam Back (aba@dcs.ex.ac.uk)
Sat, 6 Jun 1998 01:12:00 +0100


MD5:

#!/bin/perl -iH9T4C`>_-JXF8NMS^$#)4=@<,$18%"0X4!`L0%P8*#Q4``04``04#!P`` ~JLA
@A=unpack N4C24,unpack u,$^I;@K=map{int abs 2**32*sin$_}1..64;sub L{($x=pop)
<<($n=pop)|2**$n-1&$x>>32-$n}sub M{($x=pop)-($m=1+~0)*int$x/$m}do{$l+=$r=read
STDIN,$_,64;$r++,$_.="\x80"if$r<64&&!$p++;@W=unpack V16,$_."\0"x7;$W[14]=$l*8
if$r<57;($a,$b,$c,$d)=@A;for(0..63){$a=M$b+L$A[4+4*($_>>4)+$_%4],M&{(sub{$b&$c
|$d&~$b},sub{$b&$d|$c&~$d},sub{$b^$c^$d},sub{$c^($b|~$d)})[$z=$_/16]}+$W[($A[
20+$z]+$A[24+$z]*($_%16))%16]+$K[$_]+$a;($a,$b,$c,$d)=($d,$a,$b,$c)}$v=a;for(
@A[0..3]){$_=M$_+${$v++}}}while$r>56;print unpack H32,pack V4,@A # RSA's MD5

SHA1:

#!/bin/perl -iD9T4C`>_-JXF8NMS^$#)4=L/2X?!:@GF9;MGKH8\;O-S*8L'6 -SHA in perl5
@A=unpack"N*",unpack u,$^I;@K=@A=unpack"N*",unpack u,$^I;@K=splice@A,5,4;sub M{($x=pop)-($m=1+~0)*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{$l+=$r=read STDIN,$_,64;$r++,$_.="\x80"if$r<
64&&!$p++;@W=unpack N16,$_."\0"x7;$W[15]=$l*8 if$r<57;for(16..79){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

both by John Allen <allen@grumman.com>

If you convert them into functions, I wouldn't mind a copy.

btw. -i<splurge> is the same as $^I = "<splurge>".

You can do radix 64 with minor tweaks of the pack/unpack uuencode
operator. Mark Shoulsen wrote an radix 64 decoder in under 1 line of
code doing this, it was pack, and a y/s1/s1/ string to fix the input,
and some glue. Unfortunately I lost it and he doesn't have it either
anymore.

I do have this version lying around which can armor, unarmor, and
optionally cope with PGP, seems to work ok.

======================================================================
#!/usr/local/bin/perl -s

# PGP armorer-dearmorer, by Mark E. Shoulson (shoulson@cs.columbia.edu).
# Still unfinished. Use -u to unarmor, -f to add a frame if armoring.

$linebuf='';
sub fakeread {
    my ($len,$retval,$tlen);

    ($len)=@_;
    $retval='';
    while ($len>0) {
        unless ($linebuf) {
            $linebuf=<STDIN>;
            chomp $linebuf;
        }
        #print $linebuf;
        $retval.=substr($linebuf,0,$len);
        $tlen=$len;
        $len-=length($linebuf);
        $linebuf=substr($linebuf,$tlen);
        last if eof STDIN;
    }
    return $retval;
}

if (!$u) {
    print "-----BEGIN PGP MESSAGE-----\nArmor: arm.pl\n\n" if ($f);
    while (read(STDIN,$_,3)) {
        $_=unpack('B*',$_);
        $_.='0'x((6-length($_)%6)%6);
        s/(......)/00\1/g;
        $_=pack('B*',$_);
        tr|\0-\077|A-Za-z0-9+/|;
        $_.='='x(4-length($_));
        print $_; $linelen+=4;
        if ($linelen>=64) { print "\n"; $linelen=0;}
    }
    print "\n";
    print "-----END PGP MESSAGE-----\n" if $f;
}
else {
    # Search for and eat frame.
    $_=<STDIN>;
    chomp $_;
    if (/^-/) {
        $_=<STDIN> until /^\s*\n?$/;
    }
    else {
        $linebuf=$_;
    }
    while (1) {
        $last=0;
        $_=&fakeread(4);
        # equals signs are used to pad out the end.
        s/^([^-=]*)[-=].*/\1/;
        # Only last chunk may be less than 4chars. This may not always be
        # able to exclude the checksum. Oh well.
        $last=1 if length($_)<4;
        tr|A-Za-z0-9+/|\0-\077|;
        $_=unpack('B*',$_);
        s/00(......)/\1/g;
        $_=pack('B*',$_);
        $_=~s/\0$// if $last; # Strip *one* trailing null
        print $_;
        last if $last;
    }
}
======================================================================

There are a few other perl hacks at:

        http://www.dcs.ex.ac.uk/~aba/rsa/

Adam


New Message Reply About this list Date view Thread view Subject view Author view

 
All trademarks and copyrights are the property of their respective owners.

Other Directory Sites: SeekWonder | Directory Owners Forum

The following archive was created by hippie-mail 7.98617-22 on Fri Aug 21 1998 - 17:18:24 ADT