~singpolyma/haxe-sha

f41b43a6cf83b0be6c92cbb3c874aa51048dbdc2 — Stephen Paul Weber 7 months ago
Translate from sha.js
7 files changed, 285 insertions(+), 0 deletions(-)

A COPYING
A README
A sha/Hash.hx
A sha/SHA256.hx
A test.hxml
A test/TestAll.hx
A test/TestSHA256.hx
A  => COPYING +49 -0
@@ 1,49 @@
Copyright (c) 2013-2018 sha.js contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


Copyright (c) 1998 - 2009, Paul Johnston & Contributors
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

Neither the name of the author nor the names of its contributors may be used to
endorse or promote products derived from this software without specific prior
written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


A  => README +5 -0
@@ 1,5 @@
SHA hashes for Haxe

import sha.SHA256;

trace(new SHA256().update(Bytes.ofString("Hello")).digest().toHex());

A  => sha/Hash.hx +83 -0
@@ 1,83 @@
package sha;

import haxe.io.Bytes;

abstract class Hash {
	final finalSize:Int;
	var length:Int;
	final block:Bytes;

	function new(blockSize:Int, finalSize:Int) {
		this.finalSize = finalSize;
		block = Bytes.alloc(blockSize);
		length = 0;
	}

	private inline function min(a:Int, b:Int) {
		if (a < b) return a;
		return b;
	}

	private inline function writeInt(b:Bytes, i:Int, pos:Int) {
		b.set(pos, (i >> 24));
		b.set(pos + 1, (i >> 16) & 0xFF);
		b.set(pos + 2, (i >> 8) & 0xFF);
		b.set(pos + 3, i & 0xFF);
	}

	public function update(data:Bytes) {
		var accum = length;

		var offset = 0;
		while (offset < data.length) {
			var assigned = accum % block.length;
			var remainder = min(data.length - offset, block.length - assigned);

			for (i in 0...remainder) {
				block.set(assigned + i, data.get(offset + i));
			}

			accum += remainder;
			offset += remainder;

			if ((accum % block.length) == 0) {
				_update(block);
			}
		}

		length += data.length;
		return this;
	}

	public function digest() {
		var rem = length % block.length;

		block.set(rem, 0x80);

		block.fill(rem + 1, block.length - (rem + 1), 0);

		if (rem >= finalSize) {
			_update(block);
			block.fill(0, block.length, 0);
		}

		final bits = length * 8;

		if (bits <= 0xffffffff) {
			writeInt(block, bits, block.length - 4);
		} else {
			final lowBits = (bits & 0xffffffff) >>> 0;
			final highBits = Std.int((bits - lowBits) / 4294967296);

			writeInt(block, highBits, block.length - 8);
			writeInt(block, lowBits, block.length - 4);
		}

		_update(block);

		return _hash();
	}

	private abstract function _update(data:Bytes):Void;
	private abstract function _hash():Bytes;
}

A  => sha/SHA256.hx +121 -0
@@ 1,121 @@
package sha;

import haxe.io.Bytes;

final K = [
	0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
	0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
	0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
	0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
	0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
	0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
	0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
	0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
	0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
	0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
	0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
	0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
	0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
	0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
	0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
	0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2
];

class SHA256 extends Hash {
	private var _a = 0x6a09e667;
	private var _b = 0xbb67ae85;
	private var _c = 0x3c6ef372;
	private var _d = 0xa54ff53a;
	private var _e = 0x510e527f;
	private var _f = 0x9b05688c;
	private var _g = 0x1f83d9ab;
	private var _h = 0x5be0cd19;
	private var _w = [];

	public function new() {
		super(64, 56);
	}

	private function sigma0(x) {
		return (x >>> 2 | x << 30) ^ (x >>> 13 | x << 19) ^ (x >>> 22 | x << 10);
	}

	private function sigma1(x) {
		return (x >>> 6 | x << 26) ^ (x >>> 11 | x << 21) ^ (x >>> 25 | x << 7);
	}

	function gamma0(x) {
		return (x >>> 7 | x << 25) ^ (x >>> 18 | x << 14) ^ (x >>> 3);
	}

	private function gamma1(x) {
		return (x >>> 17 | x << 15) ^ (x >>> 19 | x << 13) ^ (x >>> 10);
	}

	private function ch(x, y, z) {
		return z ^ (x & (y ^ z));
	}

	private function maj(x, y, z) {
		return (x & y) | (z & (x | y));
	}

	private function _update(data:Bytes) {
		var W = _w;

		var a = _a | 0;
		var b = _b | 0;
		var c = _c | 0;
		var d = _d | 0;
		var e = _e | 0;
		var f = _f | 0;
		var g = _g | 0;
		var h = _h | 0;

		for (i in 0...16) {
			W[i] = (data.get(i * 4) << 24) | (data.get((i * 4) + 1) << 16) | (data.get((i * 4) + 2)) << 8 | data.get((i * 4) + 3);
		}

		for (i in 16...64) {
			W[i] = (gamma1(W[i - 2]) + W[i - 7] + gamma0(W[i - 15]) + W[i - 16]) | 0;
		}

		for (j in 0...64) {
			var T1 = (h + sigma1(e) + ch(e, f, g) + K[j] + W[j]) | 0;
			var T2 = (sigma0(a) + maj(a, b, c)) | 0;

			h = g;
			g = f;
			f = e;
			e = (d + T1) | 0;
			d = c;
			c = b;
			b = a;
			a = (T1 + T2) | 0;
		}

		_a = (a + _a) | 0;
		_b = (b + _b) | 0;
		_c = (c + _c) | 0;
		_d = (d + _d) | 0;
		_e = (e + _e) | 0;
		_f = (f + _f) | 0;
		_g = (g + _g) | 0;
		_h = (h + _h) | 0;
	}

	private function _hash() {
		final b = Bytes.alloc(32);

		writeInt(b, _a, 0);
		writeInt(b, _b, 4);
		writeInt(b, _c, 8);
		writeInt(b, _d, 12);
		writeInt(b, _e, 16);
		writeInt(b, _f, 20);
		writeInt(b, _g, 24);
		writeInt(b, _h, 28);

		return b;
	}
}

A  => test.hxml +4 -0
@@ 1,4 @@
--library utest

--run
test.TestAll

A  => test/TestAll.hx +10 -0
@@ 1,10 @@
package test;

import utest.Runner;
import utest.ui.Report;

class TestAll {
	public static function main() {
		utest.UTest.run([new TestSHA256()]);
	}
}

A  => test/TestSHA256.hx +13 -0
@@ 1,13 @@
package test;

import haxe.io.Bytes;
import utest.Assert;
import sha.SHA256;

class TestSHA256 extends utest.Test {
	public function testHello() {
		final hash = new SHA256();
		hash.update(Bytes.ofString("Hello"));
		Assert.equals("185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969", hash.digest().toHex());
	}
}