1. Home
  2. Developer tools
  3. CSP Hash Generator

CSP Hash Generator

Allow the few inline scripts a page really needs without falling back to unsafe-inline, which would let injected scripts run too.

Free, no sign-up. Runs in your browser: nothing is uploaded.

How to use CSP Hash Generator

  1. Paste the final HTML of the page, or one script’s exact content.
  2. Choose the hash algorithm.
  3. Copy the hash sources or the whole policy header.

Features

  • Finds inline <script>, <style> and on* handlers; skips src scripts and JSON-LD
  • SHA-256, SHA-384 and SHA-512 with Web Crypto
  • Policy header with script-src and style-src filled in
  • ‘unsafe-hashes’ added only when event handlers need it
  • Random 128-bit nonce for server-rendered pages

Supported formats

Input

  • HTML
  • JS

You get

  • Copied text

Private by design

Everything you enter is processed by your browser on this device. Nothing is sent to openWorkstand or anyone else, so the CSP Hash Generator is safe for private and work data. After you have used it once while online, it also works without an internet connection. How local processing works

Good to know

  • Hashes must be recomputed whenever a script or style changes by even one character.

Guide

Allowing inline scripts in a Content-Security-Policy with hashes

A Content-Security-Policy tells the browser which scripts and styles a page may run, which is the strongest defence against cross-site scripting: even if an attacker injects a <script> tag, the browser refuses to execute it. Many sites weaken that protection with 'unsafe-inline' because they have a few legitimate inline scripts. Hash sources allow exactly those scripts and nothing else. This tool finds the inline code in a page and computes the hashes.

Why unsafe-inline is a problem

'unsafe-inline' allows every inline script on the page, including the ones an attacker manages to inject, so a policy that contains it offers little protection against XSS. Listing a hash for each legitimate inline block lets the browser run those blocks and block any other inline code.

How a hash source works

A hash source is the base64-encoded SHA-256, SHA-384 or SHA-512 digest of the script’s exact text, written in quotes with the algorithm name, for example 'sha256-…'. When the browser meets an inline script, it hashes the text between the opening and closing tags and runs the script only if that hash is in the policy. Every character counts: spaces, indentation and line breaks are part of the input, and the text is encoded as UTF-8 first.

What the extractor finds

In HTML mode, the tool scans the page for inline <script> blocks, <style> blocks and event-handler attributes such as onclick="…". Scripts with a src attribute are skipped because they are allowed by origin, not by hash, and so are data blocks such as JSON-LD, import maps and templates, which browsers do not execute. Each block is listed with a preview and its hash, and the policy header below is filled in with them. In raw mode, paste a single script’s content to hash only that.

Event handlers and unsafe-hashes

Inline handlers are a special case. Since CSP Level 3, they can be allowed by hash only if the policy also contains 'unsafe-hashes', and the tool adds it when it finds handlers. That keyword re-enables a class of risk, so moving handlers into a script file with addEventListener is the better long-term fix.

Nonces as the alternative

For pages rendered by a server, a nonce is often easier: generate a fresh random value per response, put it in the header as 'nonce-…' and on each script tag as nonce="…". With 'strict-dynamic', scripts loaded by a trusted script are trusted too. The nonce button produces 128 random bits from the browser’s cryptographic generator, but a nonce only protects anything when it changes on every response, so it cannot be used in static HTML.

A worked example

The Sample page has an inline theme script, an inline style block, an external app.js, a JSON-LD block and a button with an onclick handler. The tool finds three items to hash: the script, the style and the handler. The policy reads default-src 'self'; script-src 'self' 'sha256-kKM2oKS6VUDHax3JAyy1x+3NH0h1NqCONQq6jVM5FUw=' 'unsafe-hashes' 'sha256-LdlORHyUW/rwezK0l13nW+IwcZmi78eWOCBjewMWRr4='; style-src 'self' 'sha256-VtY8xt///Rl0jNNqbGR9GPpkyX6goq/uXIfvOYgJ5GU=', followed by object-src 'none', base-uri 'self' and frame-ancestors 'self'. The external script and the JSON-LD block need no hash.

When hashes break

Because the hash covers every byte, any change to the script, even reformatting by a minifier, a template engine adding whitespace or a CMS rewriting quotes, produces a new hash and the browser blocks the old script. Hash the final HTML as it is served, and regenerate the policy whenever an inline block changes; build steps can automate this.

Checked against the current version of the CSP Hash Generator on .

Frequently asked questions

How do I test a policy without breaking the site?

Send it as Content-Security-Policy-Report-Only first. Browsers then report violations in the console, and to a report endpoint if you configure one, without blocking anything. Switch to the enforcing header once the reports are clean.

Do hashes work for external script files?

CSP Level 3 allows hashes to match external scripts that carry a matching integrity attribute, but support is uneven. For files from your own origin, self is simpler; for third-party files, list their origin or use Subresource Integrity on the tag.

Why does my hash not match after deploying?

The hash covers every character between the tags, including indentation and the final newline. A minifier, a template engine or a CMS that changes one space produces a different hash, so hash the HTML exactly as it is served.

Hash or nonce: which should I use?

Hashes suit static sites and cached pages because they never change. Nonces suit server-rendered pages: a new random value per response, which also covers scripts whose content changes per request.

Keep working with the result

openWorkstand also includes a free workspace with documents, spreadsheets, tasks, projects and a calendar that save to a folder on your computer.