Data update

This commit is contained in:
Ingy döt Net 2023-10-02 18:11:16 -07:00
parent 796d366b97
commit 35bcdeebf8
504 changed files with 7045 additions and 610 deletions

View file

@ -133,10 +133,10 @@ private:
}
int32_t unsigned_right_shift(const int32_t& base, const int32_t& shift) {
if ( base == 0 || shift >= 32 ) {
if ( shift < 0 || shift >= 32 || base == 0 ) {
return 0;
}
return ( base > 0 ) ? base >> shift : ( base + TWO_POWER_32 ) >> shift;
return ( base > 0 ) ? base >> shift : static_cast<uint32_t>(base) >> shift;
}
int32_t rotate(const int32_t& t, const int32_t& s) {
@ -164,7 +164,6 @@ private:
std::vector<int8_t> buffer;
const int32_t BLOCK_LENGTH = 64;
const int64_t TWO_POWER_32 = 4'294'967'296;
};
int main() {

View file

@ -1,92 +1,50 @@
sub md4($str) {
my @buf = $str.ords;
my $buflen = @buf.elems;
my buf8 $buf .= new: $str.encode;
my $buf-length = $buf.elems;
$buf.push: 0x80;
$buf.push: 0 until ($buf - (448 div 8)) %% (512 div 8);
$buf.write-uint64: $buf.elems, $buf-length*8, LittleEndian;
my \mask = (1 +< 32) - 1;
my &f = -> $x, $y, $z { ($x +& $y) +| ($x +^ mask) +& $z }
my &g = -> $x, $y, $z { ($x +& $y) +| ($x +& $z) +| ($y +& $z) }
my &h = -> $x, $y, $z { $x +^ $y +^ $z }
my &r = -> $v, $s { (($v +< $s) +& mask) +| (($v +& mask) +> (32 - $s)) }
my (&f, &g, &h, &r) =
{ $^x +& $^y +| +^$x +& $^z },
{ $^x +& $^y +| $x +& $^z +| $y +& $z },
{ $^x +^ $^y +^ $^z },
# for some reason we have to type v here
-> uint32 $v, $s { $v +< $s +| $v +> (32 - $s) }
sub pack-le (@a) {
gather for @a -> $a,$b,$c,$d { take $d +< 24 + $c +< 16 + $b +< 8 + $a }
my uint32 ($a, $b, $c, $d) = 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476;
for @$buf.rotor(64) {
my uint32 @x = .rotor(4).map: {:256[.reverse]}
my ($aa, $bb, $cc, $dd) = $a, $b, $c, $d;
for 0, 4, 8, 12 -> $i {
$a = r($a + f($b, $c, $d) + @x[ $i+0 ], 3);
$d = r($d + f($a, $b, $c) + @x[ $i+1 ], 7);
$c = r($c + f($d, $a, $b) + @x[ $i+2 ], 11);
$b = r($b + f($c, $d, $a) + @x[ $i+3 ], 19);
}
for 0, 1, 2, 3 -> $i {
$a = r($a + g($b, $c, $d) + @x[ $i+0 ] + 0x5a827999, 3);
$d = r($d + g($a, $b, $c) + @x[ $i+4 ] + 0x5a827999, 5);
$c = r($c + g($d, $a, $b) + @x[ $i+8 ] + 0x5a827999, 9);
$b = r($b + g($c, $d, $a) + @x[ $i+12] + 0x5a827999, 13);
}
for 0, 2, 1, 3 -> $i {
$a = r($a + h($b, $c, $d) + @x[ $i+0 ] + 0x6ed9eba1, 3);
$d = r($d + h($a, $b, $c) + @x[ $i+8 ] + 0x6ed9eba1, 9);
$c = r($c + h($d, $a, $b) + @x[ $i+4 ] + 0x6ed9eba1, 11);
$b = r($b + h($c, $d, $a) + @x[ $i+12] + 0x6ed9eba1, 15);
}
($a,$b,$c,$d) Z[+=] ($aa,$bb,$cc,$dd);
}
my ($a, $b, $c, $d) = 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476;
my $term = False;
my $last = False;
my $off = 0;
repeat until $last {
my @block = @buf[$off..$off+63]:v; $off += 64;
my @x;
given +@block {
when 64 {
@x = pack-le @block;
}
when 56..63 {
$term = True;
@block.push(0x80);
@block.push(slip 0 xx 63 - $_);
@x = pack-le @block;
}
when 0..55 {
@block.push($term ?? 0 !! 0x80);
@block.push(slip 0 xx 55 - $_);
@x = pack-le @block;
my $bit_len = $buflen +< 3;
@x.push: $bit_len +& mask, $bit_len +> 32;
$last = True;
}
default {
die "oops";
}
}
my ($aa, $bb, $cc, $dd) = $a, $b, $c, $d;
for 0, 4, 8, 12 -> \i {
$a = r($a + f($b, $c, $d) + @x[ i+0 ], 3);
$d = r($d + f($a, $b, $c) + @x[ i+1 ], 7);
$c = r($c + f($d, $a, $b) + @x[ i+2 ], 11);
$b = r($b + f($c, $d, $a) + @x[ i+3 ], 19);
}
for 0, 1, 2, 3 -> \i {
$a = r($a + g($b, $c, $d) + @x[ i+0 ] + 0x5a827999, 3);
$d = r($d + g($a, $b, $c) + @x[ i+4 ] + 0x5a827999, 5);
$c = r($c + g($d, $a, $b) + @x[ i+8 ] + 0x5a827999, 9);
$b = r($b + g($c, $d, $a) + @x[ i+12] + 0x5a827999, 13);
}
for 0, 2, 1, 3 -> \i {
$a = r($a + h($b, $c, $d) + @x[ i+0 ] + 0x6ed9eba1, 3);
$d = r($d + h($a, $b, $c) + @x[ i+8 ] + 0x6ed9eba1, 9);
$c = r($c + h($d, $a, $b) + @x[ i+4 ] + 0x6ed9eba1, 11);
$b = r($b + h($c, $d, $a) + @x[ i+12] + 0x6ed9eba1, 15);
}
$a = ($a + $aa) +& mask;
$b = ($b + $bb) +& mask;
$c = ($c + $cc) +& mask;
$d = ($d + $dd) +& mask;
}
sub b2l($n is copy) {
my $x = 0;
for ^4 {
$x +<= 8;
$x += $n +& 0xff;
$n +>= 8;
}
$x;
}
b2l($a) +< 96 +
b2l($b) +< 64 +
b2l($c) +< 32 +
b2l($d);
my buf8 $abcd .= new;
for $a, $b, $c, $d { $abcd.write-uint32: 4*$++, $_, LittleEndian }
blob8.new: $abcd;
}
sub MAIN {
my $str = 'Rosetta Code';
say md4($str).base(16).lc;
say md4($str);
}