sha384.js 744 B

123456789101112131415161718192021222324252627282930
  1. const crypto = require('crypto');
  2. const hash = crypto.createHash('sha384');
  3. function generateCSPHash(script) {
  4. const data = hash.update(script, 'utf-8');
  5. return (
  6. 'sha384-' +
  7. data
  8. .digest('base64')
  9. .replace(/=/g, '')
  10. .replace(/\+/g, '-')
  11. .replace(/\//g, '_')
  12. );
  13. }
  14. script = " \
  15. const button = document.querySelector('#hello2'); \
  16. button.addEventListener('click', () => { \
  17. const h1 = document.createElement('h1'); \
  18. h1.textContent = 'Hello World 2!'; \
  19. document.body.appendChild(h1); \
  20. }); \
  21. ";
  22. var http = require('http');
  23. http.createServer(function (req, res) {
  24. res.writeHead(200, {'Content-Type': 'text/html'});
  25. res.end(generateCSPHash(script));
  26. }).listen(8080);