τau / crypto/sha256 /

sha256.tau

source
/Users/niconex/Documents/tau/stdlib/crypto/sha256/sha256.tau
1# sha256 - the hash of FIPS 180-4.2#3#	sha256 = import("crypto/sha256")4#5#	sha256.Hex("abc")  # ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad6#	sha256.Sum(data)   # the same 32 bytes, unencoded7#8# It is written in tau, one 64 byte block at a time, so it is meant for9# checksums, signatures and tokens rather than for hashing a large file.1011hex = import("encoding/hex")1213# Size is how many bytes a digest takes, BlockSize how many bytes go into the14# compression function at once. HMAC needs both.15Size = 3216BlockSize = 641718mask32 = 0xffffffff1920# The first 32 bits of the fractional parts of the cube roots of the first 6421# primes, which is where the constants of the round function come from.22K = [23	0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,24	0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,25	0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,26	0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,27	0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,28	0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,29	0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,30	0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,31	0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,32	0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,33	0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f234]3536# The first 32 bits of the fractional parts of the square roots of the first37# eight primes: the state a hash starts from.38initial = [39	0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,40	0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd1941]4243# rotr turns the bits of a 32 bit word right, the ones falling off the end44# coming back at the top.45rotr = fn(x, n) { ((x >> n) | (x << (32 - n))) & mask32 }4647# block runs the compression function over the 64 bytes of b starting at off,48# and returns the new state.49block = fn(state, b, off) {50	# The sixteen words of the block, then the forty eight the round function51	# derives from them.52	w = []53	for i = 0; i < 16; ++i {54		j = off + i * 455		w = append(w, (b[j] << 24) | (b[j + 1] << 16) | (b[j + 2] << 8) | b[j + 3])56	}57	for i = 16; i < 64; ++i {58		s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ (w[i - 15] >> 3)59		s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ (w[i - 2] >> 10)60		w = append(w, (w[i - 16] + s0 + w[i - 7] + s1) & mask32)61	}6263	a = state[0]64	bb = state[1]65	c = state[2]66	d = state[3]67	e = state[4]68	f = state[5]69	g = state[6]70	h = state[7]7172	for i = 0; i < 64; ++i {73		s1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25)74		ch = (e & f) ^ ((~e & mask32) & g)75		t1 = (h + s1 + ch + K[i] + w[i]) & mask3276		s0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22)77		maj = (a & bb) ^ (a & c) ^ (bb & c)78		t2 = (s0 + maj) & mask327980		h = g81		g = f82		f = e83		e = (d + t1) & mask3284		d = c85		c = bb86		bb = a87		a = (t1 + t2) & mask3288	}8990	return [91		(state[0] + a) & mask32,92		(state[1] + bb) & mask32,93		(state[2] + c) & mask32,94		(state[3] + d) & mask32,95		(state[4] + e) & mask32,96		(state[5] + f) & mask32,97		(state[6] + g) & mask32,98		(state[7] + h) & mask3299	]100}101102# Sum returns the 32 bytes of the digest of data, a string or bytes.103Sum = fn(data) {104	if type(data) != "bytes" {105		if failed(data = bytes(string(data))) {106			return data107		}108	}109110	# The padded message: the data, a 0x80 byte, zeroes up to eight bytes short111	# of a whole block, and the length in bits as a 64 bit number.112	n = len(data)113	padded = []114	for i = 0; i < n; ++i {115		padded = append(padded, data[i])116	}117	padded = append(padded, 0x80)118	for len(padded) % 64 != 56 {119		padded = append(padded, 0)120	}121122	bits = n * 8123	for i = 7; i >= 0; --i {124		padded = append(padded, (bits >> (i * 8)) & 0xff)125	}126127	b = bytes(padded)128	state = initial129	for off = 0; off < len(b); off = off + 64 {130		state = block(state, b, off)131	}132133	out = []134	for i = 0; i < 8; ++i {135		out = append(out,136			(state[i] >> 24) & 0xff,137			(state[i] >> 16) & 0xff,138			(state[i] >> 8) & 0xff,139			state[i] & 0xff)140	}141	return bytes(out)142}143144# Hex returns the digest of data as the 64 hexadecimal digits it is usually145# written with.146Hex = fn(data) { hex.EncodeToString(Sum(data)) }