DevTools Logo

CSP Hash & Nonce Generator

CSP Hash & Nonce Generator

Turn inline script or style content into exact SHA hashes and fresh CSP nonces — entirely in your browser.

Runs in your browser
Try an example

Inline content

0 bytes

Paste only the text between the tags. Separate multiple blocks with a line containing ---; escape a literal delimiter line as \---.

Hash & nonce output

Per-response nonce

Use the same nonce in the header and on the element. Generate a fresh one for every response; a static nonce in cached HTML defeats it.

Working…

Import nonce to verify

Examples

Inline alert

Input
alert(1)
Output
script-src 'sha256-bhHHL3z2vDgxUt0W3dWQOrprscmda2Y5pLsLg4GF+pI=' 'nonce-…'

The canonical alert(1) SHA-256 vector, shown as a CSP token rather than hex.

Two blocks

Input
console.log("a")
---
console.log("b")
Output
Two labelled sets of sha256-/sha384-/sha512- tokens

Each block is hashed independently; the delimiter line itself is never hashed.

Newline mismatch

Input
alert(1) vs alert(1)\n
Output
Different sha256- values

Toggle 'Hash with trailing newline' to test the most common browser mismatch.

About this tool

A strict Content-Security-Policy blocks inline scripts and styles unless you allow-list them. This generator produces the two tokens that do that: base64 SHA-256, SHA-384 or SHA-512 hashes of the exact bytes between your <script> or <style> tags, and fresh cryptographically random nonces.

The hash is computed with the browser's WebCrypto API over UTF-8 bytes, with no trimming or normalisation. Separate multiple inline blocks with a delimiter line to hash each one, and choose the directive the tokens belong in — script-src, style-src, their -elem/-attr variants, or default-src. The assembled fragment is ready to paste into a header.

Byte-exactness is the whole point: a trailing newline, CRLF line endings, a UTF-8 BOM or a pasted tag wrapper all change the digest. The tool surfaces those invisible differences and reports the UTF-8 byte count so you can compare against the bytes the browser actually served. Everything happens locally; nothing is uploaded or stored.

What this tool does not do

  • WebCrypto requires a secure context: the tool cannot hash on plain HTTP origins other than localhost.
  • This computes hash and nonce fragments only; it does not assemble or validate a complete CSP policy.
  • Style-attribute values cannot be authorised by element hashes, and style hash support varies across browsers.
  • Nonce verification checks syntax and entropy only — freshness and actual deployment cannot be verified from a pasted value.
  • Files are read whole into memory; WebCrypto's digest API has no incremental mode for streaming large files.

How to use this tool

  1. Paste the inner content

    Copy only the text between the <script> or <style> tags. Use 'Strip wrapping tags' if you pasted the tags as well.

  2. Pick the directive

    Choose script-src, style-src, a fine-grained -elem/-attr directive, or default-src so the fragment is assembled correctly.

  3. Read the digests

    SHA-256, SHA-384 and SHA-512 tokens are computed as you type. Use the delimiter line --- to hash several blocks at once.

  4. Copy the fragment

    Copy the quoted tokens or the full directive line, then generate a nonce for the header and the matching nonce attribute.

Use cases

Legacy CSP migration

Allow-list the remaining inline scripts by hash while moving to a nonce-based strict-dynamic policy.

Report-only review

Recreate the hash from the served HTML to understand which inline block a CSP violation refers to.

Structured data

Hash a JSON-LD script block without accidentally including its <script type="application/ld+json"> wrapper.

Token shapes

TokenMeaning
'sha256-<base64>'Hash of the exact UTF-8 bytes of one inline block
'sha384-<base64>' / 'sha512-<base64>'Longer digests of the same bytes; stronger but longer headers
'nonce-<base64>'32 random bytes, base64 encoded; new value for every response
---Delimiter line between blocks; escape a literal delimiter as \---

Hashes use the standard base64 alphabet with padding, as required by CSP — not hex digests.

Common mistakes

Mistake:Including the <script> or <style> tags in the hashed text.

Fix:Hash only the inner content. Use the strip-tags action when a full element was pasted.

Mistake:Adding 'unsafe-inline' next to a hash and assuming both are enforced.

Fix:In CSP2/3-capable browsers a hash or nonce makes 'unsafe-inline' ignored in that directive. Keep it only as a fallback for older browsers.

Mistake:Expecting a style hash to authorise style="…" attributes.

Fix:Element hashes cover <style> content. Attribute values need style-src-attr with 'unsafe-hashes' and are not uniformly supported.

Mistake:Reusing one nonce across responses or cached HTML.

Fix:Generate a new nonce for every response and put the same value in both the header and the element attribute.

Frequently asked questions

Why does my hash not match the browser's?

Browsers hash the exact UTF-8 bytes they received between the tags. A trailing newline, CRLF instead of LF, a UTF-8 BOM, an invisible non-breaking space, or a framework that rewrites the block all produce a different digest. Copy the bytes from view-source, use the byte counter and invisible-character notices, and try the trailing-newline toggle before copying the token.

Do I quote hashes with 'unsafe-inline'?

Yes — CSP source expressions such as 'sha256-…' and 'nonce-…' are always wrapped in single quotes. Note also that in CSP2/3-capable browsers the presence of a hash or nonce makes 'unsafe-inline' ignored in that directive; keep 'unsafe-inline' only as a fallback for older browsers, not as extra protection.

Does a hash cover the <script> tags themselves?

No. The digest covers only the text between the opening and closing tags — attributes such as type="application/ld+json" are not included. If you pasted a whole element, use the strip-tags action first; including the tags is the most common cause of a wrong hash.

Nonce vs hash — which should I use?

Use hashes for static inline blocks that rarely change; they are fixed in the header and need no server work. Use a fresh nonce for every response when inline code is dynamic, and put the same value in the header and on each element. Nonces require per-response generation and must not live in cached HTML; a nonce cannot be verified for freshness from its value alone.

Why don't style hashes work everywhere?

An element hash covers the content of a <style> element, not a style="…" attribute. Authorising attribute values requires style-src-attr with 'unsafe-hashes', and browser support for inline style hashes has gaps. For the broadest compatibility use an external stylesheet or a nonce on the style element.

Can I verify a nonce I found in a CSP header?

Only its shape and entropy can be checked locally: the tool decodes the base64 value and reports whether it carries at least 16 random bytes, as CSP requires. Randomness and freshness cannot be proven from the pasted value — generate a new nonce for each response.

Is my inline code uploaded or stored?

No. Hashing uses WebCrypto inside the tab, the nonce comes from crypto.getRandomValues, and there are no network requests or persistent storage. The recent-hash list lives only in the current page session.

References & standards