How SRI Works with Cryptographic Hashes
You'll learn how browsers use cryptographic hash algorithms to verify that external resources haven't been tampered with.
When you include an external script or stylesheet on your page, Subresource Integrity (SRI) acts like a fingerprint scanner at a secure facility. The browser compares the downloaded file's cryptographic hash against the hash you specified in the integrity attribute.
The Three Hash Algorithms
Browsers support three SHA (Secure Hash Algorithm) variants for SRI:
- SHA-256: Produces a 256-bit (32-byte) hash
- SHA-384: Produces a 384-bit (48-byte) hash
- SHA-512: Produces a 512-bit (64-byte) hash
Higher numbers mean longer hashes and theoretically stronger security, though SHA-384 is the recommended standard for SRI.
The Verification Process
When a browser encounters a resource with an integrity attribute:
- It downloads the resource (script, stylesheet, etc.)
- Computes the cryptographic hash of the downloaded content
- Compares this computed hash to the hash in your
integrityattribute - If they match exactly, the resource executes
- If they differ even by one character, the browser blocks the resource entirely
Think of it like a wax seal on an ancient letter. If the seal is broken or doesn't match the sender's signet ring, you know something's wrong.
The hash format combines the algorithm name with the base64-encoded hash value: sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K...
Key Takeaway: SRI uses SHA-256, SHA-384, or SHA-512 cryptographic hashes to create unique fingerprints of resources, allowing browsers to detect any modifications before executing potentially compromised code.