Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
12
Task/SHA-256/Haskell/sha-256.hs
Normal file
12
Task/SHA-256/Haskell/sha-256.hs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import Data.Char (ord)
|
||||
import Crypto.Hash.SHA256 (hash)
|
||||
import Data.ByteString (unpack, pack)
|
||||
import Text.Printf (printf)
|
||||
|
||||
main = putStrLn $ -- output to terminal
|
||||
concatMap (printf "%02x") $ -- to hex string
|
||||
unpack $ -- to array of Word8
|
||||
hash $ -- SHA-256 hash to ByteString
|
||||
pack $ -- to ByteString
|
||||
map (fromIntegral.ord) -- to array of Word8
|
||||
"Rosetta code"
|
||||
13
Task/SHA-256/Julia/sha-256.julia
Normal file
13
Task/SHA-256/Julia/sha-256.julia
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
clear = "Rosetta code"
|
||||
standard = "764faf5c61ac315f1497f9dfa542713965b785e5cc2f707d6468d7d1124cdfcf"
|
||||
|
||||
using SHA
|
||||
|
||||
crypt = sha256(clear)
|
||||
|
||||
println("Testing Julia's SHA-256:")
|
||||
if crypt == standard
|
||||
println(" OK, \"", clear, "\" => ", crypt)
|
||||
else
|
||||
println("The hash does not match the standard value.")
|
||||
end
|
||||
18
Task/SHA-256/Oberon-2/sha-256.oberon-2
Normal file
18
Task/SHA-256/Oberon-2/sha-256.oberon-2
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
MODULE SHA256;
|
||||
IMPORT
|
||||
Crypto:SHA256,
|
||||
Crypto:Utils,
|
||||
Strings,
|
||||
Out;
|
||||
VAR
|
||||
h: SHA256.Hash;
|
||||
str: ARRAY 128 OF CHAR;
|
||||
|
||||
BEGIN
|
||||
h := SHA256.NewHash();
|
||||
h.Initialize;
|
||||
str := "Rosetta code";
|
||||
h.Update(str,0,Strings.Length(str));
|
||||
h.GetHash(str,0);
|
||||
Out.String("SHA256: ");Utils.PrintHex(str,0,h.size);Out.Ln
|
||||
END SHA256.
|
||||
1
Task/SHA-256/PARI-GP/sha-256.pari
Normal file
1
Task/SHA-256/PARI-GP/sha-256.pari
Normal file
|
|
@ -0,0 +1 @@
|
|||
sha256(s)=extern("echo \"Str(`echo -n '"Str(s)"'|sha256sum|cut -d' ' -f1`)\"")
|
||||
|
|
@ -1,8 +1,8 @@
|
|||
say sha256 "Rosetta code";
|
||||
|
||||
constant primes = grep &is-prime, 2 .. *;
|
||||
sub init(&f) {
|
||||
map { my $f = $^p.&f; (($f - $f.Int)*2**32).Int }, primes
|
||||
map { my $f = $^p.&f; (($f - $f.Int)*2**32).Int },
|
||||
state @ = grep *.is-prime, 2 .. *;
|
||||
}
|
||||
|
||||
sub infix:<m+> { ($^a + $^b) % 2**32 }
|
||||
|
|
@ -14,7 +14,7 @@ multi sha256(Str $str where all($str.ords) < 128) {
|
|||
}
|
||||
multi sha256(Blob $data) {
|
||||
constant K = init(* **(1/3))[^64];
|
||||
my @b = $data.list, 0x80;
|
||||
my @b = flat $data.list, 0x80;
|
||||
push @b, 0 until (8 * @b - 448) %% 512;
|
||||
push @b, reverse (8 * $data).polymod(256 xx 7);
|
||||
my @word = :256[@b.shift xx 4] xx @b/4;
|
||||
|
|
@ -36,9 +36,9 @@ multi sha256(Blob $data) {
|
|||
my $σ1 = [+^] map { rotr @h[4], $_ }, 6, 11, 25;
|
||||
my $t1 = [m+] @h[7], $σ1, $ch, K[$j], @w[$j];
|
||||
my $t2 = $σ0 m+ $maj;
|
||||
@h = $t1 m+ $t2, @h[^3], @h[3] m+ $t1, @h[4..6];
|
||||
@h = flat $t1 m+ $t2, @h[^3], @h[3] m+ $t1, @h[4..6];
|
||||
}
|
||||
@H = @H Z[m+] @h;
|
||||
@H [Z[m+]]= @h;
|
||||
}
|
||||
return Blob.new: map { reverse .polymod(256 xx 3) }, @H;
|
||||
}
|
||||
|
|
|
|||
139
Task/SHA-256/Perl/sha-256-2.pl
Normal file
139
Task/SHA-256/Perl/sha-256-2.pl
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
package Digest::SHA256::PP;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use constant WORD => 2**32;
|
||||
use constant MASK => WORD - 1;
|
||||
|
||||
my @h;
|
||||
my @k;
|
||||
|
||||
for my $p ( 2 .. 311 ) {
|
||||
# Horrible primality test, but sufficient for this task.
|
||||
next if ("1" x $p) =~ /^(11+?)\1+$/;
|
||||
# The choice to generate h and k instead of hard coding
|
||||
# them is inspired by the Perl 6 implementation.
|
||||
my $c = $p ** ( 1/3 );
|
||||
push @k, int( ($c - int $c) * WORD );
|
||||
next if @h == 8;
|
||||
my $s = $p ** ( 1/2 );
|
||||
push @h, int( ($s - int $s) * WORD );
|
||||
}
|
||||
|
||||
sub new {
|
||||
my %self = ( state => [@h], str => "", len => 0 );
|
||||
bless \%self, shift;
|
||||
}
|
||||
|
||||
my $rightrotate = sub {
|
||||
my $lo = $_[0] >> $_[1];
|
||||
my $hi = $_[0] << (32 - $_[1]);
|
||||
($hi | $lo);
|
||||
};
|
||||
|
||||
# This is adapted from the wikipedia entry on SHA2.
|
||||
my $compress = sub {
|
||||
my ($state, $bytes) = @_;
|
||||
my @w = unpack 'N*', $bytes;
|
||||
@w == 16 or die 'internal error';
|
||||
my ($a, $b, $c, $d, $e, $f, $g, $h) = @$state;
|
||||
until( @w == 64 ) {
|
||||
my $s0 = $w[-15] >> 3;
|
||||
my $s1 = $w[-2] >> 10;
|
||||
$s0 ^= $rightrotate->($w[-15], $_) for 7, 18;
|
||||
$s1 ^= $rightrotate->($w[-2], $_) for 17, 19;
|
||||
push @w, ($w[-16] + $s0 + $w[-7] + $s1) & MASK;
|
||||
}
|
||||
my $i = 0;
|
||||
for my $w (@w) {
|
||||
my $ch = ($e & $f) ^ ((~$e) & $g);
|
||||
my $maj = ($a & $b) ^ ($a & $c) ^ ($b & $c);
|
||||
my ($S0, $S1) = (0, 0);
|
||||
$S1 ^= $rightrotate->( $e, $_ ) for 6, 11, 25;
|
||||
$S0 ^= $rightrotate->( $a, $_ ) for 2, 13, 22;
|
||||
my $temp1 = $h + $S1 + $ch + $k[$i++] + $w;
|
||||
my $temp2 = $S0 + $maj;
|
||||
($h, $g, $f, $e, $d, $c, $b, $a) =
|
||||
($g, $f, $e, ($d+$temp1)&MASK, $c, $b, $a, ($temp1+$temp2)&MASK);
|
||||
}
|
||||
my $j = 0;
|
||||
$state->[$j++] += $_ for $a, $b, $c, $d, $e, $f, $g, $h;
|
||||
};
|
||||
|
||||
use constant can_Q => eval { length pack 'Q>', 0 };
|
||||
|
||||
sub add {
|
||||
my ($self, $bytes) = @_;
|
||||
$self->{len} += 8 * length $bytes;
|
||||
if( !can_Q and $self->{len} >= WORD ) {
|
||||
my $hi = int( $self->{len} / WORD );
|
||||
$self->{big} += $hi;
|
||||
$self->{len} -= $hi * WORD;
|
||||
}
|
||||
my $len = length $self->{str};
|
||||
if( ($len + length $bytes) < 64 ) {
|
||||
$self->{str} .= $bytes;
|
||||
return $self;
|
||||
}
|
||||
my $off = 64 - $len;
|
||||
$compress->( $self->{state}, $self->{str} . substr( $bytes, 0, $off ) );
|
||||
$len = length $_[0];
|
||||
while( $off+64 <= $len ) {
|
||||
$compress->( $self->{state}, substr( $bytes, $off, 64 ) );
|
||||
$off += 64;
|
||||
}
|
||||
$self->{str} = substr( $bytes, $off );
|
||||
$self;
|
||||
}
|
||||
|
||||
sub addfile {
|
||||
my ($self, $fh) = @_;
|
||||
my $s = "";
|
||||
while( read( $fh, $s, 2**13 ) ) {
|
||||
$self->add( $s );
|
||||
}
|
||||
$self;
|
||||
}
|
||||
|
||||
|
||||
sub digest {
|
||||
my $self = shift;
|
||||
my $final = $self->{str};
|
||||
$final .= chr 0x80;
|
||||
while( ( 8+length $final ) % 64 ) {
|
||||
$final .= chr 0;
|
||||
}
|
||||
if( can_Q ) {
|
||||
$final .= pack 'Q>', $self->{len};
|
||||
} else {
|
||||
$self->{big} ||= 0;
|
||||
$final .= pack 'NN', $self->{big}, $self->{len};
|
||||
}
|
||||
$compress->( $self->{state}, substr $final, 0, 64, "" ) while length $final;
|
||||
if( wantarray ) {
|
||||
map pack('N', $_), @{ $self->{state} };
|
||||
} else {
|
||||
pack 'N*', @{ $self->{state} };
|
||||
}
|
||||
}
|
||||
|
||||
sub hexdigest {
|
||||
if( wantarray ) {
|
||||
map unpack( 'H*', $_), &digest;
|
||||
} else {
|
||||
unpack 'H*', &digest;
|
||||
}
|
||||
}
|
||||
|
||||
unless( caller ) {
|
||||
my @testwith = (@ARGV ? @ARGV : 'Rosetta code');
|
||||
for my $str (@testwith) {
|
||||
my $digester = __PACKAGE__->new;
|
||||
$digester->add($str);
|
||||
print "'$str':\n";
|
||||
print join(" ", $digester->hexdigest), "\n";
|
||||
}
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
@ -1,9 +1,10 @@
|
|||
extern crate rustc;
|
||||
extern crate crypto;
|
||||
|
||||
use rustc::util::sha2::{Sha256, Digest};
|
||||
use crypto::sha2::Sha256;
|
||||
use crypto::digest::Digest;
|
||||
|
||||
fn main() {
|
||||
let mut digest = Sha256::new();
|
||||
digest.input_str("Rosetta code");
|
||||
assert!(digest.result_str() == "764faf5c61ac315f1497f9dfa542713965b785e5cc2f707d6468d7d1124cdfcf".to_string());
|
||||
assert!(digest.result_str() == "764faf5c61ac315f1497f9dfa542713965b785e5cc2f707d6468d7d1124cdfcf".into());
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue