MD5/Implementation: Difference between revisions

Content deleted Content added
Grondilu (talk | contribs)
Line 2,218: Line 2,218:


=={{header|Perl 6}}==
=={{header|Perl 6}}==
{{works with|rakudo|2015-09-22}}
{{broken}}

<lang perl6>use Test;
<lang perl6>use Test;

sub prefix:<¬>(\x) { (+^ x) % 2**32 }
sub prefix:<¬>(\x) { (+^ x) % 2**32 }
sub infix:<⊞>(\x, \y) { (x + y) % 2**32 }
sub infix:<⊞>(\x, \y) { (x + y) % 2**32 }
sub infix:«<<<»(\x, \n) { (x +< n) % 2**32 +| (x +> (32-n)) }
sub infix:«<<<»(\x, \n) { (x +< n) % 2**32 +| (x +> (32-n)) }

constant FGHI = -> \X, \Y, \Z { (X +& Y) +| (¬X +& Z) },
constant FGHI = -> \X, \Y, \Z { (X +& Y) +| (¬X +& Z) },
-> \X, \Y, \Z { (X +& Z) +| (Y +& ¬Z) },
-> \X, \Y, \Z { (X +& Z) +| (Y +& ¬Z) },
-> \X, \Y, \Z { X +^ Y +^ Z },
-> \X, \Y, \Z { X +^ Y +^ Z },
-> \X, \Y, \Z { Y +^ (X +| ¬Z) };
-> \X, \Y, \Z { Y +^ (X +| ¬Z) };

constant S = (7, 12, 17, 22) xx 4,
constant S = flat (7, 12, 17, 22) xx 4,
(5, 9, 14, 20) xx 4,
(5, 9, 14, 20) xx 4,
(4, 11, 16, 23) xx 4,
(4, 11, 16, 23) xx 4,
(6, 10, 15, 21) xx 4;
(6, 10, 15, 21) xx 4;

constant T = (floor(abs(sin($_ + 1)) * 2**32) for ^64);
constant T = (floor(abs(sin($_ + 1)) * 2**32) for ^64);

constant k = ( $_ for ^16),
constant k = flat ( $_ for ^16),
((5*$_ + 1) % 16 for ^16),
((5*$_ + 1) % 16 for ^16),
((3*$_ + 5) % 16 for ^16),
((3*$_ + 5) % 16 for ^16),
((7*$_ ) % 16 for ^16);
((7*$_ ) % 16 for ^16);

sub little-endian($w, $n, *@v) { (@v X+> ($w X* ^$n)) X% (2 ** $w) }
sub little-endian($w, $n, *@v) {
my \step1 = ($w X* ^$n).eager; # temporary bug workaround

my \step2 = (@v X+> step1);
step2 X% (2 ** $w);
}
sub md5-pad(Blob $msg)
sub md5-pad(Blob $msg)
{
{
my \bits = 8 * $msg.elems;
my \bits = 8 * $msg.elems;
my @padded = $msg.list, 0x80, 0x00 xx (-(bits div 8 + 1 + 8) % 64);
my @padded = flat $msg.list, 0x80, 0x00 xx (-(bits div 8 + 1 + 8) % 64);
@padded.map({ :256[$^d,$^c,$^b,$^a] }), little-endian(32, 2, bits);
flat @padded.map({ :256[$^d,$^c,$^b,$^a] }), little-endian(32, 2, bits);
}
}

sub md5-block(@H is rw, @X)
sub md5-block(@H is rw, @X)
{
{
Line 2,262: Line 2,265:
@H «⊞=» ($A, $B, $C, $D);
@H «⊞=» ($A, $B, $C, $D);
}
}

sub md5(Blob $msg --> Blob)
sub md5(Blob $msg --> Blob)
{
{
Line 2,270: Line 2,273:
Blob.new: little-endian(8, 4, @H);
Blob.new: little-endian(8, 4, @H);
}
}

for 'd41d8cd98f00b204e9800998ecf8427e', '',
for 'd41d8cd98f00b204e9800998ecf8427e', '',
'0cc175b9c0f1b6a831c399e269772661', 'a',
'0cc175b9c0f1b6a831c399e269772661', 'a',