Secure Random Bytes Generator

Cryptographically secure pseudo-random byte generator using ChaCha20.

This utility produces an arbitrary number of cryptographically strong random bytes, suitable for:

  • Nonces

  • Encryption keys

  • Session tokens

  • Salts

  • Any security-sensitive randomness

The generator is implemented as a singleton so that the internal ChaCha20 stream cipher state is preserved across calls. This avoids reseeding and ensures consistent forward-secure random output.


Constructor- init()

Initializes the internal ChaCha20 stream cipher using a randomly generated 256-bit key and 128-bit nonce. The cipher is kept as a persistent encryptor object so that successive calls to next() continue the stream, ensuring cryptographically strong forward-secure random output.

Methods

next(number_of_bytes)

Generate the next number_of_bytes pseudo-random bytes from the ChaCha20 stream.

Parameters
  • number_of_bytes (int) Number of random bytes to produce.

Returns
  • bytes — A bytes object containing the requested number of cryptographically secure random bytes.

·

©

2026

·

©

2026