MD5/Implementation: Difference between revisions
→{{header|Raku}}: create proto and put all subs in the main candidate
(→{{header|Raku}}: put tests in a CHECK block) |
(→{{header|Raku}}: create proto and put all subs in the main candidate) |
||
Line 4,697:
(formerly Perl 6)
{{works with|rakudo|2022-07}}
<syntaxhighlight lang="raku" line>
multi md5(Str $str) { samewith $str.encode }
constant FGHI = { ($^a +& $^b) +| (+^$a +& $^c) },▼
sub rotl(uint32 $a, UInt $n -->
constant FGHI
constant @k = flat ( $_ for ^16),▼
((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) }▼
constant @S = flat < 7 12 17 22 5 9 14 20 4 11 16 23 6 10 15 21 >.rotor(4) X[xx] 4;
sub md5-pad(blob8 $msg)▼
constant @T = ^64 .map: { floor(abs(sin($_ + 1)) * 2**32) }
▲ sub little-endian($w, $n, *@v) { (@v X+> flat ($w X* ^$n)) X% (2 ** $w) }
▲ sub md5-pad(blob8 $msg)
{
my $bits = 8 * $msg.elems;
(
).flat
.rotor(16)
.map({ blob32.new: @$_ })
}
sub md5-block(blob32 $H where
{
blob32.new: $H[] Z+
reduce -> blob32 $b, $i {
}, $H, |^64;
}
▲sub md5(Blob $msg --> blob8)
▲ blob8.new: little-endian 8, 4, |
reduce &md5-block,
}
Line 4,757 ⟶ 4,758:
'57edf4a22be3c955ac49da2e2107b67a', '12345678901234567890123456789012345678901234567890123456789012345678901234567890'
-> $expected, $msg {
my $digest = md5($msg
is($digest, $expected, "$digest is MD5 digest of '$msg'");
}
|