Three hashes of the same text
Hash text with SHA-256, SHA3-256 and BLAKE2b in pure JavaScript, live as you type.
Apache-2.0Runs on this page
Package: @awacloud/fw
Hash the input with SHA-256, SHA3-256 and BLAKE2b-256 in pure JavaScript, the digests shown in hex as you type.
Uses @awacloud/fw (Apache-2.0). This example's code is Apache-2.0.
When to use it
Integrity checks and content addressing, in any browser and in workers,
without crypto.subtle (see fw/crypto-webcrypto for the native tier).
For a keyed tag or a derived key, see fw/crypto-mac-kdf.
Copy-paste steps
npm install @awacloud/fw- Serve
node_modules/@awacloud/fw/src/at/vendor/fw/(node serve.jsfrom the examples root does this; any static server works). - Copy
index.htmlandmain.jsnext to each other and openindex.htmlthrough that server. The../../_style/examples.csslink is optional: copy that file along, or delete the link.
What it shows
sha256.hash(text)andsha3.sha3_256(text)return bit arrays:bitArray.ba_to_ui8turns them into bytes.blake2b.hash(bytes, 32)returns bytes already.hex.fromBytesfor display; the digests match the public known answers (FIPS 180-4, FIPS 202, RFC 7693).
Code
main.js
// SPDX-License-Identifier: Apache-2.0
import fw from '@awacloud/fw';
import { sha256 } from '@awacloud/fw/crypto/hash/sha256.js';
import { sha3 } from '@awacloud/fw/crypto/hash/sha3.js';
import { blake2b } from '@awacloud/fw/crypto/hash/blake2b.js';
import { hex } from '@awacloud/fw/io/codec/hex.js';
fw.runtime.registerAllDeep([sha256, sha3, blake2b, hex]);
const SHA256 = fw.runtime.resolve('sha256');
const SHA3 = fw.runtime.resolve('sha3');
const BLAKE2B = fw.runtime.resolve('blake2b');
const HEX = fw.runtime.resolve('hex');
// SHA-256 and SHA3 return bit arrays (32-bit words): bitArray turns them into bytes.
const BA = fw.runtime.resolve('bitArray');
const encoder = new TextEncoder();
/**
* Three 256-bit digests of the same text.
* @param {string} message
* @returns {{ sha256: string, sha3: string, blake2b: string }} hex digests
*/
export function digests(message) {
return {
sha256: HEX.fromBytes(BA.ba_to_ui8(SHA256.hash(message))),
sha3: HEX.fromBytes(BA.ba_to_ui8(SHA3.sha3_256(message))),
blake2b: HEX.fromBytes(BLAKE2B.hash(encoder.encode(message), 32)),
};
}
/** @param {string} id @param {string} value */
const show = (id, value) => { /** @type {HTMLElement} */ (document.getElementById(id)).textContent = value; };
const input = /** @type {HTMLInputElement} */ (document.getElementById('input'));
function update() {
const d = digests(input.value);
show('sha256', d.sha256);
show('sha3', d.sha3);
show('blake2b', d.blake2b);
}
input.addEventListener('input', update);
update();index.html
<!doctype html>
<!-- SPDX-License-Identifier: Apache-2.0 -->
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>fw · Three hashes of the same text</title>
<link rel="stylesheet" href="../../_style/examples.css">
<script type="importmap">
{ "imports": {
"@awacloud/fw": "/vendor/fw/main.js", "@awacloud/fw/": "/vendor/fw/" } }
</script>
<script type="module" src="./main.js"></script>
</head>
<body>
<main>
<h1>Three hashes of the same text</h1>
<label for="input">Message</label>
<input id="input" type="text" value="abc" autocomplete="off">
<dl>
<dt>SHA-256</dt><dd><code id="sha256"></code></dd>
<dt>SHA3-256</dt><dd><code id="sha3"></code></dd>
<dt>BLAKE2b-256</dt><dd><code id="blake2b"></code></dd>
</dl>
</main>
</body>
</html>