Chapter 13 of 22 Part III: Cybersecurity and Privacy

The Digital Fingerprint

5 Verified Book Snippets

Every .js file below is byte-identical (SHA-256 verified) to the printed code in Volume 1. Runnable blocks execute inside an isolated sandboxed iframe and Web Worker with a 2-second timeout and zero network access.

Download Chapter ZIP

Hash (Ch. 13 Example 1)

File: block-01.js (byte-identical) · 431 bytes · SHA-256: 58d07c8719c9…
SHA-256 HTTPS Web Crypto

Dependencies: Browser Web Crypto API (W3C Secure Context: HTTPS or localhost). Uses window.crypto.subtle; test inside the chapter Interactive Lab over HTTPS (https://howdigitalworks.dev) or localhost.

const bytes = s => new TextEncoder().encode(s);
const hex = b => Array.from(new Uint8Array(b),
  n => n.toString(16).padStart(2, '0')).join('');
async function hash(text) {
  return hex(await crypto.subtle.digest('SHA-256', bytes(text)));
}
function changedBits(a, b) {
  let n = 0;
  for (let i = 0; i < a.length; i++) {
    let x = parseInt(a[i], 16) ^ parseInt(b[i], 16);
    while (x) { n += x & 1; x >>= 1; }
  }
  return n;
}
Download .js

A — The Digital Fingerprint (Example 2)

File: block-02.js (byte-identical) · 95 bytes · SHA-256: dfdd23cfea92…
SHA-256 Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Runs directly in Node.js or the isolated Web Worker sandbox and prints console output.

const a = await hash("abc");
const b = await hash("abd");
console.log(a, b, changedBits(a, b));
Download .js

New Keys (Ch. 13 Example 3)

File: block-03.js (byte-identical) · 396 bytes · SHA-256: 3ed10e871304…
SHA-256 HTTPS Web Crypto

Dependencies: Browser Web Crypto API (W3C Secure Context: HTTPS or localhost). Uses window.crypto.subtle; test inside the chapter Interactive Lab over HTTPS (https://howdigitalworks.dev) or localhost.

async function newKeys() {
  return crypto.subtle.generateKey(
    {name:'ECDSA', namedCurve:'P-256'}, false,
    ['sign','verify']);
}
const scheme = {name:'ECDSA', hash:'SHA-256'};
async function signText(key, text) {
  return crypto.subtle.sign(scheme, key, bytes(text));
}
async function verifyText(key, text, signature) {
  return crypto.subtle.verify(scheme, key, signature, bytes(text));
}
Download .js

Keys — The Digital Fingerprint (Example 4)

File: block-04.js (byte-identical) · 269 bytes · SHA-256: 6e1a55dcc3f3…
SHA-256 Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Runs directly in Node.js or the isolated Web Worker sandbox and prints console output.

const keys = await newKeys();
const text = "Science Fair: Mira, prize 10";
const sig = await signText(keys.privateKey, text);
console.log(await verifyText(keys.publicKey, text, sig));
console.log(await verifyText(keys.publicKey,
  "Science Fair: Mira, prize 90", sig));
Download .js

Demo OTP (Ch. 13 Example 5)

File: block-05.js (byte-identical) · 585 bytes · SHA-256: a65fadde36a1…
SHA-256 HTTPS Web Crypto

Dependencies: Browser Web Crypto API (W3C Secure Context: HTTPS or localhost). Uses window.crypto.subtle; test inside the chapter Interactive Lab over HTTPS (https://howdigitalworks.dev) or localhost.

async function demoOTP(seconds, digits = 6) {
  const secret = bytes('12345678901234567890');
  const key = await crypto.subtle.importKey('raw', secret,
    {name:'HMAC', hash:'SHA-1'}, false, ['sign']);
  const counter = new ArrayBuffer(8);
  new DataView(counter).setBigUint64(0,
    BigInt(Math.floor(seconds / 30)));
  const h = new Uint8Array(await crypto.subtle.sign('HMAC',key,counter));
  const i = h[h.length - 1] & 15;
  const value = ((h[i] & 127) * 16777216) +
    (h[i+1] * 65536) + (h[i+2] * 256) + h[i+3];
  return String(value % (10 ** digits)).padStart(digits, '0');
}
Download .js