Skip to content

Instantly share code, notes, and snippets.

@ngbrown
Last active November 30, 2023 16:28
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 ngbrown/a8de4e3bc05a610a634d05e03864c679 to your computer and use it in GitHub Desktop.
Save ngbrown/a8de4e3bc05a610a634d05e03864c679 to your computer and use it in GitHub Desktop.
Print CSP Hash
function _arrayBufferToBase64(buffer) {
let binary = '';
const bytes = new Uint8Array(buffer);
for (var i = 0; i < bytes.byteLength; i++) {
binary += String.fromCharCode(bytes[i]);
}
return window.btoa(binary);
}
function getCspDigest(code) {
crypto.subtle.digest('SHA-256', new TextEncoder().encode(code)).then(function(val){
const digest = ["sha256", _arrayBufferToBase64(val)].join('-');
console.log(`The digest for your script is: "'${digest}'"`);
});
}
getCspDigest(r);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment