MD5/Implementation: Difference between revisions
→{{header|Raku}}: compact
(→{{header|Raku}}: compact) |
|||
Line 4,814:
(formerly Perl 6)
{{works with|rakudo|2022-07}}
<syntaxhighlight lang="raku" line>proto md5($msg)
multi md5(Str $
multi md5(Blob $msg) {
sub rotl(uint32
constant FGHI = { ($^a +& $^b) +| (+^$a +& $^c) },▼
{ ($^a +& $^c) +| ($^b +& +^$c) },▼
{ $^a +^ $^b +^ $^c },▼
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 little-endian($w, $n, *@v) { (@v X+> flat ($w X* ^$n)) X% (2 ** $w) }
▲ my $bits = 8 * $msg.elems;
reduce -> $b, $i {
(flat $msg.list, 0x80, 0x00 xx -($bits div 8 + 1 + 8) % 64)▼
$b[1] + rotl(
▲ little-endian(32, 2, $bits)
$b[0] + (BEGIN Array.new:
.rotor(16)▼
)[$i div 16](|$b[1..3]) +
(BEGIN blob32.new: map &floor ∘ * * 2**32 ∘ &abs ∘ &sin ∘ * + 1, ^64)[$i] +
$X[(BEGIN Blob.new: 16 X[R%] flat ($++, 5*$++ + 1, 3*$++ + 5, 7*$++) Xxx 16)[$i]],
▲ blob32.new: $H[] Z+
▲
▲ reduce -> blob32 $b, $i {
▲ blob32.new:
$b[2]
}, $blob, |^64;
▲ }, $H, |^64;
|map { blob32.new: @$_ },
▲ (constant $ = blob32.new: 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476),
}
CHECK {
use Test;
for 'd41d8cd98f00b204e9800998ecf8427e', '',
'0cc175b9c0f1b6a831c399e269772661', 'a',
'900150983cd24fb0d6963f7d28e17f72', 'abc',
'f96b697d7cb7938d525a2f31aaf161d0', 'message digest',
Line 4,878 ⟶ 4,863:
is($digest, $expected, "$digest is MD5 digest of '$msg'");
}
done-testing;
}</syntaxhighlight>
|