@bradthomasbrown/sha
v1.0.0
Published
Minimal, unoptimized, and unaudited SHA (256 only for now) hash library in JavaScript/TypeScript.
Downloads
129
Readme
sha
This is a simple, minimal implementation of SHA-256 in TypeScript/JavaScript.
Why?
In line with our "making a bunch of libraries our selves" approach, we needed SHA-256 for an EVM precompile implementation, so we went ahead and made this. It should also support arbitrary bit strings, which is unusual and not necessary, enabled with the "unaligned bits" parameter.
Installation
npm i @bradthomasbrown/shaUsage
import { sha256 } from "./dist/sha";
const encoder = new TextEncoder();
// sha256("abc")
console.log(sha256(encoder.encode("abc")).toHex());
// ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
// sha256("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")
console.log(sha256(encoder.encode("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")).toHex());
// 248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1
// sha256("a" * 1_000_000
console.log(sha256(encoder.encode("a".repeat(1000000))).toHex());
// cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0
// sha256(1b)
console.log(sha256(new Uint8Array([0b10000000]), 1).toHex());
// b9debf7d52f36e6468a54817c1fa071166c3a63d384850e1575b42f702dc5aa1
// sha256(101b)
console.log(sha256(new Uint8Array([0b10100000]), 3).toHex());
// 36c2b2165533d184079d0431fdfa588021eff5cb79a6a6ad82d7d0377c81928b
// sha256(000000001b)
console.log(sha256(new Uint8Array([0, 0b10000000]), 1).toHex());
// cd30fdd0694698ed4db051b5317fbcd585a6c66b38bab81f5fe3515375ab35e6