Chapter 13 of 22Part 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.
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;
}
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.
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.