UNPKG

contingent

Version:

Create cryptographically-strong random numbers in node.js or the browser

35 lines (34 loc) 885 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function randomBytes(n) { const crypto = window.crypto || window.msCrypto; if (crypto == null) { throw new Error('crypto not found for randomBytes'); } const uint8 = crypto.getRandomValues(new Uint8Array(n)); return new DataView(uint8.buffer); } exports.randomBytes = randomBytes; function randomBit() { return randomBytes(1).getUint8(0) % 2 === 0; } exports.randomBit = randomBit; function randomByte() { return randomBytes(1).getUint8(0); } exports.randomByte = randomByte; function randomInt() { return randomBytes(4).getInt32(0); } exports.randomInt = randomInt; function randomUint() { return randomBytes(4).getUint32(0); } exports.randomUint = randomUint; exports.default = { randomBytes, randomBit, randomByte, randomInt, randomUint, };