Content Security Policy Hash & Nonce Generator
Generate CSP hashes and nonce values locally, reduce reliance on
'unsafe-inline', and review common CSP weaknesses.
Generator and analyzer
Introduction
Content Security Policy helps reduce cross-site scripting and injection risk. However,
many policies rely on 'unsafe-inline' because inline scripts or styles
are hard to manage.
Hashes and nonces provide safer ways to allow specific inline content without permitting all inline execution.
How it works
The hash generator uses the browser’s Web Crypto API to compute SHA-256, SHA-384, or SHA-512 hashes from the exact content you provide. The nonce generator uses cryptographically secure random values. The analyzer parses CSP directives and checks for common risky patterns.
- No content is sent to a server.
- Hashes are computed locally.
- Nonces are generated locally.
- CSP analysis is heuristic and advisory.
How to use
- Paste the exact inline script or style content.
- Select the content type and hash algorithm.
- Generate and copy the CSP hash source.
- Add the hash to the appropriate CSP directive.
- Alternatively, generate a nonce and apply it to inline elements.
- Analyze your full CSP for common weaknesses.
Practical example
If you have a small inline analytics script, you can hash the exact script contents and
add the resulting 'sha256-...' value to script-src. Then you
can remove 'unsafe-inline' from that directive.
Use cases
- Migrating away from
'unsafe-inline'. - Allowing small inline bootstrap scripts.
- Securing critical inline styles.
- Auditing CSP before deployment.
- Preparing security documentation.
Best practices
- Use nonces for dynamic inline content where possible.
- Use hashes for stable inline snippets.
- Avoid reusing nonces across requests.
- Keep hash inputs identical to deployed content.
- Combine CSP with secure cookies, input validation, and framework protections.
Common mistakes
- Hashing formatted code that differs from production whitespace.
- Reusing nonces across page loads.
- Leaving
'unsafe-inline'alongside hashes or nonces. - Using wildcard script sources.
- Deploying CSP changes without testing in report-only mode.
Limitations
- The analyzer does not fetch live headers.
- It does not simulate browser enforcement fully.
- Some directives are experimental or browser-specific.
- CSP compatibility depends on your application’s scripts, styles, and third parties.
Browser compatibility
Web Crypto hash generation requires a secure context such as HTTPS or localhost. CSP support varies by browser and directive, so test target environments carefully.
FAQ
Is my inline code uploaded?
No. Hashing and analysis happen locally in your browser.
Should I use a hash or a nonce?
Use hashes for stable inline content. Use nonces for content that changes per request or is generated server-side.
Can I reuse a nonce?
No. Nonces should be unique per response. Reusing nonces weakens the protection.
Does this tool guarantee XSS protection?
No. CSP is one layer of defense. Secure coding, input validation, output encoding, and framework protections remain essential.