Skip to main content
Module

x/cstring/mod.js

Generate cryptographically random strings with ease.
Latest
File
/** * @param {Number} length - The length of the string.*/export default function cstring(length) { if (!length) throw 'missing string length'
if (!Number.isInteger(length)) throw 'string length must be an integer'
let string = '', values = crypto.getRandomValues(new Uint8Array(length))
for (; length--;) { const _ = 63 & values[length]
string += (_ < 36 ? _.toString(36) : _ < 62 ? (_ - 26).toString(36).toUpperCase() : _ < 63 ? '_' : '-') }
return string}