Open in a new tab

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

  1. npm install @awacloud/fw
  2. Serve node_modules/@awacloud/fw/src/ at /vendor/fw/ (node serve.js from the examples root does this; any static server works).
  3. Copy index.html and main.js next to each other and open index.html through that server. The ../../_style/examples.css link is optional: copy that file along, or delete the link.

What it shows

  • sha256.hash(text) and sha3.sha3_256(text) return bit arrays: bitArray.ba_to_ui8 turns them into bytes. blake2b.hash(bytes, 32) returns bytes already.
  • hex.fromBytes for display; the digests match the public known answers (FIPS 180-4, FIPS 202, RFC 7693).

Code

main.js

javascriptApache-2.042 lines

// 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

htmlApache-2.027 lines

<!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>