SHA-256: Difference between revisions

→‎{{header|Perl 6}}: using Blob instead of Buf
(→‎{{header|Racket}}: More compact code)
(→‎{{header|Perl 6}}: using Blob instead of Buf)
Line 365:
 
<lang Perl 6>say .list».fmt("%02x").join given sha256 "Rosetta code";
 
constant primes = grep &is-prime, 2 .. *;
sub init(&f) {
map { my $f = $^p.&f; (($f - $f.Int)*2**32).Int }, primes
}
 
sub infix:<m+> { ($^a + $^b) % 2**32 }
sub rotr($n, $b) { $n +> $b +| $n +< (32 - $b) }
 
proto sha256($) returns BufBlob {*}
multi sha256(Str $str where all($str.ords) < 128) {
sha256 $str.encode: 'ascii'
}
multi sha256(BufBlob $data) {
constant K = init(* **(1/3))[^64];
my $l = 8 * my @b = $data.list;
push @b, 0x80; push @b, 0 until (8*@b-448) %% 512;
 
push @b, reverse gather for ^8 { take $l%256; $l div=256 }
my @word = :256[@b.shift xx 4] xx @b/4;
 
my @H = init(&sqrt)[^8];
my @w;
Line 407:
@H = @H Z[m+] @h;
}
return BufBlob.new: map -> $word is rw {
reverse gather for ^4 { take $word % 256; $word div= 256 }
}, @H;
1,934

edits