How to generate a nonce in node.js? How to generate a nonce in node.js? node.js node.js

How to generate a nonce in node.js?


Just to confirm that indeed this does work in NodeJS for CSP nonces

const crypto = require('crypto');let nonce = crypto.randomBytes(16).toString('base64');


I suggest using uuid for this: https://www.npmjs.com/package/uuid

Each uuid is exactly 16 bytes (128 bits) as desired, and you have a higher probability of your computer being hit by a meteor than generating a uuid collision.