Cross-platform cryptographically secure random bytes generation for Node.js and browsers
npx @tessl/cli install tessl/npm-randombytes@2.1.0randombytes provides cryptographically secure random byte generation that works identically in both Node.js and browser environments. It offers a simple, unified API that transparently uses Node.js crypto.randomBytes or browser Web Crypto API depending on the runtime environment.
npm install randombytesconst randomBytes = require('randombytes');For ES modules:
import randomBytes from 'randombytes';const randomBytes = require('randombytes');
// Generate 16 random bytes synchronously
const bytes = randomBytes(16);
console.log(bytes); // <Buffer a1 b2 c3 ...>
// Generate 32 random bytes asynchronously
randomBytes(32, (err, bytes) => {
if (err) throw err;
console.log(bytes); // <Buffer d4 e5 f6 ...>
});Generates cryptographically secure random bytes with both synchronous and asynchronous interfaces.
/**
* Generate cryptographically secure random bytes
* @param {number} size - Number of bytes to generate (0 to 4294967295)
* @param {function} [callback] - Optional callback function (err, bytes)
* @returns {Buffer} Random bytes buffer (sync mode) or undefined (async mode)
* @throws {RangeError} When size > 4294967295
* @throws {Error} When size is negative or non-numeric
* @throws {Error} In unsupported browsers
*/
function randomBytes(size, callback);Parameters:
size (number): Number of random bytes to generate
RangeError for values exceeding maximumOptional Parameters:
callback (function): Callback with signature (err, bytes)
undefinedBufferReturn Value:
Buffer containing random bytesundefined, passes result via callback as (null, Buffer)Error Handling:
RangeError: Thrown when size exceeds 4,294,967,295Error: Thrown for negative size or non-numeric size parameterError: In browsers lacking Web Crypto API support with message: "Secure random number generation is not supported by this browser. Use Chrome, Firefox or Internet Explorer 11"Platform Implementation:
crypto.randomBytescrypto.getRandomValues() or legacy msCrypto.getRandomValues()Usage Examples:
const randomBytes = require('randombytes');
// Synchronous usage
const syncBytes = randomBytes(16);
console.log('Sync bytes:', syncBytes.toString('hex'));
// Asynchronous usage
randomBytes(32, (err, asyncBytes) => {
if (err) {
console.error('Error generating bytes:', err);
return;
}
console.log('Async bytes:', asyncBytes.toString('hex'));
});
// Large byte generation (automatically chunked in browsers)
const largeBytes = randomBytes(100000);
console.log('Generated', largeBytes.length, 'bytes');
// Error handling for invalid inputs
try {
randomBytes(-1); // Throws error
} catch (err) {
console.error('Invalid size:', err.message);
}
try {
randomBytes(4294967296); // Throws RangeError
} catch (err) {
console.error('Size too large:', err.message);
}randombytes requires a modern browser with Web Crypto API support:
In unsupported browsers, the function throws an error with guidance to upgrade to a supported browser.