Skip to content

Instantly share code, notes, and snippets.

@deangrant
Created December 10, 2022 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deangrant/776dcff7052ae7c14e2d2beb229cfa5d to your computer and use it in GitHub Desktop.
Save deangrant/776dcff7052ae7c14e2d2beb229cfa5d to your computer and use it in GitHub Desktop.
Generate a hash to allow the execution of inline scripts in a Content Security Policy (CSP)
# The below code snippet will output the expected hash of the script in the console
# error message to use as a hash in a Content Security Policy (CSP). The CSP Level
# 2 specification allows, sha256, sha384, and sha512 hash algorithms.
#
# Example:
#
# echo -n 'doSomething();' | openssl sha256 -binary | openssl base64
# RFWPLDbv2BY+rCkDzsE+0fr8ylGr2R2faWMhq4lfEQc=
#
# Add the following to the script-src directive for your Content Security Policy.
# script-src 'sha256-RFWPLDbv2BY+rCkDzsE+0fr8ylGr2R2faWMhq4lfEQc=';
echo -n '{{ script }}' | openssl {{ hash_algorithm }} -binary | openssl base64
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment