Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -0,0 +1,2 @@
(use 'pandect.core)
(sha256 "Rosetta code")

View file

@ -0,0 +1,5 @@
void main() {
import std.stdio, std.digest.sha;
writefln("%-(%02x%)", "Rosetta code".sha256Of);
}

View file

@ -19,7 +19,7 @@ struct SHA256 {
alias TResult = ubyte[256 / 8];
version(WORDS_BIGENDIAN) {
static uint bswap(in uint n) pure nothrow { return n; }
static uint bswap(in uint n) pure nothrow @safe @nogc { return n; }
}
// Bytes used to pad the buffer to the next 64-byte boundary.
@ -30,7 +30,7 @@ struct SHA256 {
Takes a pointer to a 256 bit block of data (eight 32 bit ints) and
intializes it to the start constants of the SHA256 algorithm. This
must be called before using hash in the call to sha256_hash. */
void init() pure nothrow {
void init() pure nothrow @safe @nogc {
state = [0x6a09e667U, 0xbb67ae85U, 0x3c6ef372U, 0xa54ff53aU,
0x510e527fU, 0x9b05688cU, 0x1f83d9abU, 0x5be0cd19U];
total[] = 0;
@ -42,7 +42,7 @@ struct SHA256 {
the initialization function) update the context for the next LEN
bytes starting at BUFFER.
It is not required that LEN is a multiple of 64. */
void processBytes(in ubyte[] inBuffer) pure nothrow {
void processBytes(in ubyte[] inBuffer) pure nothrow @nogc {
// When we already have some bits in our internal
// buffer concatenate both inputs first.
const(ubyte)* inBufferPtr = inBuffer.ptr;
@ -61,10 +61,8 @@ struct SHA256 {
processBlock(bufferB[0 .. bufLen & ~63]);
bufLen &= 63;
// The regions in the following copy operation
// cannot overlap.
memcpy(bufferB.ptr,
&bufferB[(left_over + add) & ~63], bufLen);
// The regions in the following copy operation cannot overlap.
memcpy(bufferB.ptr, &bufferB[(left_over + add) & ~63], bufLen);
}
inBufferPtr += add;
@ -99,15 +97,15 @@ struct SHA256 {
the next len bytes starting at buffer.
It is necessary that len is a multiple of 64. */
void processBlock(in ubyte[] inBuffer)
pure nothrow in {
pure nothrow @nogc in {
assert(inBuffer.length % 64 == 0);
} body {
// Round functions.
static uint F1(in uint e, in uint f, in uint g) pure nothrow {
static uint F1(in uint e, in uint f, in uint g) pure nothrow @safe @nogc {
return g ^ (e & (f ^ g));
}
static uint F2(in uint a, in uint b, in uint c) pure nothrow {
static uint F2(in uint a, in uint b, in uint c) pure nothrow @safe @nogc {
return (a & b) | (c & (a | b));
}
@ -132,18 +130,18 @@ struct SHA256 {
if (total[0] < len)
total[1]++;
static uint rol(in uint x, in uint n) pure nothrow {
static uint rol(in uint x, in uint n) pure nothrow @safe @nogc {
return (x << n) | (x >> (32 - n)); }
static uint S0(in uint x) pure nothrow {
static uint S0(in uint x) pure nothrow @safe @nogc {
return rol(x, 25) ^ rol(x, 14) ^ (x >> 3); }
static uint S1(in uint x) pure nothrow {
static uint S1(in uint x) pure nothrow @safe @nogc {
return rol(x, 15) ^ rol(x, 13) ^ (x >> 10); }
static uint SS0(in uint x) pure nothrow {
static uint SS0(in uint x) pure nothrow @safe @nogc {
return rol(x, 30) ^ rol(x,19) ^ rol(x, 10); }
static uint SS1(in uint x) pure nothrow {
static uint SS1(in uint x) pure nothrow @safe @nogc {
return rol(x, 26) ^ rol(x, 21) ^ rol(x, 7); }
uint M(in uint I) nothrow {
uint M(in uint I) pure nothrow @safe @nogc {
immutable uint tm = S1(x[(I - 2) & 0x0f]) +
x[(I - 7) & 0x0f] +
S0(x[(I - 15) & 0x0f]) +
@ -154,7 +152,7 @@ struct SHA256 {
static void R(in uint a, in uint b, in uint c, ref uint d,
in uint e, in uint f, in uint g, ref uint h,
in uint k, in uint m) pure nothrow {
in uint k, in uint m) pure nothrow @safe @nogc {
immutable t0 = SS0(a) + F2(a, b, c);
immutable t1 = h + SS1(e) + F1(e, f, g) + k + m;
d += t1;
@ -268,7 +266,7 @@ struct SHA256 {
resBuf.
Important: On some systems it is required that resBuf is correctly
aligned for a 32-bit value. */
void conclude() pure nothrow {
void conclude() pure nothrow @nogc {
// Take yet unprocessed bytes into account.
immutable bytes = bufLen;
immutable size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4;
@ -294,7 +292,7 @@ struct SHA256 {
result must be in little endian byte order.
Important: On some systems it is required that resBuf is correctly
aligned for a 32-bit value. */
ref TResult read(ref TResult resBuf) const pure nothrow {
ref TResult read(return ref TResult resBuf) pure nothrow @nogc {
foreach (immutable i, immutable s; state)
(cast(uint*)resBuf.ptr)[i] = bswap(s);
return resBuf;
@ -307,7 +305,7 @@ struct SHA256 {
the wanted ASCII representation of the message digest.
Important: On some systems it is required that resBuf be correctly
aligned for a 32 bits value. */
ref TResult finish(ref TResult resBuf) pure nothrow {
ref TResult finish(return ref TResult resBuf) pure nothrow @nogc {
conclude;
return read(resBuf);
}
@ -317,8 +315,8 @@ struct SHA256 {
buffer. The result is always in little endian byte order, so that
a byte-wise output yields to the wanted ASCII representation of
the message digest. */
static ref TResult digest(in ubyte[] inBuffer, ref TResult resBuf)
pure nothrow {
static ref TResult digest(in ubyte[] inBuffer, return ref TResult resBuf)
pure nothrow @nogc {
SHA256 sha = void;
// Initialize the computation context.
@ -333,7 +331,7 @@ struct SHA256 {
/// ditto
static TResult digest(in ubyte[] inBuffer) pure nothrow {
static TResult digest(in ubyte[] inBuffer) pure nothrow @nogc {
align(4) TResult resBuf = void;
return digest(inBuffer, resBuf);
}

View file

@ -0,0 +1 @@
(secure-hash 'sha256 "Rosetta code") ;; as string of hex digits

View file

@ -0,0 +1,4 @@
;; using the crypto module from http://www.newlisp.org/code/modules/crypto.lsp.html
;; (import native functions from the crypto library, provided by OpenSSL)
(module "crypto.lsp")
(crypto:sha256 "Rosetta Code")

View file

@ -1,21 +1,19 @@
#import <CommonCrypto/CommonHMAC.h>
#import <Cocoa/Cocoa.h>
#import <CommonCrypto/CommonDigest.h>
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSString* key = @"secret";
NSString* data = @"Message";
const char *cKey = [key cStringUsingEncoding:NSASCIIStringEncoding];
const char *cData = [data cStringUsingEncoding:NSASCIIStringEncoding];
unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
NSMutableString* result = [NSMutableString stringWithCapacity:(CC_SHA256_DIGEST_LENGTH * 2)];
for(CC_LONG i = 0; i < CC_SHA256_DIGEST_LENGTH; i++)
[result appendFormat:@"%02x", cHMAC[i]];
NSLog(@"Sha-256: %@", result);
int main(int argc, char ** argv) {
NSString * msg = @"Rosetta code";
unsigned char buf[CC_SHA256_DIGEST_LENGTH];
const char * rc = [msg cStringUsingEncoding:NSASCIIStringEncoding];
if (! CC_SHA256(rc, strlen(rc), buf)) {
NSLog(@"Failure...");
return -1;
}
NSMutableString * res = [NSMutableString stringWithCapacity:(CC_SHA256_DIGEST_LENGTH * 2)];
for (int i = 0; i < CC_SHA256_DIGEST_LENGTH; ++i) {
[res appendFormat:@"%02x", buf[i]];
}
NSLog(@"Output: %@", res);
return 0;
}

View file

@ -1,4 +1,4 @@
say .list».fmt("%02x").join given sha256 "Rosetta code";
say sha256 "Rosetta code";
constant primes = grep &is-prime, 2 .. *;
sub init(&f) {
@ -14,10 +14,9 @@ multi sha256(Str $str where all($str.ords) < 128) {
}
multi sha256(Blob $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 @b = $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;
my @H = init(&sqrt)[^8];
@ -41,7 +40,5 @@ multi sha256(Blob $data) {
}
@H = @H Z[m+] @h;
}
return Blob.new: map -> $word is rw {
reverse gather for ^4 { take $word % 256; $word div= 256 }
}, @H;
return Blob.new: map { reverse .polymod(256 xx 3) }, @H;
}

View file

@ -0,0 +1,160 @@
(setq *Sha256-K
(mapcar hex
'("428A2F98" "71374491" "B5C0FBCF" "E9B5DBA5" "3956C25B"
"59F111F1" "923F82A4" "AB1C5ED5" "D807AA98" "12835B01"
"243185BE" "550C7DC3" "72BE5D74" "80DEB1FE" "9BDC06A7"
"C19BF174" "E49B69C1" "EFBE4786" "0FC19DC6" "240CA1CC"
"2DE92C6F" "4A7484AA" "5CB0A9DC" "76F988DA" "983E5152"
"A831C66D" "B00327C8" "BF597FC7" "C6E00BF3" "D5A79147"
"06CA6351" "14292967" "27B70A85" "2E1B2138" "4D2C6DFC"
"53380D13" "650A7354" "766A0ABB" "81C2C92E" "92722C85"
"A2BFE8A1" "A81A664B" "C24B8B70" "C76C51A3" "D192E819"
"D6990624" "F40E3585" "106AA070" "19A4C116" "1E376C08"
"2748774C" "34B0BCB5" "391C0CB3" "4ED8AA4A" "5B9CCA4F"
"682E6FF3" "748F82EE" "78A5636F" "84C87814" "8CC70208"
"90BEFFFA" "A4506CEB" "BEF9A3F7" "C67178F2") ) )
(de rightRotate (X C)
(| (mod32 (>> C X)) (mod32 (>> (- C 32) X))) )
(de mod32 (N)
(& N `(hex "FFFFFFFF")) )
(de not32 (N)
(x| N `(hex "FFFFFFFF")) )
(de add32 @
(mod32 (pass +)) )
(de sha256 (Str)
(let Len (length Str)
(setq Str
(conc
(need
(-
8
(* 64 (/ (+ Len 1 8 63) 64)) )
(conc (mapcar char (chop Str)) (cons `(hex "80")))
0 )
(flip
(make
(setq Len (* 8 Len))
(do 8
(link (& Len 255))
(setq Len (>> 8 Len )) ) ) ) ) ) )
(let
(H0 `(hex "6A09E667")
H1 `(hex "BB67AE85")
H2 `(hex "3C6EF372")
H3 `(hex "A54FF53A")
H4 `(hex "510E527F")
H5 `(hex "9B05688C")
H6 `(hex "1F83D9AB")
H7 `(hex "5BE0CD19") )
(while Str
(let
(A H0
B H1
C H2
D H3
E H4
F H5
G H6
H H7
W
(conc
(make
(do 16
(link
(apply
|
(mapcar >> (-24 -16 -8 0) (cut 4 'Str)) ) ) ) )
(need 48 0) ) )
(for (I 17 (>= 64 I) (inc I))
(let
(Wi15 (get W (- I 15))
Wi2 (get W (- I 2))
S0
(x|
(rightRotate Wi15 7)
(rightRotate Wi15 18)
(>> 3 Wi15) )
S1
(x|
(rightRotate Wi2 17)
(rightRotate Wi2 19)
(>> 10 Wi2) ) )
(set (nth W I)
(add32
(get W (- I 16))
S0
(get W (- I 7))
S1 ) ) ) )
(use (Tmp1 Tmp2)
(for I 64
(setq
Tmp1
(add32
H
(x|
(rightRotate E 6)
(rightRotate E 11)
(rightRotate E 25) )
(x| (& E F) (& (not32 E) G))
(get *Sha256-K I)
(get W I) )
Tmp2
(add32
(x|
(rightRotate A 2)
(rightRotate A 13)
(rightRotate A 22) )
(x|
(& A B)
(& A C)
(& B C) ) )
H G
G F
F E
E (add32 D Tmp1)
D C
C B
B A
A (add32 Tmp1 Tmp2) ) ) )
(setq
H0 (add32 H0 A)
H1 (add32 H1 B)
H2 (add32 H2 C)
H3 (add32 H3 D)
H4 (add32 H4 E)
H5 (add32 H5 F)
H6 (add32 H6 G)
H7 (add32 H7 H) ) ) )
(mapcan
'((N)
(flip
(make
(do 4
(link (& 255 N))
(setq N (>> 8 N)) ) ) ) )
(list H0 H1 H2 H3 H4 H5 H6 H7) ) ) )
(let Str "Rosetta code"
(println
(pack
(mapcar
'((B) (pad 2 (hex B)))
(sha256 Str) ) ) )
(println
(pack
(mapcar
'((B) (pad 2 (hex B)))
(native
"libcrypto.so"
"SHA256"
'(B . 32)
Str
(length Str)
'(NIL (32)) ) ) ) ) )
(bye)

View file

@ -0,0 +1,9 @@
extern crate rustc;
use rustc::util::sha2::{Sha256, Digest};
fn main() {
let mut digest = Sha256::new();
digest.input_str("Rosetta code");
assert!(digest.result_str() == "764faf5c61ac315f1497f9dfa542713965b785e5cc2f707d6468d7d1124cdfcf".to_string());
}

View file

@ -0,0 +1,11 @@
object RosettaSHA256 extends App {
def MD5(s: String): String = {
// Besides "MD5", "SHA-256", and other hashes are available
val m = java.security.MessageDigest.getInstance("SHA-256").digest(s.getBytes("UTF-8"))
m.map("%02x".format(_)).mkString
}
assert(MD5("Rosetta code") == "764faf5c61ac315f1497f9dfa542713965b785e5cc2f707d6468d7d1124cdfcf")
println("Successfully completed without errors.")
}

View file

@ -0,0 +1 @@
(SHA256 new hashStream: 'Rosetta code' readStream) hex.