hbutils.random.binary

This module provides utilities for generating random bytes with customizable options.

The module offers functionality to generate random byte sequences with control over length, zero byte inclusion, and the random number generator used.

random_bytes

hbutils.random.binary.random_bytes(length: int = 32, allow_zero: bool = False, rnd: Random | None = None) bytes[source]

Generate random bytes with customizable options.

This function generates a sequence of random bytes with specified length. It provides options to control whether zero bytes (0x00) are allowed and which random number generator to use.

Parameters:
  • length (int) – Length of the byte sequence to generate, defaults to 32.

  • allow_zero (bool) – Whether to allow 0x00 to appear in the result. If False, only bytes from 0x01 to 0xFF will be generated. Defaults to False.

  • rnd (Optional[random.Random]) – Random object to use for generation. If None, uses the default system random number generator.

Returns:

A sequence of random bytes with the specified length.

Return type:

bytes

Examples::
>>> from hbutils.random import random_bytes
>>> random_bytes()
b'\xdc\xdc$\x05\x83\x04\x812\xfd\xda^\x7f[{\xbc\x99\x88*\xab3\x87}\xd1\xab\xddc\xa2p\xb2\xcb\x07\xc5'
>>> random_bytes(64)
b"i7\x98\xd5\x81\x1d\xdb\xd8\xe1^\xf2\xe4\xbf\xe0O^\xeb\xed\xb0i\xaa\xf3\x16Jx\xf7J\xd7\xae1\x81\xc6\xad\xd21\x15\x8aX\xb6\xc7\x85\xa4\x1c{\xac^6\xdf\x03\x94kR}\x91\x96\xfe\x06{'I\xed5\x03r"
>>> random_bytes(16, allow_zero=True)  # May contain 0x00 bytes
b'\x00\x45\x8a\x00\xf3\x12\x67\x00\xab\xcd\xef\x00\x11\x22\x33\x44'