SRI Hash Generator

Free SRI hash generator. Paste a CDN URL, upload a file or paste code to get sha256, sha384 or sha512 integrity attributes and a ready-to-copy script tag.

Advertisement

SRI Hash Generator for Script and Stylesheet Integrity

This SRI hash generator produces the integrity value you attach to a <script> or <link> tag so the browser will refuse to run a third-party file that has been altered. Give it a CDN URL, upload a file from disk, or paste the source directly, and it returns a base64-encoded SHA-256, SHA-384 or SHA-512 digest formatted exactly as Subresource Integrity expects — plus the complete HTML tag, with the crossorigin attribute already set, ready to paste into your template.

Hashing happens entirely in your browser through the Web Crypto API (crypto.subtle.digest). Pasted source and uploaded files never leave your machine, which means you can generate integrity hashes for unreleased internal bundles without shipping them to a third party first.

What Subresource Integrity Actually Does

When you load jQuery, a font loader or an analytics snippet from a public CDN, you are executing code you do not control on your own origin, with full access to your DOM, your cookies and your forms. If that CDN is compromised, or an intermediary tampers with the response, the injected code runs with your site's privileges. Subresource Integrity closes that gap. The browser fetches the resource, computes its cryptographic digest, and compares it against the value you declared. On a mismatch it discards the response and fires an error event — the script never executes and the stylesheet never applies.

The critical property is that verification is done by the browser, not by you or the CDN. An attacker who controls the CDN cannot supply a matching hash for modified content without breaking SHA-2, and an attacker who controls the network cannot rewrite your HTML if it is served over HTTPS.

Integrity Attribute Syntax

The value is a hash expression: the algorithm prefix, a hyphen, then the base64 encoding of the raw digest bytes. Note that it is base64 of the binary digest — not base64 of the hex string, which is the single most common mistake when people generate these by hand.

<script src="https://cdn.example.com/lib.min.js" integrity="sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC" crossorigin="anonymous"></script>

For a stylesheet the shape is the same: <link rel="stylesheet" href="https://cdn.example.com/app.css" integrity="sha384-..." crossorigin="anonymous">

Three details decide whether it works in practice:

  • crossorigin is mandatory for cross-origin resources. Without it the response is an opaque one that the browser will not read, so integrity checking cannot happen and the resource is blocked outright. Use anonymous for public CDN assets and use-credentials only when the request must carry cookies. The CDN must also return a permissive Access-Control-Allow-Origin header — this tool reports whether the URL you fetched does.
  • Multiple hashes are allowed, separated by whitespace (integrity="sha384-abc... sha512-def..."). The browser picks the strongest algorithm it recognises and requires only that one to match. Selecting several algorithms here produces exactly that, which is useful during a migration where two builds may be served.
  • The hash covers the exact bytes. A CDN that recompresses, re-minifies or appends a newline invalidates it. Always pin an immutable, versioned URL — never a @latest alias, whose whole purpose is to change underneath you.

Which Hash Algorithm to Choose

The SRI specification defines exactly three: sha256, sha384 and sha512. MD5 and SHA-1 are not permitted and browsers ignore them. All three produce a value your users' browsers compute at load time, so the cost difference is negligible in practice — the digest is calculated once per resource fetch, over a file that is typically tens of kilobytes.

  • sha384 — the practical default and what the tool preselects. 48 raw bytes become a 64-character base64 string. It is the algorithm the CDN copy-paste widgets emit, so it is also the one your team will recognise in review.
  • sha256 — 32 bytes, a 44-character base64 string, the shortest attribute. Cryptographically sufficient; choose it if attribute length genuinely matters to you.
  • sha512 — 64 bytes, an 88-character base64 string. The largest margin, and the sensible pick if a policy requires 256-bit security strength.

If you want raw digests for a purpose other than SRI — verifying a download, comparing file copies — the general hash generator gives you hex output across more algorithms.

How to Generate an SRI Hash

  1. Choose an input mode: URL to fetch a live CDN asset, File to upload a build artifact, or Text to paste source directly.
  2. Select the algorithms you want. Tick more than one to emit a multi-hash integrity attribute.
  3. Set crossorigin to anonymous unless the resource requires credentials, and tick async or defer if your script tag needs them.
  4. Read the results: the raw base64 digest, the finished integrity attribute, and the complete <script> or <link> tag. Copy the tag or download the output.
  5. Deploy, then confirm in DevTools that the resource loads with no integrity error in the console.

To check an existing tag rather than create one, fetch the same URL here and compare the generated hash against the integrity value already in your HTML. A mismatch means the file changed since the attribute was written — which is precisely the condition SRI exists to catch.

Enforcing SRI Site-Wide

Individual tags are opt-in, so a single forgotten script defeats the policy. Content Security Policy provides require-sri-for script style, which instructs the browser to reject any script or stylesheet loaded without an integrity attribute. Support has been uneven across browsers and the directive has drifted through the W3C process, so treat it as defence in depth rather than a guarantee, and keep a lint rule in CI that fails a build when a cross-origin <script src> lacks integrity. You can assemble the surrounding policy with the CSP generator and confirm what your site actually sends using the security headers analyzer.

Frequently Asked Questions

What is an SRI hash?

An SRI hash is a cryptographic digest of a script or stylesheet, written as algorithm-base64digest in the integrity attribute of a tag. The browser recomputes the digest of what it downloaded and refuses to use the file unless the two match.

Is this SRI hash generator free?

Yes — free, no account, no limit on how many hashes you generate.

Which algorithm should I use, sha256, sha384 or sha512?

sha384 is the conventional default and what most CDNs publish. sha256 produces the shortest attribute and remains cryptographically sound; sha512 gives the widest margin. All three are valid SRI algorithms and all are widely supported.

Why does my script fail to load after adding integrity?

Usually one of three causes: the crossorigin attribute is missing on a cross-origin resource, the CDN does not send an Access-Control-Allow-Origin header, or the file changed after you generated the hash. The browser console names which of these applies.

Do I always need crossorigin="anonymous"?

For any resource on a different origin, yes. Without it the browser receives an opaque response it cannot inspect, so integrity cannot be verified and the load is blocked. Same-origin resources do not require it.

Can I put more than one hash in the integrity attribute?

Yes. Separate hash expressions with whitespace. The browser selects the strongest algorithm it supports and only that one must match — helpful when two builds of a file may be served during a rollout.

Can I check an existing integrity attribute with this tool?

Yes. Fetch the same URL in URL mode and compare the generated value with the one in your markup. If they differ, the file has been modified since the attribute was written.

Does SRI work with dynamically injected scripts?

Yes, if you set the property when creating the element: assign el.integrity and el.crossOrigin before appending the node to the document. Setting them afterwards is too late.

Are my files uploaded to a server?

No. Digests are computed locally with the Web Crypto API. Uploaded and pasted content stays in the browser. Only the URL mode makes a network request, and that request goes to the CDN you named.

Should I use SRI on my own same-origin files?

It adds little security — an attacker who can alter those files can alter the HTML that declares the hash — and it adds a real maintenance cost, since every rebuild changes the digest. Reserve SRI for third-party origins you do not control.

What is require-sri-for?

A Content Security Policy directive, written require-sri-for script style, that tells the browser to block scripts or stylesheets loaded without an integrity attribute. Browser support has been inconsistent, so pair it with a CI check rather than relying on it alone.

What Is Subresource Integrity (SRI)

Subresource Integrity (SRI) is a web security feature that allows browsers to verify that files fetched from CDNs and third-party servers have not been tampered with. By adding a cryptographic hash to <script> and <link> tags, SRI ensures that the browser only executes resources that match the expected content. If a CDN is compromised and serves malicious code, SRI-protected pages will refuse to load the altered resource.

Supply chain attacks targeting CDN-hosted JavaScript libraries are a growing threat. The 2018 event-stream incident, the 2021 ua-parser-js compromise, and numerous attacks on npm packages demonstrate that third-party code can be weaponized. SRI is the browser-native defense against these attacks—no additional libraries or services required.

How SRI Works

SRI uses cryptographic hash functions to create a fingerprint of the expected file contents:

  1. Generate hash: Compute the SHA-256, SHA-384, or SHA-512 hash of the resource file
  2. Add integrity attribute: Include the hash in the HTML tag's integrity attribute
  3. Browser verifies: When the browser downloads the resource, it computes its hash and compares it to the declared value
  4. Block on mismatch: If the hashes don't match, the browser refuses to execute the resource

Example:

<script src="https://cdn.example.com/lib.js"
        integrity="sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8w"
        crossorigin="anonymous"></script>
Hash AlgorithmOutput LengthRecommendation
SHA-256256 bits (64 hex chars)Minimum acceptable
SHA-384384 bits (96 hex chars)Recommended (best balance)
SHA-512512 bits (128 hex chars)Maximum security

Multiple hashes: You can include multiple hashes for forward compatibility when updating resources:

integrity="sha384-abc123... sha384-def456..."

The crossorigin="anonymous" attribute is required for SRI on cross-origin resources; without it, the browser cannot verify the hash.

Common Use Cases

  • CDN-hosted libraries: Protect jQuery, Bootstrap, React, and other libraries loaded from public CDNs
  • Third-party scripts: Verify analytics, chat widgets, and advertising scripts haven't been modified
  • Compliance requirements: PCI-DSS 4.0 requires integrity verification for payment page scripts
  • Supply chain security: Defend against compromised CDN infrastructure or npm package hijacking
  • Build pipeline verification: Generate SRI hashes during CI/CD and embed them in production HTML

Best Practices

  1. Use SHA-384 as the minimum — SHA-256 is acceptable but SHA-384 is the recommended default for SRI hashes
  2. Always include crossorigin="anonymous" — Required for cross-origin resources; without it, SRI verification silently fails
  3. Regenerate hashes when updating libraries — Every version change produces a different hash; automate this in your build process
  4. Use SRI for all CDN resources — Apply integrity attributes to every <script> and <link rel="stylesheet"> loaded from third-party origins
  5. Combine with Content Security Policy — Use CSP's require-sri-for directive to mandate SRI for all scripts and styles on your pages

Frequently Asked Questions

What is Subresource Integrity (SRI) and why should I use it?+

Subresource Integrity (SRI) ensures external resources (CDN JS/CSS) have not been tampered with. Browser verifies file hash matches integrity attribute before executing. Example: script with src attribute pointing to https://cdn.example.com/lib.js with integrity sha384-abc123 and crossorigin anonymous. If file modified (CDN compromise, MITM attack), hash will not match and browser blocks execution. Benefits: prevent CDN compromise, detect file modifications, supply chain security, compliance requirements (PCI DSS). Required for: external resources from CDN, third-party libraries, cross-origin files. W3C standard, supported by all modern browsers. This tool generates SRI hashes for any JS/CSS file instantly.

Which hash algorithm should I use for SRI (SHA-256, SHA-384, or SHA-512)?+

SRI supports SHA-256, SHA-384, SHA-512. Recommendations: SHA-384 most common (balance of security and size), SHA-512 maximum security (longer hash), SHA-256 faster but less secure (still acceptable). You can specify multiple: integrity="sha384-abc123 sha512-def456" (browser uses strongest it supports). Performance: negligible difference for small files, SHA-384 recommended by most CDNs (jsdelivr, cdnjs). Hash format: algorithm-base64hash. Example: "sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC". Security: all three are cryptographically secure for SRI purpose. This tool generates all three algorithms - choose based on your security policy.

How do I implement SRI for third-party CDN resources?+

Add integrity and crossorigin attributes to script/link tags. Steps: 1) Get hash from CDN (many provide it) or generate with this tool. 2) Add to HTML: <script src="https://cdn.jsdelivr.net/npm/vue@3" integrity="sha384-..." crossorigin="anonymous"></script>. 3) Test: browser console shows error if hash mismatch. Requirements: crossorigin attribute required (CORS), HTTPS only (SRI does not work on HTTP), same hash for same file (cache-friendly). Common CDNs with SRI: jsdelivr.com (shows SRI hash), cdnjs.com (copy SRI button), unpkg.com (generate hash). For self-hosted: generate hash during build, include in HTML templates. This tool generates production-ready SRI snippets.

What happens if the SRI hash doesn't match?+

Browser blocks resource execution and fires error event. Console shows: "Failed to find a valid digest in the 'integrity' attribute for resource 'URL'". Consequences: script does not run (may break page functionality), CSS does not apply (visual issues), onerror event fires (can catch with JavaScript). Common causes: file updated on CDN (new version), typo in hash value, wrong file URL, CORS misconfiguration (missing crossorigin). Debugging: check browser console, verify file content matches hash, regenerate hash, ensure CORS headers present. Fallback strategy: use <script onerror="..."> handler to load local copy if CDN fails. Update hashes when upgrading library versions. This tool helps verify current file matches expected hash.

How do I generate SRI hashes during build process?+

Integrate into build pipeline for automated hash generation. Methods: webpack-subresource-integrity plugin (Webpack), vite-plugin-sri (Vite), generate with command line: openssl dgst -sha384 -binary file.js | openssl base64 -A, Node.js script with crypto module. Build process: 1) Build assets (minify JS/CSS). 2) Generate SRI hash for each file. 3) Inject into HTML template. 4) Deploy assets and HTML together. Version control: commit hashes with code, update on file changes only. CI/CD: automate hash generation on every deployment. Popular tools handle automatically. This tool useful for: local development, manual verification, one-off hash generation, learning SRI concepts.

Can I use SRI with dynamic content or inline scripts?+

SRI only works for external resources (separate file URLs), not inline scripts/styles. Inline scripts have no integrity attribute. External scripts with src attribute pointing to https://cdn.example.com/file.js with integrity attribute work with SRI. For dynamic content: if file changes frequently, SRI problematic (hash changes). Solutions: versioned URLs (lib-v1.2.3.js), build-time hash generation, accept hash updates on deployments. Dynamic loading (JavaScript): fetch with integrity check using the Fetch API. Service workers: verify hashes in SW logic. If content truly dynamic (user-generated, personalized), SRI not applicable - use other security measures (CSP, input sanitization). This tool generates hashes for static resources, the primary SRI use case.

How does SRI relate to Content Security Policy (CSP)?+

SRI and CSP are complementary security mechanisms. CSP: defines which domains can load resources (script-src, style-src). SRI: verifies resource content has not changed. Together: CSP allows cdn.example.com, SRI verifies specific file hash. Example: CSP header Content-Security-Policy: script-src 'self' https://cdn.example.com, HTML: <script src="https://cdn.example.com/lib.js" integrity="sha384-...">. CSP controls source, SRI controls content. Best practice: use both - CSP prevents unauthorized sources, SRI prevents compromised allowed sources. Modern security: CSP + SRI + HTTPS = layered defense. This tool focuses on SRI hash generation - combine with CSP headers for complete protection.

What are the limitations and browser support for SRI?+

Browser support: all modern browsers (Chrome 45+, Firefox 43+, Safari 11.1+, Edge 17+). IE 11 doesn't support (ignores integrity attribute). Limitations: HTTPS only (no SRI on HTTP), requires CORS (crossorigin attribute), doesn't work for inline scripts, hash updates needed on file changes, doesn't prevent all attacks (can block if CDN fully compromised). Not a replacement for: code review, dependency scanning, CSP, regular security updates. Best for: third-party CDN resources, static assets, public libraries. Performance: minimal overhead (hash verification is fast). Fallback: older browsers ignore integrity attribute gracefully, still load resource. This tool generates cross-browser compatible SRI hashes following W3C standard.

This tool is provided for informational and educational purposes only. All processing happens in your browser — no data is sent to or stored on our servers. While we strive for accuracy, we make no warranties about the completeness or reliability of results.