Pikkola SRI, tool No. 91

The hash a browser checks before it runs your script.

Compute the SHA-256, SHA-384 and SHA-512 integrity of a file or a snippet, write the script and link tags that carry it, and verify an attribute you were given against the bytes you have. Everything runs in WebCrypto, in this tab.

3
algorithms
2
elements
0
requests

The integrity bench

Hash the bytes, then write the tag

An integrity attribute is a promise about exact bytes. Paste the file's text or drop the file itself, read the three hashes, then copy the tag and the policy line that go with it.

Drop the file you serve

Read as bytes in this tab.

Content text
Integrity
The tags

      
The policy line

      

A cross-origin file needs crossorigin="anonymous".

What integrity does, and what it cannot

An integrity attribute binds a tag to a digest. It stops a changed file from running, and it does not stop a malicious one from being approved in the first place.

01

The hash is of bytes, not of meaning

One space added, one line ending changed from LF to CRLF, and the digest is different. That is why the page hashes the file you drop rather than a URL you paste, and why a minified build is hashed after minification.

02

An unpinned tag runs anything

Without integrity, a script tag trusts the CDN, its DNS and its deploy process on every page load. With a hash, a changed file fails closed: the browser refuses to execute it and reports an error in the console.

03

The policy line is the other half

An inline script cannot carry the attribute, so a Content-Security-Policy hash is what binds it. External scripts are better pinned with a host allowlist and strict-dynamic, and the line here is a starting point, not a complete policy.

What is hashed, and what is only written out

The page is a digest and a text builder. It does not lint a policy, does not check CORS and does not know what your CDN sends.

Part What this page does What it will not do
Text input Encoded as UTF-8 and hashed exactly as typed, with line endings left alone Normalise the text or strip whitespace
A dropped file Read as bytes and hashed from the raw buffer Decode it as text first, which would change the hash of a binary file
The three algorithms All three are computed and each is shown with its bit length Pick one behind your back: the tag is built from the one you choose
A URL Written into the tag and never fetched Download the URL and hash what came back, which is a different file from the one your users get
crossorigin and referrerpolicy Written only when you pick them, in the order the HTML expects Guess a CORS setting for you: the CDN decides whether it works
The verify tab Compares a pasted attribute with the computed bytes and names each algorithm Fetch the script a page actually loaded

Integrity does not survive a dynamic import or an eval in most browsers, and it says nothing about the content being safe. A pinned script that mines a visitor's CPU is still pinned, and still runs.

Field guide

Bytes, CORS and what a hash promises

Subresource Integrity is a small attribute with an exact meaning: this tag may only run if the bytes it fetches hash to this value. Understanding what it covers explains both the failures it prevents and the ones it does not.

The digest is over the response body, exactly

integrity="sha384-..." is the base64 of the SHA-384 digest of the decoded response body: not the compressed bytes on the wire, not the file on disk before minification, not the same file with a different line ending. A build that changes a single byte changes the digest, and a server that re-encodes or rewrites a file in flight will fail the check for everyone. That is why the page takes the artefact, hashes it, and never fetches the URL: the only bytes that matter are the ones the browser will receive.

All three algorithms are computed so you can see the bit lengths side by side: 32, 48 and 64 bytes. A browser uses the strongest algorithm in the attribute that it supports, so an attribute with two tokens is a hedge for a client that lacks one of them, not a second chance at matching.

Cross-origin, and the CORS dependency nobody mentions

To check integrity a browser has to read the body of the response, and for a cross-origin request that means the response must be readable under CORS. Concretely: crossorigin="anonymous" on the tag, and Access-Control-Allow-Origin on the response. Without both, the fetch fails before the digest is compared, and the console message is a CORS error rather than an integrity error, which sends people looking in the wrong place.

Same-origin scripts need no crossorigin and no CORS header. A script served from your own origin with integrity is the simplest correct setup, and it is worth preferring for a critical library.

What an integrity attribute cannot do

It pins a file; it does not vouch for it. A hash you generated from a compromised build is a perfect pin on a malicious file. It also does not survive everything: a dynamic import() of a URL, eval, a blob URL, or a service worker that rewrites the response all step outside the attribute's reach in most browsers. And it does not cover the page around the script, so an XSS in your own code is unaffected by any of it.

For inline script there is no attribute at all, which is why the CSP hash exists: script-src 'sha384-...' allows exactly that inline text and nothing else. That is the only form in which a hash binds an inline block.

SRI and a Content-Security-Policy are different tools

Integrity protects the bytes of one resource from being swapped. A policy decides which resources may load at all. A host allowlist is broad: every path on that host is allowed. A hash is narrow: one file, exactly. In practice the durable pattern for external scripts is a host allowlist plus 'strict-dynamic', with an integrity attribute on each pinned script as a second lock, and hashes for the inline bootstrap that has to exist.

The policy line this page writes is a correct hash source for the element you built. It is not a complete policy: a real one also names default-src, object-src 'none', base-uri and the rest, and it needs testing with a report-only pass before enforcement.

Verifying a claim you were given

The verify tab exists for the moment a vendor hands you an integrity attribute and a file. Paste both and the page computes the three digests and compares each token, naming the algorithm that matched and the one that did not. A common failure is a file that was re-saved with CRLF line endings or a trailing newline added by an editor, and the per-algorithm report is what turns "it does not work" into "the bytes differ".

The same digests without the tag are on Pikkola Hash. The full policy around them is on Pikkola CSP.

Questions

Why not just give the page a URL?

Because a digest of a file this page fetched is a digest of what this page got, not of what your visitors get: CDNs serve different builds by region, compress on the fly and change a file without changing its name. Hash the artefact you deploy. If you cannot, hash the exact response body with curl and a file, not with a browser that may have been given something else.

Which algorithm should the tag use?

sha384 is the usual choice and the one the SRI specification recommends. sha512 costs a little more and is fine. sha256 is supported but shorter than the guidance prefers. All three are accepted in one attribute separated by spaces, and a browser uses the strongest it supports, so listing two is a reasonable hedge.

Why did my integrity fail with a CORS error?

Integrity on a cross-origin file requires a CORS response, because the browser has to read the body to hash it. That means crossorigin="anonymous" on the tag (or use-credentials with credentials) and an Access-Control-Allow-Origin header from the CDN. Without both, the request fails before the hash is even compared.

Does the CSP line work as written?

It is a correct hash source, and that is all it is. For an inline script, script-src with the hash is the whole story. For an external one, a host allowlist plus strict-dynamic is the more durable pattern, because a hash pins one file and you cannot add a nonce to a file on a CDN. Treat the line as a starting point and test it with the policy report.

Where does the digest happen?

In WebCrypto in this tab, on the bytes you supplied. Nothing is uploaded, the URL field is never fetched, and a reload forgets the content. The only request the page makes after it loads is the cookieless page-view count. A private build is as safe here as a public one.