Skip to content

Random

The rite.crypto.random submodule provides cryptographically secure random generators built on Python's secrets and os.urandom.

Random Module

Cryptographically secure random generation.

This submodule provides secure random generation using Python's secrets module for cryptographic purposes.

Examples

from rite.crypto.random import random_bytes, random_hex len(random_bytes(16)) 16 len(random_hex(16)) 32

Modules

random_bytes

Random Bytes Generation

Generate cryptographically secure random bytes.

Examples

from rite.crypto.random import random_bytes len(random_bytes(16)) 16 len(random_bytes(32)) 32

Functions

random_bytes(nbytes: int = 32) -> bytes

Generate cryptographically secure random bytes.

Parameters:

Name Type Description Default
nbytes int

Number of random bytes to generate.

32

Returns:

Type Description
bytes

Random bytes of specified length.

Examples:

>>> len(random_bytes())
32
>>> len(random_bytes(16))
16
>>> isinstance(random_bytes(8), bytes)
True

random_choice

Random Choice Selection

Cryptographically secure random choice from sequence.

Examples

from rite.crypto.random import random_choice random_choice(['a', 'b', 'c']) in ['a', 'b', 'c'] True random_choice("ABC") in "ABC" True

Functions

random_choice(seq: Sequence[T]) -> T

Choose random element from sequence (cryptographically secure).

Parameters:

Name Type Description Default
seq Sequence[T]

Non-empty sequence to choose from.

required

Returns:

Type Description
T

Randomly selected element.

Raises:

Type Description
IndexError

If sequence is empty.

Examples:

>>> random_choice(['a', 'b', 'c']) in ['a', 'b', 'c']
True
>>> random_choice("XYZ") in "XYZ"
True
>>> random_choice([1, 2, 3]) in [1, 2, 3]
True

random_hex

Random Hex String Generation

Generate cryptographically secure random hex strings.

Examples

from rite.crypto.random import random_hex len(random_hex(16)) 32 len(random_hex(32)) 64

Functions

random_hex(nbytes: int = 32) -> str

Generate cryptographically secure random hex string.

Parameters:

Name Type Description Default
nbytes int

Number of random bytes (output is 2x this length).

32

Returns:

Type Description
str

Random hexadecimal string.

Examples:

>>> len(random_hex())
64
>>> len(random_hex(16))
32
>>> all(c in '0123456789abcdef' for c in random_hex(8))
True

random_int

Random Integer Generation

Generate cryptographically secure random integers.

Examples

from rite.crypto.random import random_int 0 <= random_int(0, 100) <= 100 True random_int(1, 6) in range(1, 7) True

Functions

random_int(a: int, b: int) -> int

Generate cryptographically secure random integer in range [a, b].

Parameters:

Name Type Description Default
a int

Lower bound (inclusive).

required
b int

Upper bound (inclusive).

required

Returns:

Type Description
int

Random integer between a and b (inclusive).

Examples:

>>> n = random_int(1, 10)
>>> 1 <= n <= 10
True
>>> random_int(0, 0)
0
>>> n = random_int(100, 200)
>>> 100 <= n <= 200
True

random_urlsafe

Random URL-Safe String Generation

Generate cryptographically secure URL-safe random strings.

Examples

from rite.crypto.random import random_urlsafe len(random_urlsafe(16)) 24 len(random_urlsafe(32)) 43

Functions

random_urlsafe(nbytes: int = 32) -> str

Generate cryptographically secure URL-safe string.

Parameters:

Name Type Description Default
nbytes int

Number of random bytes (output length varies).

32

Returns:

Type Description
str

Random URL-safe base64 string.

Examples:

>>> token = random_urlsafe()
>>> len(token) > 40
True
>>> token = random_urlsafe(16)
>>> '-' not in token or '_' not in token
True
Notes

Output uses base64url encoding (A-Z, a-z, 0-9, -, _). Length is approximately 4/3 of nbytes.

options: show_root_heading: true show_source: false heading_level: 2