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
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_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
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:
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