Cybersecurity

SRI Hash Algorithms: SHA-256 vs SHA-384 vs SHA-512

Subresource Integrity supports SHA-256, SHA-384, and SHA-512. All three are currently secure — here's how their digest length, security margin, and browser behavior compare, and why SHA-384 is the common default.

By Inventive HQ Team

Subresource Integrity (SRI) supports three hash algorithms — SHA-256, SHA-384, and SHA-512 — and all three are currently considered secure. They differ only in digest length and long-term security margin: SHA-256 produces a 256-bit hash, SHA-384 a 384-bit hash, and SHA-512 a 512-bit hash, each base64-encoded and prefixed with its algorithm name in the integrity attribute (e.g. sha384-oqVuAf…). SHA-384 is the de facto default used by MDN examples, major CDNs, and most SRI generators, because it adds security margin over SHA-256 without SHA-512's slightly longer string. Longer hashes buy future safety margin, not extra protection against any attack that exists today.

That's the summary an AI Overview would give you. Here's what it can't: the actual trade-off is almost never about cryptographic strength — for real websites all three algorithms are equally unbroken — so the decision comes down to convention, digest length, and matching whatever your CDN already emits. Below is the side-by-side comparison, a diagram of how the algorithm prefix drives verification, and the specific reasons to reach for one over another.

SHA-256 vs SHA-384 vs SHA-512 for SRI

PropertySHA-256SHA-384SHA-512
Digest length256 bits (32 bytes)384 bits (48 bytes)512 bits (64 bytes)
Base64 length in integrity~44 chars~64 chars~88 chars
Algorithm prefixsha256-sha384-sha512-
SHA-2 familyYesYes (truncated SHA-512)Yes
Currently secureYes — no practical attacksYes — no practical attacksYes — no practical attacks
Future security marginGoodLargerLargest
Browser support (SRI)All modern browsersAll modern browsersAll modern browsers
CDN / generator defaultOccasionalMost common defaultOccasional
When to useSmallest attribute; fine for most sitesRecommended default — best balanceStrictest policies or max future margin

The row that matters most is the last one. SHA-384 is recommended not because SHA-256 is weak but because it is the value the ecosystem already standardized on — copy a snippet from cdnjs or jsDelivr and you will almost always see sha384-. Matching that convention keeps your markup consistent with the wider web and with your own copied CDN tags.

Advertisement

How the algorithm prefix drives verification

The integrity attribute is self-describing: the text before the dash tells the browser which hash function to run over the downloaded bytes, then it compares that digest to the base64 value after the dash. If they match, the resource loads; if they differ by even one bit, the browser blocks it.

How an SRI integrity attribute is verified The browser downloads a resource, hashes its bytes with the algorithm named in the prefix, and compares the result to the base64 digest. A match loads the file; a mismatch blocks it. integrity="sha384-oqVuAf…" sha384 algorithm prefix oqVuAf… expected base64 digest Download resource bytes Hash with SHA-384 Compare to expected digest Match → load file runs normally Mismatch → block tampered file rejected

Because the prefix names the algorithm inline, you can even list several hashes separated by spaces — integrity="sha256-abc… sha384-def…" — and every conformant browser picks the strongest one it recognizes. That makes migrating between algorithms painless.

Generate the right hash

You never compute these by hand. Point a generator at the exact file (or URL) you are pinning and copy the emitted attribute. This tool produces all three algorithm variants so you can paste whichever your policy or CDN convention calls for:

Loading interactive tool...

What "secure" really means here

All three algorithms belong to the SHA-2 family, and none of them has a known practical collision or preimage attack. That is why the honest answer to "which is most secure?" is "they are equally unbroken today." The difference is future margin:

  • SHA-256 — 256-bit digest. No practical attacks; perfectly adequate for SRI. Its only edge is the shortest integrity string.
  • SHA-384 — 384-bit digest, computed as a truncated SHA-512. The community default: enough extra bits to feel comfortable for the long haul, standard across CDNs.
  • SHA-512 — 512-bit digest. The largest safety margin and the longest attribute value; choose it when a security policy explicitly mandates the strongest available function.

Deliberately not allowed in SRI are MD5 and SHA-1 — both have practical collision attacks — and SHA-3, which the current specification simply does not include. If a generator or legacy snippet hands you an md5- or sha1- integrity value, treat it as broken and regenerate with SHA-384.

Practical recommendation

For nearly every website: use SHA-384, and match whatever your CDN already emits. Reach for SHA-512 only when a compliance policy demands the maximum digest length, and drop to SHA-256 only if you have a specific reason to minimize attribute size. Whatever you pick, generate the hash from the exact bytes you are serving, and remember that any change to the file — intentional or malicious — invalidates the hash and blocks the load. That last property is the whole reason SRI exists.

Frequently Asked Questions

Which hash algorithm should I use for SRI?

Use SHA-384. The Subresource Integrity spec supports SHA-256, SHA-384, and SHA-512, and all three are currently secure. SHA-384 is the de facto default — it is what MDN examples, popular CDNs, and most SRI generators emit — because it offers a larger security margin than SHA-256 without the slightly longer digest of SHA-512. Any of the three protects you today; SHA-384 is simply the well-worn middle path.

What hash algorithms does Subresource Integrity support?

Exactly three: SHA-256, SHA-384, and SHA-512, all from the SHA-2 family. Conformant browsers MUST support all three. Weaker functions like MD5 and SHA-1 are deliberately not allowed, and SHA-3 is not part of the current SRI specification. The algorithm is named by a prefix in the integrity attribute, such as sha384-.

How is the SRI hash encoded in the integrity attribute?

Each hash is written as algorithm-base64value — the algorithm prefix (sha256, sha384, or sha512), a dash, then the raw hash digest encoded in base64 (RFC 4648). For example, integrity="sha384-oqVuAf...". It is not hex. You can list several space-separated hashes and the browser picks the strongest one it recognizes.

Is SHA-256 secure enough for SRI?

Yes. SHA-256 has no known practical collision or preimage attacks and is considered secure for SRI today. SHA-384 and SHA-512 are chosen not because SHA-256 is broken but because their larger digests give a wider long-term safety margin. For most sites the practical security difference is negligible.

Does a longer SRI hash make my site more secure right now?

Not measurably. All three algorithms currently resist collision and preimage attacks, so SHA-512 is not more secure than SHA-256 against any attack that exists today. The longer digest buys future margin — more bits an attacker would have to defeat if a weakness were ever found — not extra protection against present-day threats.

Can I list more than one hash in a single integrity attribute?

Yes. Put multiple space-separated hashes in the integrity value, for example integrity="sha256-abc... sha384-def...". The browser validates against the strongest algorithm it supports. This is useful during migrations, letting older and newer clients each find a hash they can verify.

Which SRI hash do CDNs like cdnjs and jsDelivr use?

Most major CDNs and the SRI Hash Generator tools default to SHA-384. That is why copied CDN snippets almost always show a sha384- prefix. You are free to regenerate the same file as SHA-256 or SHA-512 if a policy requires it — the resource is identical, only the digest changes.

Does the SRI hash change if the file changes?

Yes — that is the entire point. The hash is computed over the exact bytes of the resource, so any change, even a single character, produces a completely different digest and the browser refuses to load the modified file. This is what protects you if a CDN is compromised and a script is tampered with.

SRISHA-256SHA-384SHA-512cryptographic hashing