iso-web
Version:
Isomorphic web apis utilities for fetch, event target, signals, crypto and doh.
21 lines (18 loc) • 497 B
JavaScript
/**
* Isomorphic web crypto and random bytes exports
*
* @module
*/
import crypto from 'crypto'
export const webcrypto = /** @type {Crypto} */ (crypto.webcrypto)
/**
* Secure PRNG - Random bytes from webcrypto
*
* @param {number} length - The length of the random bytes
*/
export function randomBytes(length = 32) {
if (crypto.webcrypto) {
return crypto.webcrypto.getRandomValues(new Uint8Array(length))
}
throw new Error("The environment doesn't have randomBytes function")
}