Chapter 13 of 22Part III: Cybersecurity and Privacy
The Digital Fingerprint
Each project below is tailored to a specific algorithm and scientific question from Chapter 13, with concrete inputs, verifiable numerical outputs, boundary cases, and links to the related lab and pioneer.
Project 1 of 3 · v1-c13-p01Foundational · ~25 min
SHA-256 Avalanche & Trusted-Reference Verifier
Lab activity: A. One letter, a storm of changes · Related code: block-01.js, block-04.js · Pioneer: View profile
Real-World Problem: Downloading software requires verifying that the file was not corrupted or tampered with.
Scientific Question: Why does a hash mismatch prove modification only when compared against a trusted reference digest?
Objective: Compute SHA-256 digests with Web Crypto, measure bit-level avalanche on a 1-character edit, and separate integrity from authenticity.
Hypothesis to Test: Changing one character of an input flips approximately 50% (~128 of 256) of the SHA-256 output bits.
Step-by-Step Implementation
Compute SHA-256("abc") via crypto.subtle.digest and verify it matches the NIST FIPS 180-4 test vector.
Change one character ("abd") and count differing bits across all 256 output bits.
Simulate an attacker replacing BOTH a file and an untrusted hash hosted on the same mirror.
Explain why obtaining the reference hash over a separate trusted/signed channel is essential.
Concrete Input: "abc" vs. "abd" (and NIST vector check for "abc").
Expected Output: SHA-256("abc") = ba7816bf8f01cfea...; Hamming distance between "abc" and "abd" digests ≈ 128 bits.
Boundary Condition: Identical inputs always produce identical 256-bit digests regardless of platform.
Common Mistake to Avoid: Treating a matching hash from an untrusted webpage as proof of authenticity.
Interpreting Results: Hashes verify integrity against a trusted reference; authentication requires a secret key (HMAC) or asymmetric signature.
Show Hint, Extension Challenge & Reflection
Hint: Review the worked example and equation in Chapter 13 before testing the boundary case: Identical inputs always produce identical 256-bit digests regardless of platform.
Extension Challenge: Explain why plain SHA-256 is too fast for password storage and why Argon2id/PBKDF2 adds salt and work factor.
Reflection Question: Why are Merkle trees (Ralph Merkle) built out of cryptographic hashes in Git and certificate transparency?
Lab activity: B. Issue and verify a fictional certificate · Related code: block-02.js, block-05.js · Pioneer: View profile
Real-World Problem: Browsers must verify that a website's public key genuinely belongs to that domain without sharing a private key.
Scientific Question: How does asymmetric signing (private key signs, public key verifies) prevent forgery even when everyone holds the verification key?
Objective: Generate an ECDSA P-256 keypair in Web Crypto, sign a fictional certificate JSON, verify it, and test tampering.
Hypothesis to Test: Altering a single character in the fictional certificate's subject or expiry date causes ECDSA verification to return false.
Step-by-Step Implementation
Generate an ECDSA P-256 keypair using crypto.subtle.generateKey over HTTPS or localhost.
Sign the UTF-8 bytes of the fictional certificate with the private key (SHA-256 hash).
Verify the signature using only the public key.
Tamper with one character of the certificate and confirm verification returns false.
Test Cases: Valid sig -> true; Tampered payload -> false; Wrong public key -> false.
Boundary Condition: ECDSA signatures include a fresh random nonce k, so signing the same payload twice yields two distinct valid signatures.
Common Mistake to Avoid: Comparing signature bytes for equality instead of calling crypto.subtle.verify().
Interpreting Results: Digital signatures bind identity and integrity together across public networks.
Show Hint, Extension Challenge & Reflection
Hint: Review the worked example and equation in Chapter 13 before testing the boundary case: ECDSA signatures include a fresh random nonce k, so signing the same payload twice yields two distinct valid signatures.
Extension Challenge: Test verifying a valid signature with a second freshly generated public key to confirm cross-key rejection.
Reflection Question: How did Leslie Lamport and Ralph Merkle's early signature concepts evolve into today's web PKI?
Lab activity: C. A time-based code · Related code: block-03.js, block-04.js, block-05.js · Pioneer: View profile
Real-World Problem: Two-factor authenticator apps generate 6-digit codes offline that expire every 30 seconds.
Scientific Question: How does RFC 6238 derive a 6-digit code from an HMAC, and which hash algorithms does the standard allow?
Objective: Compute TOTP codes using Web Crypto HMAC (supporting SHA-1, SHA-256, and SHA-512) and test clock-skew windows.
Hypothesis to Test: Using the same secret and Unix time step yields identical 6-digit codes offline, while a 31-second clock skew moves to the next counter.
Step-by-Step Implementation
Compute 64-bit big-endian time counter T = Math.floor(unixSeconds / stepSeconds) (default X = 30s).
Compute HMAC(secret, T) via crypto.subtle.sign using selectable SHA-1, SHA-256, or SHA-512.
Apply RFC 4226 dynamic truncation (low 4 bits of last byte select 4-byte offset) and take mod 10^6.
Verify that an expired code from T − 2 is rejected while T ± 1 handles small clock drift.
Concrete Input: Fictional test secret "12345678901234567890", Unix time = 59s, step X = 30s, digits = 6.
Expected Output: Counter T = 1 -> deterministic 6-digit RFC 6238 test code; supports SHA-1, SHA-256, and SHA-512.
Test Cases: RFC 6238 test vector (T=1, 20-byte ASCII "12345678901234567890", SHA-1, 8 digits -> 94287082).
Boundary Condition: Codes must be zero-padded on the left (e.g., "004812", not "4812").
Common Mistake to Avoid: Claiming TOTP is restricted to SHA-1 or hardcoded to 30 seconds (RFC 6238 allows SHA-256/SHA-512 and configurable step/digits).
Interpreting Results: Time synchronization plus HMAC delivers offline second-factor codes without network calls.
Show Hint, Extension Challenge & Reflection
Hint: Review the worked example and equation in Chapter 13 before testing the boundary case: Codes must be zero-padded on the left (e.g., "004812", not "4812").
Extension Challenge: Compare outputs for HMAC-SHA-1 vs. HMAC-SHA-256 on the same fictional test secret and timestamp.
Reflection Question: Why should real 2FA secrets never be pasted into untrusted websites?