MD5/Implementation: Difference between revisions

→‎{{header|Raku}}: use core serialization methods
(→‎{{header|Raku}}: use core serialization methods)
Line 4,816:
multi md5(Str $msg) { md5 $msg.encode }
multi md5(Blob $msg) {
sub rotl(uint32 \x, \n) returns uint32 { (x +< n) +| (x +> (32-n)) }
sub little-endian($w, $n, *@v) { (@v X+> flat ($w X* ^$n)) X% (2 ** $w) }
my \bits = 8 * $msg.elems;
my buf8 $buf .= new;
Blob.new: little-endian 8, 4,
$buf.write-uint32: $buf.elems, $_, LittleEndian for
|reduce -> Blob $blob, blob32 $X {
blob32.new: $blob Z+
reduce -> $b, $i {
blob32.new:
$b[3],
$b[1] + rotl(
$b[0]-> uint32 \x, \n { (x +< n) +| (BEGINx Array.new:+> (32-n)) }(
($b[0] + (BEGIN Array.new:
{ ($^x +& $^y) +| (+^$x +& $^z) },
{ ($^x +& $^z) +| ($^y +& +^$z) },
Line 4,833:
)[$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]],
) mod 2**32,
(BEGIN flat < 7 12 17 22 5 9 14 20 4 11 16 23 6 10 15 21 >.rotor(4) Xxx 4)[$i]
),
Line 4,842 ⟶ 4,843:
(BEGIN blob32.new: 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476),
|map { blob32.new: @$_ },
){
blob32.new(flat(@$msg, 0x80, 0x00 xx (-(bits div 8 + 1 + 8) % 64))
$^b.rotorpush(4$_).map({ :256[for (@^a.reverse]$msg, })0x80, little0x00 xx (-endian(32,bits 2,div bits8 + 1 + 8) % 64))
.flat.rotor(4).map({ :256[@^a.reverse] });
)
$b.write-uint64: $b.elems, bits, LittleEndian;
$b;
}(buf32.new)
.rotor(16);
$buf;
}
 
CHECK {
use Test;
1,934

edits