MD4: Difference between revisions
m
→{{header|Wren}}: Minor tidy
(New post.) |
m (→{{header|Wren}}: Minor tidy) |
||
(8 intermediate revisions by 2 users not shown) | |||
Line 590:
engine_update(string_to_byte_vector(text), 0, text.length());
const int32_t buffer_index =
const int32_t padding_length = ( buffer_index < 56 ) ? 56 - buffer_index : 120 - buffer_index;
Line 597:
for ( int32_t i = 0; i < 8; ++i ) {
tail[padding_length + i] = static_cast<int8_t>(
}
Line 605:
for ( int32_t i = 0; i < 4; ++i ) {
for ( int32_t j = 0; j < 4; ++j ) {
result[i * 4 + j] = static_cast<int8_t>(
}
}
Line 614:
private:
void engine_update(const std::vector<int8_t>& message_bytes,
const int32_t& offset, const int32_t& message_length) {
Line 632 ⟶ 621:
}
int32_t buffer_index =
count += message_length;
const int32_t partial_length = BLOCK_LENGTH - buffer_index;
Line 696 ⟶ 685:
context[2] += c;
context[3] += d;
}
void engine_reset() {
count = 0;
context.assign(4, 0);
context[0] = 0x67452301;
context[1] = 0xefcdab89;
context[2] = 0x98badcfe;
context[3] = 0x10325476;
extra.assign(16, 0);
buffer.assign(BLOCK_LENGTH, 0);
}
Line 703:
std::transform(text.begin(), text.end(), std::back_inserter(bytes),
[](char ch){ return
return bytes;
Line 709:
int32_t unsigned_right_shift(const int32_t& base, const int32_t& shift) {
if (
return 0;
}
return ( base > 0 ) ? base >> shift : static_cast<uint32_t>(
}
Line 1,982:
(formerly Perl 6)
<syntaxhighlight lang="raku" line>sub md4($str) {
my
my $
$buf.push: 0x80;
$buf.push: 0 until ($buf - (448 div 8)) %% (512 div 8);
# raku serializes in little endian by default
$buf.write-uint64: $buf.elems, $buf-length*8;
my
# for some reason we have to type v here
-> uint32 $v, $s { $v +< $s +| $v +> (32 - $s) }
my uint32 ($a, $b, $c, $d) = 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476;
loop (my $pos = 0; $pos < $buf.elems; $pos+=64) {
my ($aa, $bb, $cc, $dd) = $a, $b, $c, $d;
for 0, 4, 8, 12 -> $i {
$a = r($a + f($b, $c, $d) + $buf.read-uint32($pos+($i+0)*4), 3);
$d = r($d + f($a, $b, $c) + $buf.read-uint32($pos+($i+1)*4), 7);
$c = r($c + f($d, $a, $b) + $buf.read-uint32($pos+($i+2)*4), 11);
}
$a = r($a + g($b, $c, $d) + $buf.read-uint32($pos+($i+0 )*4) + 0x5a827999, 3);
$d = r($d + g($a, $b, $c) + $buf.read-uint32($pos+($i+4 )*4) + 0x5a827999, 5);
$c = r($c + g($d, $a, $b) + $buf.read-uint32($pos+($i+8 )*4) + 0x5a827999, 9);
$b = r($b + g($c, $d, $a) + $buf.read-uint32($pos+($i+12)*4) + 0x5a827999, 13);
$a = r($a + h($b, $c, $d) + $buf.read-uint32($pos+($i+0 )*4) + 0x6ed9eba1, 3);
$d = r($d + h($a, $b, $c) + $buf.read-uint32($pos+($i+4 )*4) + 0x6ed9eba1, 9);
$c = r($c + h($d, $a, $b) + $buf.read-uint32($pos+($i+8 )*4) + 0x6ed9eba1, 11);
$b = r($b + h($c, $d, $a) + $buf.read-uint32($pos+($i+12)*4) + 0x6ed9eba1, 15);
}
reduce { $^buf.write-uint32: $buf.elems, $^x; $buf }, buf8.new, $a, $b, $c, $d;
}
CHECK {
use Test;
plan 1;
is md4('Rosetta Code').list.fmt('%02X'), 'A5 2B CF C6 A0 D0 D3 00 CD C5 DD BF BE FE 47 8B';
}</syntaxhighlight>
{{out}}
<pre>1..1
ok 1 -
</pre>
=={{header|Ruby}}==
Line 2,411 ⟶ 2,369:
{{trans|D}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="
var toBytes = Fn.new { |val|
|