Last active
November 30, 2023 16:28
-
-
Save ngbrown/a8de4e3bc05a610a634d05e03864c679 to your computer and use it in GitHub Desktop.
Print CSP Hash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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