How to Generate SRI Hashes for Secure CDN Assets: A Complete Guide
Learn how to use Subresource Integrity (SRI) hashes to protect your website from CDN tampering and supply chain attacks with the free SRI Hash Generator tool.
Table of Contents
How to Generate SRI Hashes for Secure CDN Assets: A Complete Guide
Modern websites load dozens of external resources β JavaScript libraries, CSS frameworks, fonts, and third-party widgets β from content delivery networks (CDNs). While CDNs improve performance, they also introduce a serious risk: if an attacker compromises the CDN (or you're loading from a third-party host you don't control), they can swap a legitimate file for a malicious one and inject code straight into your visitors' browsers. That's where Subresource Integrity (SRI) comes in.
SRI lets you embed a cryptographic hash of an external file directly in your HTML. The browser computes the hash of the downloaded file and compares it against the one you declared. If they don't match, the browser refuses to execute the resource β blocking malicious content before it ever runs. To create these hashes quickly, you can use the free SRI Hash Generator, which fetches a URL, computes the digest, and hands you a ready-to-paste <script> or <link> tag.
In this guide, we'll walk through what SRI is, why it matters, how to use the generator, and the best practices that keep your CDN-loaded assets trustworthy.
Why Use SRI Hash Generator?
- Protects against supply chain attacks. Even if a CDN is breached and a file is tampered with, the mismatched hash prevents the altered file from executing in your users' browsers.
- Ensures content integrity. You get a cryptographic guarantee that the bytes your users download are exactly the bytes you intended to serve β byte for byte.
- Works entirely in the browser. The tool uses the native crypto.subtle.digest() API to compute hashes locally, so your resources and hashes never pass through a third-party server.
- Generates ready-to-use HTML. You don't have to hand-craft integrity and crossorigin attributes β the tool produces a complete, correctly-formatted tag you can paste straight into your markup.
- Supports all W3C-recommended algorithms. Choose SHA-256, SHA-384 (the W3C default), or SHA-512 depending on your security and compatibility requirements.
- No setup, no cost. The generator is free, requires no account, and runs on any modern browser without installation.
Key Features
| Feature | Description |
|---|---|
| Multi-algorithm hashing | Generate integrity hashes using SHA-256, SHA-384 (default), or SHA-512. |
| URL-based generation | Paste any resource URL (e.g. a CDN link) and the tool fetches and hashes the file automatically. |
| Auto HTML tag output | Produces a complete <script> or <link> tag with integrity and crossorigin="anonymous" attributes. |
| Smart file-type detection | Detects .js, .css, and other extensions to generate the correct tag β or a comment for unsupported types. |
| One-click copy | Copy either the raw integrity hash or the full HTML tag to your clipboard instantly. |
| CORS-aware fetching | Sets the correct cross-origin request mode so CDN resources are retrieved and hashed reliably. |
- Keyboard-friendly. Press Ctrl/Cmd+Enter to trigger generation instantly, plus a handy Load Example button to see the tool in action without typing.
- Zero data retention. Files are fetched, hashed in memory, and discarded β nothing is stored or logged.
- Reset and iterate. A reset button lets you clear the form and hash a new resource in seconds.
How to Use SRI Hash Generator
- Paste your resource URL. Enter the full URL of the file you want to protect, such as https://cdn.example.com/library.js or a Bootstrap CSS link from a CDN.
- Choose an algorithm. SHA-384 is selected by default (the W3C recommendation), but you can switch to SHA-256 or SHA-512 if your project standardizes on one.
- Generate the hash. Click Generate β or press Ctrl/Cmd+Enter. The tool fetches the file, computes the digest, and base64-encodes the result.
- Copy the output you need. Use the copy buttons to grab either the raw integrity value (e.g. sha384-...) or the complete HTML tag with all attributes filled in.
- Paste into your HTML. Replace your existing <script src="..."> or <link href="..."> tag with the generated one, deploy, and verify in the browser console that the resource loads without integrity errors.
How Subresource Integrity Works
At its core, SRI is a single HTML attribute: integrity. It holds a hash algorithm name and a base64-encoded cryptographic digest of the file's contents, joined by a hyphen:
<script src="https://cdn.example.com/library.js" integrity="sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC" crossorigin="anonymous"></script>
Here's how the browser uses it:
- You declare the hash. When you add the integrity attribute, you're telling the browser, "This is the exact content I expect."
- The browser downloads the resource. It fetches the file from the CDN just as it normally would β but it doesn't execute or apply it yet.
- It computes the hash locally. Using the algorithm named in the attribute (sha384 above), the browser runs the same digest function over the downloaded bytes.
- It compares the digests. The freshly computed base64 digest is compared against the one in the integrity attribute.
- On match β execute. If they're identical, the script runs or the stylesheet is applied normally.
- On mismatch β block. If even a single byte differs, the browser refuses to execute the resource and reports a integrity-failure error in the console. No malicious code runs.
The crossorigin="anonymous" attribute is essential here. Without it, the browser treats the resource as same-origin and won't send the credentials required for the integrity check on many CDN configurations. The anonymous setting tells the browser to request the resource without cookies or credentials, which is what most public CDNs expect β and without it, the integrity attribute may be ignored entirely.
Practical Use Cases
CDN-Hosted Libraries (jQuery, Bootstrap, lodash)
The classic SRI scenario. When you load jQuery or Bootstrap from a public CDN, pinning the hash guarantees that the library your users get is the one you tested. This is exactly what the official CDNs provide β and now you can generate those hashes yourself for any library and version.
Font Files
Web fonts loaded from services like Google Fonts or Font Awesome benefit from SRI too. A tampered font file could be used to fingerprint users or, in edge cases, deliver payloads. Hashing your @font-face source files and any stylesheet that defines them keeps typography trusted.
Framework Bundles (React, Vue, Angular)
If you ship a UMD build of React, Vue, or Angular from a CDN rather than bundling it, SRI protects the entry point of your entire application. A compromised framework build is one of the most dangerous supply chain attacks possible β integrity attributes make that attack surface effectively zero.
Third-Party Widgets and Analytics
Analytics snippets, chat widgets, A/B testing scripts, and ad tags frequently load external JavaScript you don't control. Adding an integrity attribute to these tags ensures that if the vendor's CDN is compromised, the altered script won't run on your site β protecting both you and your users.
Best Practices
- Rotate hashes whenever you update a file. A new version of a library has new bytes and therefore a new hash. Pin the new hash before deploying, or your users will see integrity failures.
- Prefer SHA-384. It's the W3C's recommended default β strong enough for integrity checking and widely supported across all modern browsers, with better collision resistance than SHA-256.
- Always set crossorigin="anonymous". Without the correct CORS mode, the browser may skip the integrity check entirely, defeating the purpose of the hash.
- Audit third-party CDNs regularly. Periodically regenerate hashes and confirm the files still match what you deployed. A CDN that silently changes a file (legitimately or otherwise) will surface as a mismatch.
- Regenerate after minification or build changes. If your build pipeline minifies or transpiles differently between releases, the output bytes change β so the SRI hash must be recomputed each time.
- Add integrity attributes to every external resource. Scripts, stylesheets, and even imported modules can all carry integrity. Don't leave any CDN-loaded file unprotected.
Secure Your CDN Assets Today
Adding Subresource Integrity hashes to your external resources takes about thirty seconds per file and closes one of the most under-appreciated security gaps on the modern web. There's no build step, no dependency, and no cost β just a hash attribute that lets the browser do the hard work for you. Try the SRI Hash Generator now and lock down your CDN assets today.
Related Tools You Might Like
Stay secure and build with integrity!