MD5/Implementation: Difference between revisions
→{{header|Raku}}: modernize and fix
(→{{header|Raku}}: add broken tag) |
(→{{header|Raku}}: modernize and fix) |
||
Line 4,696:
=={{header|Raku}}==
(formerly Perl 6)
{{works with|rakudo|
<syntaxhighlight lang="raku" line>sub
▲<syntaxhighlight lang="raku" line>sub infix:<⊞>(uint32 $a, uint32 $b --> uint32) { ($a + $b) +& 0xffffffff }
constant FGHI = { ($^a +& $^b) +| (+^$a +& $^c) },
{ ($^a +& $^c) +| ($^b +& +^$c) },
{ $^a +^ $^b +^ $^c
{ $^b +^ ($^a +| +^$^c)
constant
constant $T = (floor(abs(sin($_ + 1)) * 2**32) for ^64);▼
constant @k = flat ( $_ for
▲constant T = (floor(abs(sin($_ + 1)) * 2**32) for ^64);
((5*$_ + 1) % 16 for ^16),
((3*$_ + 5) % 16 for ^16),
((7*$_ ) % 16 for ^16);
sub little-endian($w, $n, *@v) { (@v X+> flat ($w X* ^$n)) X% (2 ** $w) }
sub md5-pad(Blob $msg)
{
my $bits = 8 * $msg.elems;
(
).flat
.rotor(16)
.map({ blob32.new: @$_ })
}
sub md5-block(
{
blob32.new: $H[] Z+md5.raku
reduce -> blob32 $b, $i {
$b[3],
($b[1] + rotl($b[0] + FGHI[$i div 16](|$b[1,2,3]) + $T[$i] + $X[@k[$i]], @S[$i])),
$b[1],
$b[2]
}, $H, |^64;
}
sub md5(Blob $msg -->
{
blob8.new: little-endian 8, 4, |
my uint32 @M = md5-pad($msg);▼
reduce &md5-block,
▲ Blob.new: little-endian(8, 4, @H);
}
Line 4,757 ⟶ 4,756:
'57edf4a22be3c955ac49da2e2107b67a', '12345678901234567890123456789012345678901234567890123456789012345678901234567890'
-> $expected, $msg {
my $digest = md5($msg.encode
is($digest, $expected, "$digest is MD5 digest of '$msg'");
}</syntaxhighlight>
|