b2KIT

SRI Hash Generator

Generate Subresource Integrity hashes for script and link tags to prevent CDN tampering attacks.

Tested tool guide Tested browser tools Checked August 16, 2026

What SRI Hash Generator does, with a checked example

This tool turns file contents into the Subresource Integrity (SRI) hash that goes inside a script or link tag's integrity attribute, so the browser can reject anything a CDN has tampered with. It digests the exact bytes you enter and returns the ready-to-use value with the algorithm prefix, in the form sha384-.... The thing that surprises most people: the hash must be of the file exactly as served, byte for byte. Hash your local source file instead of the minified build the CDN actually serves, and every visitor's browser will block the resource.

Worked example

A concrete input and expected output from the current implementation.

Input

abc

Expected output

sha384-ywB1P0WjXou1oD1pmsZQBycsMqsO3tFjGotgWkP/W+2AhgcroefMI1i67KE0yCWh

Hashing the three ASCII bytes of 'abc' with SHA-384 and base64-encoding the 48-byte digest yields that 64-character string; the sha384- prefix is the algorithm label the integrity attribute requires. For a real deployment, paste the file's exact served contents - change one byte and the whole digest changes.

How the result is produced

1

Digesting the bytes

The tool hashes the text you paste byte for byte and base64-encodes the raw digest, then prefixes the algorithm name and a dash to produce the value that goes inside integrity="...". Nothing about the URL, the file name, or the library version enters the hash, so the pasted content must be identical to what the browser will actually download.

2

Verification in the browser

When a tag carries an integrity attribute, the browser fetches the resource, computes the same digest, and compares the two. Any mismatch means the resource is discarded: scripts do not execute and stylesheets do not apply. For cross-origin files the tag must also include crossorigin="anonymous" and the CDN must answer with an Access-Control-Allow-Origin header, or the browser refuses the resource instead of loading it unverified.

Good uses

  • Adding an integrity attribute to a third-party script loaded from a public CDN so a hijacked or compromised CDN response is rejected instead of executed.
  • Regenerating the hash after bumping a library version or switching CDN hosts, because the new file's bytes differ and the old integrity value would block it.
  • Hashing CDN-hosted stylesheets the same way, which stops CSS tampering such as injected phishing overlays or UI redressing on a page that trusts a third-party theme.

Limits and checks

  • The hash covers bytes served, not provenance. Pasting unminified source when the CDN serves a minified build, or a file with different line endings (LF versus CRLF), produces a valid-looking hash that blocks the resource for every visitor.
  • Only the sha256-, sha384-, and sha512- prefixes are defined by the SRI specification. A digest under any other label fails verification even when the base64 string itself is correct.
  • SRI is integrity, not availability or authenticity. It catches tampering after you obtained the hash, and it does nothing if the CDN is unreachable. Take the hash from the library's official release notes, not from the CDN's own page, or you certify whatever an attacker chose to serve.

Common questions

Why does my browser block the script even though the hash looks right?

For a cross-origin resource the tag needs crossorigin="anonymous" and the CDN must send an Access-Control-Allow-Origin header. Without both, the browser cannot read the response to verify it, so it blocks the resource rather than loading it unverified. The failure shows up in the console as an integrity or CORS error.

Do I need a new hash every time the file changes?

Yes. Any change to the served bytes - a patch release, a different minifier setting, even a line-ending conversion - produces a different digest, and the old integrity value blocks the new file. Regenerate the hash whenever the file's content changes and update the tag in the same deployment.

References and verification

The example and behavioral notes were checked against the browser implementation. Standards and primary references below define the relevant format, formula, or platform behavior.

Related Tools