SRI hash generator for scripts and stylesheets
This SRI hash generator computes the Subresource Integrity value for a JavaScript or CSS file and wraps it in a ready-to-use HTML tag. You paste the file contents (or upload the file itself), choose a digest, and get a string such as sha384-… plus a <script> or <link rel="stylesheet"> element with the integrity and crossorigin attributes already filled in.
What is Subresource Integrity?
Subresource Integrity (SRI) is a W3C browser feature that lets you pin a third-party file to an exact version. When you load a library from a CDN, you are trusting that server to keep serving the same bytes. If the CDN is compromised or the file changes, every site that includes it runs the new code.
With SRI you add a cryptographic hash of the expected file to the tag. Before executing the script or applying the stylesheet, the browser hashes what it downloaded and compares. If the digests differ, the resource is blocked and a console error is logged, so a tampered file never runs.
Example
Using the sample file from the tool with the default SHA-384 option and the URL https://cdn.example.com/lib/app.min.js, the output is:
sha384-5THHiNTcO4NqmiMOyQLTJlh4rdCraN1xWjw7oVCB6VWQVfLzX2a3QocOQ7xApux8
<script src="https://cdn.example.com/lib/app.min.js"
integrity="sha384-5THHiNTcO4NqmiMOyQLTJlh4rdCraN1xWjw7oVCB6VWQVfLzX2a3QocOQ7xApux8"
crossorigin="anonymous"></script>
The integrity value is the algorithm name, a hyphen, and the Base64 (not hex) encoding of the raw digest. That is why an SRI string looks different from the output of a regular SHA-384 hash generator, which usually prints hex.
Options explained
- Hash – SHA-384 is the common default and is what most CDNs publish. SHA-256 and SHA-512 are also valid. All three puts several space-separated hashes in one attribute; browsers use the strongest algorithm they support.
- File URL for the tag – the
srcorhrefwritten into the generated tag. It is only used for the markup, not fetched. - Tag – Detect from URL produces a
<link rel="stylesheet">when the URL ends in.cssand a<script>otherwise. You can force either one.
The status bar reports how many bytes were hashed, which is a quick sanity check that you pasted the whole file.
Why crossorigin="anonymous" matters
Browsers only perform integrity checks on cross-origin files that are fetched in CORS mode. The generated tag therefore always includes crossorigin="anonymous". The CDN must also answer with an Access-Control-Allow-Origin header; major public CDNs do, but a self-hosted asset bucket may need that header configured before SRI works.
Common errors and fixes
- "Failed to find a valid digest in the integrity attribute" – the bytes differ from what you hashed. Upload the exact file the CDN serves rather than a copy you reformatted, minified or saved with different line endings. A single changed newline produces a different hash.
- Pasted text vs. uploaded file – pasting converts the text to UTF-8 and may drop a byte order mark or change CRLF to LF. When in doubt, use the upload option so the original bytes are hashed.
- Floating versions – URLs such as
@latestor/lib/v3/can change contents at any time, breaking the hash. Pin an exact version. - CORS blocked – if the server does not send CORS headers, the resource fails even when the hash is correct.
When to use SRI
Add integrity attributes to any script or stylesheet loaded from a domain you do not control. It pairs well with a Content Security Policy; you can review your policy with the CSP evaluator. For first-party files served from your own origin, SRI adds little because an attacker who can change the file can usually change the HTML too.
Privacy
Hashing uses the browser's built-in WebCrypto API. Your file is read locally and is never uploaded to our server, so you can safely hash private bundles before deploying them.