API reference › @evolu/common › Crypto › RandomBytes

Defined in: packages/common/src/Crypto.ts:33

Cryptographically secure random bytes with length-branded results.

Methods

create()

Call Signature

create(bytesLength: 16): Uint8Array<ArrayBufferLike> & Brand<"Entropy"> & Brand<"Length16">;

Defined in: packages/common/src/Crypto.ts:79

Creates cryptographically secure random bytes with type-safe length branding.

Uses the operating system's cryptographically secure random number generator (crypto.getRandomValues) to generate high-quality entropy suitable for cryptographic operations.

Type Safety

Returns specific branded types for common sizes:

  • Entropy16 for 16-byte values (128 bits)
  • Entropy24 for 24-byte values (192 bits)
  • Entropy32 for 32-byte values (256 bits)
  • Entropy64 for 64-byte values (512 bits)
  • Entropy for any other size

Example

import {
  assertEqual,
  assertType,
  createRandomBytes,
  type Entropy16,
  type Entropy24,
  type Entropy32,
  type Entropy64,
} from "@evolu/common";

const randomBytes = createRandomBytes();
const nonce16 = randomBytes.create(16);
const nonce24 = randomBytes.create(24);
const key = randomBytes.create(32);
const seed = randomBytes.create(64);
const custom = randomBytes.create(48);

assertType<typeof nonce16, Entropy16>();
assertType<typeof nonce24, Entropy24>();
assertType<typeof key, Entropy32>();
assertType<typeof seed, Entropy64>();
assertEqual(custom.length, 48);

Call Signature

create(bytesLength: 24): Uint8Array<ArrayBufferLike> & Brand<"Entropy"> & Brand<"Length24">;

Defined in: packages/common/src/Crypto.ts:80

Call Signature

create(bytesLength: 32): Uint8Array<ArrayBufferLike> & Brand<"Entropy"> & Brand<"Length32">;

Defined in: packages/common/src/Crypto.ts:81

Call Signature

create(bytesLength: 64): Uint8Array<ArrayBufferLike> & Brand<"Entropy"> & Brand<"Length64">;

Defined in: packages/common/src/Crypto.ts:82

Call Signature

create(bytesLength: number): Uint8Array<ArrayBufferLike> & Brand<"Entropy">;

Defined in: packages/common/src/Crypto.ts:83