or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

asymmetric-cryptography.mddigital-signatures.mdhash-functions.mdhmac.mdindex.mdkey-derivation.mdkey-exchange.mdrandom-generation.mdsymmetric-encryption.md
tile.json

random-generation.mddocs/

Random Number Generation

Cryptographically secure random number generation for initialization vectors, keys, nonces, and other security-critical random data.

Capabilities

Random Bytes Generation

Generates cryptographically secure random bytes using the system's entropy source.

/**
 * Generate cryptographically secure random bytes
 * @param {number} size - Number of bytes to generate
 * @returns {Buffer} Buffer containing random bytes
 */
function randomBytes(size);

// Aliases for randomBytes
const rng = randomBytes;
const pseudoRandomBytes = randomBytes;
const prng = randomBytes;

Usage Examples:

const crypto = require('crypto-browserify');

// Generate 16 random bytes for an IV
const iv = crypto.randomBytes(16);
console.log(iv.toString('hex')); // e.g., "a1b2c3d4e5f6789012345678901234ab"

// Generate a random key
const key = crypto.randomBytes(32);

// Use alias functions
const randomData = crypto.rng(8);
const nonce = crypto.prng(12);

Random Fill

Fills an existing buffer with random data, either synchronously or asynchronously.

/**
 * Fill buffer with random data asynchronously
 * @param {Buffer} buffer - Buffer to fill with random data
 * @param {function} callback - Callback function called with error and filled buffer
 */
function randomFill(buffer, callback);

/**
 * Fill buffer with random data synchronously
 * @param {Buffer} buffer - Buffer to fill with random data
 * @returns {Buffer} The filled buffer
 */
function randomFillSync(buffer);

Usage Examples:

const crypto = require('crypto-browserify');

// Asynchronous random fill
const buffer = Buffer.alloc(16);
crypto.randomFill(buffer, (err, buf) => {
  if (err) throw err;
  console.log('Filled buffer:', buf.toString('hex'));
});

// Synchronous random fill
const syncBuffer = Buffer.alloc(8);
crypto.randomFillSync(syncBuffer);
console.log('Sync filled:', syncBuffer.toString('hex'));

// Fill existing buffer with specific data
const existingBuffer = Buffer.from('0000000000000000', 'hex');
crypto.randomFillSync(existingBuffer);
console.log('Overwritten:', existingBuffer.toString('hex'));

Error Handling

Random generation functions may throw errors in the following scenarios:

  • Invalid size parameter: When size is not a positive number or exceeds system limits
  • System entropy issues: When the system's random number generator is unavailable
  • Buffer allocation errors: When there's insufficient memory to allocate the random buffer
try {
  const randomData = crypto.randomBytes(1024 * 1024); // Large allocation
} catch (err) {
  console.error('Random generation failed:', err.message);
}