MD5/Implementation: Difference between revisions

→‎{{header|Raku}}: put tests in a CHECK block
m (→‎{{header|Raku}}: minor style fixes)
(→‎{{header|Raku}}: put tests in a CHECK block)
Line 4,706:
constant @S = flat < 7 12 17 22 5 9 14 20 4 11 16 23 6 10 15 21 >.rotor(4) X[xx] 4;
constant $T = (floor(abs(sin($_ + 1)) * 2**32) for ^64);
constant @k = flat ( $_ for ^16),
((5*$_ + 1) % 16 for ^16),
((3*$_ + 5) % 16 for ^16),
Line 4,713:
sub little-endian($w, $n, *@v) { (@v X+> flat ($w X* ^$n)) X% (2 ** $w) }
 
sub md5-pad(Blobblob8 $msg)
{
my $bits = 8 * $msg.elems;
Line 4,727:
sub md5-block(blob32 $H where $H == 4, blob32 $X where $X == 16)
{
blob32.new: $H[] Z+ md5.raku
reduce -> blob32 $b, $i {
blob32.new:
$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;
}
Line 4,739:
sub md5(Blob $msg --> blob8)
{
blob8.new: little-endian 8, 4, |
|reduce &md5-block,
(constant $ = blob32.new: 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476),
|md5-pad $msg;
}
CHECK {
use Test;
plan 7;
 
for 'd41d8cd98f00b204e9800998ecf8427e', '',
'0cc175b9c0f1b6a831c399e269772661', 'a',
'900150983cd24fb0d6963f7d28e17f72', 'abc',
'f96b697d7cb7938d525a2f31aaf161d0', 'message digest',
'c3fcd3d76192e4007dfb496cca67e13b', 'abcdefghijklmnopqrstuvwxyz',
'd174ab98d277d9f5a5611c2c9f419d9f', 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
'57edf4a22be3c955ac49da2e2107b67a', '12345678901234567890123456789012345678901234567890123456789012345678901234567890'
-> $expected, $msg {
my $digest = md5($msg.encode).list».fmt('%02x').join;
is($digest, $expected, "$digest is MD5 digest of '$msg'");
}
}</syntaxhighlight>
 
1,934

edits