Skip to content

Crypto Module

The rite.crypto module provides cryptographic operations including hashing, ciphers, UUIDs, and secure random generation.

Overview

crypto

Cryptography Module

Comprehensive cryptographic operations for hashing, encryption, random generation, and UUIDs.

This module provides four main categories:

  1. Hashing (hash submodule)
  2. MD5, SHA-1, SHA-2 (256, 384, 512)
  3. SHA-3 (256, 512)
  4. BLAKE2b, BLAKE2s
  5. HMAC message authentication

  6. Ciphers (cipher submodule)

  7. Classical ciphers: Caesar, Vigenère, Atbash, ROT13
  8. Transposition ciphers: Rail Fence, Scytale
  9. Substitution ciphers: Playfair, Four Square, Baconian
  10. Modern: XOR, Autokey

  11. Random (random submodule)

  12. Cryptographically secure random bytes, hex, strings
  13. Random integers and choices

  14. UUID (uuid submodule)

  15. UUID generation and validation
  16. UUID version detection

Examples

from rite.crypto import hash_sha256, random_hex hash_sha256("hello")[:16] '2cf24dba5fb0a30e' len(random_hex(16)) 32

Functions

decode_atbash_cipher

decode_atbash_cipher(text: str) -> str

Decode text using the Atbash cipher.

This is functionally identical to encoding, since Atbash is symmetric.

Parameters:

Name Type Description Default
text str

The Atbash-encoded string.

required

Returns:

Type Description
str

The decoded string (same as encoding).

decode_autokey_cipher

decode_autokey_cipher(encoded_text: str, key: str) -> str

Decode text using the Autokey cipher.

Parameters:

Name Type Description Default
encoded_text str

The ciphertext to decode.

required
key str

The original cipher key.

required

Returns:

Type Description
str

Decoded plaintext.

decode_baconian_cipher

decode_baconian_cipher(encoded_text: str) -> str

Decode Baconian-encoded text (a/b) back into plaintext.

Ignores incomplete final segments and assumes input is clean (only 'a' and 'b').

Parameters:

Name Type Description Default
encoded_text str

A string of 'a' and 'b' characters, in chunks of 5.

required

Returns:

Type Description
str

Decoded plaintext.

decode_caesar_cipher

decode_caesar_cipher(encoded_text: str, shift: int) -> str

Decode Caesar cipher text by reversing the shift.

Parameters:

Name Type Description Default
encoded_text str

The ciphertext to decode.

required
shift int

The original shift used during encoding.

required

Returns:

Type Description
str

Decoded plaintext string.

decode_four_square_cipher

decode_four_square_cipher(encoded_text: str, key1: str, key2: str) -> str

Decode ciphertext encoded with the Four-Square cipher.

Parameters:

Name Type Description Default
encoded_text str

The encrypted message to decode.

required
key1 str

Key used for the top-right square.

required
key2 str

Key used for the bottom-left square.

required

Returns:

Type Description
str

The decoded plaintext.

decode_playfair_cipher

decode_playfair_cipher(encoded_text: str, key: str) -> str

Decode Playfair-encoded ciphertext.

Parameters:

Name Type Description Default
encoded_text str

The encoded string.

required
key str

Cipher key used during encoding.

required

Returns:

Type Description
str

Decoded plaintext (not automatically de-padded).

decode_rail_fence_cipher

decode_rail_fence_cipher(encoded_text: str, num_rails: int) -> str

Decode a Rail Fence-encoded string.

Parameters:

Name Type Description Default
encoded_text str

The encoded string to decode.

required
num_rails int

Number of rails used during encoding.

required

Returns:

Type Description
str

The original decoded plaintext string.

decode_rot13_cipher

decode_rot13_cipher(text: str) -> str

Decode text using the Rot13 cipher.

This is functionally identical to encoding, as Rot13 is symmetric.

Parameters:

Name Type Description Default
text str

The Rot13-encoded text.

required

Returns:

Type Description
str

The decoded text.

decode_scytale_cipher

decode_scytale_cipher(encoded_text: str, diameter: int) -> str

Decode a Scytale-encoded string.

Parameters:

Name Type Description Default
encoded_text str

The ciphertext to decode.

required
diameter int

Number of columns used during encoding.

required

Returns:

Type Description
str

Decoded plaintext string.

decode_transposition_cipher

decode_transposition_cipher(encoded_text: str, key: int, strip_padding: bool = True) -> str

Decode text from a simple columnar transposition cipher.

Parameters:

Name Type Description Default
encoded_text str

The transposed text to decode.

required
key int

The number of columns used in encoding.

required
strip_padding bool

Whether to strip trailing padding spaces (default: True).

True

Returns:

Type Description
str

The decoded (original) text.

decode_vigenere_cipher

decode_vigenere_cipher(encoded_text: str, key: str) -> str

Decode text from the Vigenère cipher.

Non-alphabet characters are preserved.

Parameters:

Name Type Description Default
encoded_text str

The ciphertext to decode.

required
key str

The keyword originally used to encode.

required

Returns:

Type Description
str

The decoded plaintext.

decode_xor_cipher

decode_xor_cipher(text: str, key: str) -> str

Decode XOR-encoded text using the same key.

This function is equivalent to encode_xor_cipher().

Parameters:

Name Type Description Default
text str

The XOR-encoded string.

required
key str

The encryption key used for encoding.

required

Returns:

Type Description
str

The decoded plaintext string.

encode_atbash_cipher

encode_atbash_cipher(text: str) -> str

Encode (or decode) text using the Atbash cipher.

Since Atbash is symmetric, the same function can be used for encoding and decoding.

Parameters:

Name Type Description Default
text str

Input string to encode or decode.

required

Returns:

Type Description
str

The Atbash-encoded/decoded string.

encode_autokey_cipher

encode_autokey_cipher(text: str, key: str) -> str

Encode text using the Autokey cipher.

Parameters:

Name Type Description Default
text str

The plaintext to encode.

required
key str

The cipher key (only letters are used).

required

Returns:

Type Description
str

Encoded ciphertext.

encode_baconian_cipher

encode_baconian_cipher(text: str) -> str

Encode plaintext using the Baconian cipher.

Non-alphabetic characters are ignored. All letters are converted to lowercase.

Parameters:

Name Type Description Default
text str

Input text to encode.

required

Returns:

Type Description
str

A string of 'a' and 'b' representing the encoded text.

encode_caesar_cipher

encode_caesar_cipher(text: str, shift: int) -> str

Encode text using the Caesar cipher.

Only alphabetic characters are shifted. Case is preserved. Other characters remain unchanged.

Parameters:

Name Type Description Default
text str

The plaintext string to encode.

required
shift int

The number of positions to shift (can be positive or negative).

required

Returns:

Type Description
str

Encoded ciphertext string.

encode_four_square_cipher

encode_four_square_cipher(text: str, key1: str, key2: str) -> str

Encode text using the Four-Square cipher.

Parameters:

Name Type Description Default
text str

The plaintext to encode.

required
key1 str

Key for the top-right square.

required
key2 str

Key for the bottom-left square.

required

Returns:

Type Description
str

The encoded ciphertext.

encode_playfair_cipher

encode_playfair_cipher(text: str, key: str) -> str

Encode plaintext using the Playfair cipher.

Parameters:

Name Type Description Default
text str

Input plaintext.

required
key str

Cipher key for generating the Playfair square.

required

Returns:

Type Description
str

Encoded ciphertext.

encode_rail_fence_cipher

encode_rail_fence_cipher(text: str, num_rails: int) -> str

Encode text using the Rail Fence cipher.

Parameters:

Name Type Description Default
text str

The plaintext to encode.

required
num_rails int

Number of rails (rows) to use in the zigzag pattern.

required

Returns:

Type Description
str

Encoded ciphertext string.

encode_rot13_cipher

encode_rot13_cipher(text: str) -> str

Encode or decode text using the Rot13 cipher.

Since Rot13 is symmetric, this function is used for both encoding and decoding.

Parameters:

Name Type Description Default
text str

The input text to encode or decode.

required

Returns:

Type Description
str

The transformed text.

encode_scytale_cipher

encode_scytale_cipher(text: str, diameter: int) -> str

Encode a string using the Scytale cipher.

Parameters:

Name Type Description Default
text str

The input plaintext.

required
diameter int

Number of columns (characters per wrap-around).

required

Returns:

Type Description
str

Encoded ciphertext.

encode_transposition_cipher

encode_transposition_cipher(text: str, key: int) -> str

Encode text using a simple columnar transposition cipher.

Parameters:

Name Type Description Default
text str

The plaintext to encode.

required
key int

The number of columns (i.e., transposition key).

required

Returns:

Type Description
str

The encoded (scrambled) text.

encode_vigenere_cipher

encode_vigenere_cipher(text: str, key: str) -> str

Encode text using the Vigenère cipher.

Non-alphabet characters are preserved.

Parameters:

Name Type Description Default
text str

The plaintext to encode.

required
key str

The keyword used to perform letter shifts.

required

Returns:

Type Description
str

The encoded ciphertext.

encode_xor_cipher

encode_xor_cipher(text: str, key: str) -> str

Encode or decode text using the XOR cipher.

Since XOR is symmetric, this function performs both encryption and decryption.

Parameters:

Name Type Description Default
text str

The plaintext or ciphertext string.

required
key str

The encryption key.

required

Returns:

Name Type Description
str

A string with the result of XOR encryption/decryption.

Note str

Non-printable characters may be present in the result.

four_square_cipher_pair

four_square_cipher_pair(pair: str, square_tl: list[list[str]], square_tr: list[list[str]], square_bl: list[list[str]], square_br: list[list[str]], mode: str = 'encode') -> str

Encode or decode a letter pair using the Four-Square cipher.

Parameters:

Name Type Description Default
pair str

A two-character string.

required
square_tl list[list[str]]

Top-left square (standard).

required
square_tr list[list[str]]

Top-right square (key 1).

required
square_bl list[list[str]]

Bottom-left square (key 2).

required
square_br list[list[str]]

Bottom-right square (standard).

required
mode str

'encode' or 'decode'.

'encode'

Returns:

Type Description
str

Encoded or decoded letter pair.

hash_sha256_hmac

hash_sha256_hmac(key: str | bytes, msg: str | bytes, encoding: str = 'utf-8') -> str

Compute SHA-256 HMAC of message with key.

Parameters:

Name Type Description Default
key str | bytes

Secret key for HMAC.

required
msg str | bytes

Message to authenticate.

required
encoding str

Text encoding if inputs are strings.

'utf-8'

Returns:

Type Description
str

Hexadecimal HMAC-SHA-256 string.

Examples:

>>> hmac_result = hash_sha256_hmac("secret", "message")
>>> len(hmac_result)
64

hash_sha512_hmac

hash_sha512_hmac(key: str | bytes, msg: str | bytes, encoding: str = 'utf-8') -> str

Compute SHA-512 HMAC of message with key.

Parameters:

Name Type Description Default
key str | bytes

Secret key for HMAC.

required
msg str | bytes

Message to authenticate.

required
encoding str

Text encoding if inputs are strings.

'utf-8'

Returns:

Type Description
str

Hexadecimal HMAC-SHA-512 string.

Examples:

>>> hmac_result = hash_sha512_hmac("secret", "message")
>>> len(hmac_result)
128

is_valid_uuid

is_valid_uuid(uuid_to_test: str, version: int = 4) -> bool

Check if uuid_to_test is a valid UUID.

Parameters:

Name Type Description Default
uuid_to_test str

String to validate as UUID

required
version int

UUID version to check (1, 2, 3, or 4)

4

Returns:

Type Description
bool

True if valid UUID, False otherwise

Example

is_valid_uuid('550e8400-e29b-41d4-a716-446655440000') True is_valid_uuid('invalid') False

Modules

cipher

Rite - Cryptography - Cipher Module

This module serves as the central import hub for all classical ciphers implemented in the Rite cryptography toolkit. It exposes both encode and decode functions for a variety of historical and educational ciphers.

Available ciphers
  • Atbash
  • Autokey
  • Baconian
  • Caesar
  • Four-Square
  • Playfair
  • Rail Fence
  • Rot13
  • Scytale
  • Transposition
  • Vigenère
  • XOR
References
Functions
decode_atbash_cipher
decode_atbash_cipher(text: str) -> str

Decode text using the Atbash cipher.

This is functionally identical to encoding, since Atbash is symmetric.

Parameters:

Name Type Description Default
text str

The Atbash-encoded string.

required

Returns:

Type Description
str

The decoded string (same as encoding).

decode_autokey_cipher
decode_autokey_cipher(encoded_text: str, key: str) -> str

Decode text using the Autokey cipher.

Parameters:

Name Type Description Default
encoded_text str

The ciphertext to decode.

required
key str

The original cipher key.

required

Returns:

Type Description
str

Decoded plaintext.

decode_baconian_cipher
decode_baconian_cipher(encoded_text: str) -> str

Decode Baconian-encoded text (a/b) back into plaintext.

Ignores incomplete final segments and assumes input is clean (only 'a' and 'b').

Parameters:

Name Type Description Default
encoded_text str

A string of 'a' and 'b' characters, in chunks of 5.

required

Returns:

Type Description
str

Decoded plaintext.

decode_caesar_cipher
decode_caesar_cipher(encoded_text: str, shift: int) -> str

Decode Caesar cipher text by reversing the shift.

Parameters:

Name Type Description Default
encoded_text str

The ciphertext to decode.

required
shift int

The original shift used during encoding.

required

Returns:

Type Description
str

Decoded plaintext string.

decode_four_square_cipher
decode_four_square_cipher(encoded_text: str, key1: str, key2: str) -> str

Decode ciphertext encoded with the Four-Square cipher.

Parameters:

Name Type Description Default
encoded_text str

The encrypted message to decode.

required
key1 str

Key used for the top-right square.

required
key2 str

Key used for the bottom-left square.

required

Returns:

Type Description
str

The decoded plaintext.

decode_playfair_cipher
decode_playfair_cipher(encoded_text: str, key: str) -> str

Decode Playfair-encoded ciphertext.

Parameters:

Name Type Description Default
encoded_text str

The encoded string.

required
key str

Cipher key used during encoding.

required

Returns:

Type Description
str

Decoded plaintext (not automatically de-padded).

decode_rail_fence_cipher
decode_rail_fence_cipher(encoded_text: str, num_rails: int) -> str

Decode a Rail Fence-encoded string.

Parameters:

Name Type Description Default
encoded_text str

The encoded string to decode.

required
num_rails int

Number of rails used during encoding.

required

Returns:

Type Description
str

The original decoded plaintext string.

decode_rot13_cipher
decode_rot13_cipher(text: str) -> str

Decode text using the Rot13 cipher.

This is functionally identical to encoding, as Rot13 is symmetric.

Parameters:

Name Type Description Default
text str

The Rot13-encoded text.

required

Returns:

Type Description
str

The decoded text.

decode_scytale_cipher
decode_scytale_cipher(encoded_text: str, diameter: int) -> str

Decode a Scytale-encoded string.

Parameters:

Name Type Description Default
encoded_text str

The ciphertext to decode.

required
diameter int

Number of columns used during encoding.

required

Returns:

Type Description
str

Decoded plaintext string.

decode_transposition_cipher
decode_transposition_cipher(encoded_text: str, key: int, strip_padding: bool = True) -> str

Decode text from a simple columnar transposition cipher.

Parameters:

Name Type Description Default
encoded_text str

The transposed text to decode.

required
key int

The number of columns used in encoding.

required
strip_padding bool

Whether to strip trailing padding spaces (default: True).

True

Returns:

Type Description
str

The decoded (original) text.

decode_vigenere_cipher
decode_vigenere_cipher(encoded_text: str, key: str) -> str

Decode text from the Vigenère cipher.

Non-alphabet characters are preserved.

Parameters:

Name Type Description Default
encoded_text str

The ciphertext to decode.

required
key str

The keyword originally used to encode.

required

Returns:

Type Description
str

The decoded plaintext.

decode_xor_cipher
decode_xor_cipher(text: str, key: str) -> str

Decode XOR-encoded text using the same key.

This function is equivalent to encode_xor_cipher().

Parameters:

Name Type Description Default
text str

The XOR-encoded string.

required
key str

The encryption key used for encoding.

required

Returns:

Type Description
str

The decoded plaintext string.

encode_atbash_cipher
encode_atbash_cipher(text: str) -> str

Encode (or decode) text using the Atbash cipher.

Since Atbash is symmetric, the same function can be used for encoding and decoding.

Parameters:

Name Type Description Default
text str

Input string to encode or decode.

required

Returns:

Type Description
str

The Atbash-encoded/decoded string.

encode_autokey_cipher
encode_autokey_cipher(text: str, key: str) -> str

Encode text using the Autokey cipher.

Parameters:

Name Type Description Default
text str

The plaintext to encode.

required
key str

The cipher key (only letters are used).

required

Returns:

Type Description
str

Encoded ciphertext.

encode_baconian_cipher
encode_baconian_cipher(text: str) -> str

Encode plaintext using the Baconian cipher.

Non-alphabetic characters are ignored. All letters are converted to lowercase.

Parameters:

Name Type Description Default
text str

Input text to encode.

required

Returns:

Type Description
str

A string of 'a' and 'b' representing the encoded text.

encode_caesar_cipher
encode_caesar_cipher(text: str, shift: int) -> str

Encode text using the Caesar cipher.

Only alphabetic characters are shifted. Case is preserved. Other characters remain unchanged.

Parameters:

Name Type Description Default
text str

The plaintext string to encode.

required
shift int

The number of positions to shift (can be positive or negative).

required

Returns:

Type Description
str

Encoded ciphertext string.

encode_four_square_cipher
encode_four_square_cipher(text: str, key1: str, key2: str) -> str

Encode text using the Four-Square cipher.

Parameters:

Name Type Description Default
text str

The plaintext to encode.

required
key1 str

Key for the top-right square.

required
key2 str

Key for the bottom-left square.

required

Returns:

Type Description
str

The encoded ciphertext.

encode_playfair_cipher
encode_playfair_cipher(text: str, key: str) -> str

Encode plaintext using the Playfair cipher.

Parameters:

Name Type Description Default
text str

Input plaintext.

required
key str

Cipher key for generating the Playfair square.

required

Returns:

Type Description
str

Encoded ciphertext.

encode_rail_fence_cipher
encode_rail_fence_cipher(text: str, num_rails: int) -> str

Encode text using the Rail Fence cipher.

Parameters:

Name Type Description Default
text str

The plaintext to encode.

required
num_rails int

Number of rails (rows) to use in the zigzag pattern.

required

Returns:

Type Description
str

Encoded ciphertext string.

encode_rot13_cipher
encode_rot13_cipher(text: str) -> str

Encode or decode text using the Rot13 cipher.

Since Rot13 is symmetric, this function is used for both encoding and decoding.

Parameters:

Name Type Description Default
text str

The input text to encode or decode.

required

Returns:

Type Description
str

The transformed text.

encode_scytale_cipher
encode_scytale_cipher(text: str, diameter: int) -> str

Encode a string using the Scytale cipher.

Parameters:

Name Type Description Default
text str

The input plaintext.

required
diameter int

Number of columns (characters per wrap-around).

required

Returns:

Type Description
str

Encoded ciphertext.

encode_transposition_cipher
encode_transposition_cipher(text: str, key: int) -> str

Encode text using a simple columnar transposition cipher.

Parameters:

Name Type Description Default
text str

The plaintext to encode.

required
key int

The number of columns (i.e., transposition key).

required

Returns:

Type Description
str

The encoded (scrambled) text.

encode_vigenere_cipher
encode_vigenere_cipher(text: str, key: str) -> str

Encode text using the Vigenère cipher.

Non-alphabet characters are preserved.

Parameters:

Name Type Description Default
text str

The plaintext to encode.

required
key str

The keyword used to perform letter shifts.

required

Returns:

Type Description
str

The encoded ciphertext.

encode_xor_cipher
encode_xor_cipher(text: str, key: str) -> str

Encode or decode text using the XOR cipher.

Since XOR is symmetric, this function performs both encryption and decryption.

Parameters:

Name Type Description Default
text str

The plaintext or ciphertext string.

required
key str

The encryption key.

required

Returns:

Name Type Description
str

A string with the result of XOR encryption/decryption.

Note str

Non-printable characters may be present in the result.

four_square_cipher_pair
four_square_cipher_pair(pair: str, square_tl: list[list[str]], square_tr: list[list[str]], square_bl: list[list[str]], square_br: list[list[str]], mode: str = 'encode') -> str

Encode or decode a letter pair using the Four-Square cipher.

Parameters:

Name Type Description Default
pair str

A two-character string.

required
square_tl list[list[str]]

Top-left square (standard).

required
square_tr list[list[str]]

Top-right square (key 1).

required
square_bl list[list[str]]

Bottom-left square (key 2).

required
square_br list[list[str]]

Bottom-right square (standard).

required
mode str

'encode' or 'decode'.

'encode'

Returns:

Type Description
str

Encoded or decoded letter pair.

Modules
cipher_atbash
Rite - Cryptography - Cipher - Atbash Cipher Module

Provides functionality to encode and decode text using the Atbash cipher.

The Atbash cipher is a classical substitution cipher that reverses the alphabet (A ↔ Z, B ↔ Y, ..., a ↔ z).

References
Functions
decode_atbash_cipher
decode_atbash_cipher(text: str) -> str

Decode text using the Atbash cipher.

This is functionally identical to encoding, since Atbash is symmetric.

Parameters:

Name Type Description Default
text str

The Atbash-encoded string.

required

Returns:

Type Description
str

The decoded string (same as encoding).

encode_atbash_cipher
encode_atbash_cipher(text: str) -> str

Encode (or decode) text using the Atbash cipher.

Since Atbash is symmetric, the same function can be used for encoding and decoding.

Parameters:

Name Type Description Default
text str

Input string to encode or decode.

required

Returns:

Type Description
str

The Atbash-encoded/decoded string.

cipher_autokey
Rite - Cryptography - Cipher - Autokey Cipher Module

Provides functionality to encode and decode text using the Autokey cipher.

The Autokey cipher is a polyalphabetic substitution cipher that extends the key with the plaintext itself during encoding, and with the decoded text during decoding.

References
Functions
decode_autokey_cipher
decode_autokey_cipher(encoded_text: str, key: str) -> str

Decode text using the Autokey cipher.

Parameters:

Name Type Description Default
encoded_text str

The ciphertext to decode.

required
key str

The original cipher key.

required

Returns:

Type Description
str

Decoded plaintext.

encode_autokey_cipher
encode_autokey_cipher(text: str, key: str) -> str

Encode text using the Autokey cipher.

Parameters:

Name Type Description Default
text str

The plaintext to encode.

required
key str

The cipher key (only letters are used).

required

Returns:

Type Description
str

Encoded ciphertext.

cipher_baconian
Rite - Cryptography - Cipher - Baconian Cipher Module

Provides functionality to encode and decode text using the Baconian cipher.

The Baconian cipher encodes each letter of the alphabet using a unique 5-letter combination of 'a' and 'b'. It is a classical steganographic cipher.

References
Functions
decode_baconian_cipher
decode_baconian_cipher(encoded_text: str) -> str

Decode Baconian-encoded text (a/b) back into plaintext.

Ignores incomplete final segments and assumes input is clean (only 'a' and 'b').

Parameters:

Name Type Description Default
encoded_text str

A string of 'a' and 'b' characters, in chunks of 5.

required

Returns:

Type Description
str

Decoded plaintext.

encode_baconian_cipher
encode_baconian_cipher(text: str) -> str

Encode plaintext using the Baconian cipher.

Non-alphabetic characters are ignored. All letters are converted to lowercase.

Parameters:

Name Type Description Default
text str

Input text to encode.

required

Returns:

Type Description
str

A string of 'a' and 'b' representing the encoded text.

cipher_caesar
Rite - Cryptography - Cipher - Caesar Cipher Module

Provides functionality to encode and decode text using the Caesar cipher.

The Caesar cipher is a substitution cipher that shifts characters by a fixed offset.

References
Functions
decode_caesar_cipher
decode_caesar_cipher(encoded_text: str, shift: int) -> str

Decode Caesar cipher text by reversing the shift.

Parameters:

Name Type Description Default
encoded_text str

The ciphertext to decode.

required
shift int

The original shift used during encoding.

required

Returns:

Type Description
str

Decoded plaintext string.

encode_caesar_cipher
encode_caesar_cipher(text: str, shift: int) -> str

Encode text using the Caesar cipher.

Only alphabetic characters are shifted. Case is preserved. Other characters remain unchanged.

Parameters:

Name Type Description Default
text str

The plaintext string to encode.

required
shift int

The number of positions to shift (can be positive or negative).

required

Returns:

Type Description
str

Encoded ciphertext string.

cipher_four_square
Rite - Cryptography - Cipher - Four-Square Cipher Module

Provides functionality to encode and decode text using the Four-Square cipher.

This classical cipher uses four 5x5 matrices to encode letter pairs.

References
Functions
decode_four_square_cipher
decode_four_square_cipher(encoded_text: str, key1: str, key2: str) -> str

Decode ciphertext encoded with the Four-Square cipher.

Parameters:

Name Type Description Default
encoded_text str

The encrypted message to decode.

required
key1 str

Key used for the top-right square.

required
key2 str

Key used for the bottom-left square.

required

Returns:

Type Description
str

The decoded plaintext.

encode_four_square_cipher
encode_four_square_cipher(text: str, key1: str, key2: str) -> str

Encode text using the Four-Square cipher.

Parameters:

Name Type Description Default
text str

The plaintext to encode.

required
key1 str

Key for the top-right square.

required
key2 str

Key for the bottom-left square.

required

Returns:

Type Description
str

The encoded ciphertext.

find_position
find_position(letter: str, square: list[list[str]]) -> tuple[int, int]

Find the (row, col) position of a letter in a 5x5 matrix.

Parameters:

Name Type Description Default
letter str

The letter to locate.

required
square list[list[str]]

A 5x5 matrix.

required

Returns:

Type Description
tuple[int, int]

(row_index, col_index) of the letter.

four_square_cipher_pair
four_square_cipher_pair(pair: str, square_tl: list[list[str]], square_tr: list[list[str]], square_bl: list[list[str]], square_br: list[list[str]], mode: str = 'encode') -> str

Encode or decode a letter pair using the Four-Square cipher.

Parameters:

Name Type Description Default
pair str

A two-character string.

required
square_tl list[list[str]]

Top-left square (standard).

required
square_tr list[list[str]]

Top-right square (key 1).

required
square_bl list[list[str]]

Bottom-left square (key 2).

required
square_br list[list[str]]

Bottom-right square (standard).

required
mode str

'encode' or 'decode'.

'encode'

Returns:

Type Description
str

Encoded or decoded letter pair.

generate_square
generate_square(key: str) -> list[list[str]]

Generate a 5x5 matrix for the Four-Square cipher using a key.

Parameters:

Name Type Description Default
key str

Keyword to populate the matrix.

required

Returns:

Type Description
list[list[str]]

A 5x5 list of characters (J is merged with I).

cipher_playfair
Rite - Cryptography - Cipher - Playfair Cipher Module

Provides functionality to encode and decode text using the Playfair cipher.

This classical digraph cipher uses a 5x5 matrix to substitute pairs of letters.

References
Functions
create_playfair_square
create_playfair_square(key: str) -> list[list[str]]

Create a 5x5 Playfair square using the given key.

J is omitted (merged with I), and duplicates are removed in order.

Parameters:

Name Type Description Default
key str

The keyword to use for square generation.

required

Returns:

Type Description
list[list[str]]

A 5x5 list of characters.

decode_playfair_cipher
decode_playfair_cipher(encoded_text: str, key: str) -> str

Decode Playfair-encoded ciphertext.

Parameters:

Name Type Description Default
encoded_text str

The encoded string.

required
key str

Cipher key used during encoding.

required

Returns:

Type Description
str

Decoded plaintext (not automatically de-padded).

encode_playfair_cipher
encode_playfair_cipher(text: str, key: str) -> str

Encode plaintext using the Playfair cipher.

Parameters:

Name Type Description Default
text str

Input plaintext.

required
key str

Cipher key for generating the Playfair square.

required

Returns:

Type Description
str

Encoded ciphertext.

find_position
find_position(letter: str, square: list[list[str]]) -> tuple[int, int]

Find (row, col) of a letter in the Playfair square.

Parameters:

Name Type Description Default
letter str

The letter to locate.

required
square list[list[str]]

The 5x5 matrix.

required

Returns:

Type Description
tuple[int, int]

Tuple of (row_index, col_index)

playfair_cipher_pair
playfair_cipher_pair(pair: str, square: list[list[str]], mode: str = 'encode') -> str

Encode or decode a digraph using the Playfair cipher.

Parameters:

Name Type Description Default
pair str

Two-letter string.

required
square list[list[str]]

The Playfair square.

required
mode str

'encode' or 'decode'.

'encode'

Returns:

Type Description
str

Encoded or decoded two-letter string.

prepare_text
prepare_text(text: str) -> str

Prepare text for Playfair encoding.

Replaces J with I, removes non-alphabetic characters, inserts 'X' between duplicate letters, and pads the end if necessary.

Parameters:

Name Type Description Default
text str

Raw input text.

required

Returns:

Type Description
str

Cleaned and paired text ready for encoding.

cipher_rail_fence
Rite - Cryptography - Cipher - Rail Fence Cipher Module

Provides functionality to encode and decode text using the Rail Fence cipher.

The Rail Fence cipher is a transposition cipher that rearranges characters in a zigzag pattern across a fixed number of rails (rows).

References
Functions
decode_rail_fence_cipher
decode_rail_fence_cipher(encoded_text: str, num_rails: int) -> str

Decode a Rail Fence-encoded string.

Parameters:

Name Type Description Default
encoded_text str

The encoded string to decode.

required
num_rails int

Number of rails used during encoding.

required

Returns:

Type Description
str

The original decoded plaintext string.

encode_rail_fence_cipher
encode_rail_fence_cipher(text: str, num_rails: int) -> str

Encode text using the Rail Fence cipher.

Parameters:

Name Type Description Default
text str

The plaintext to encode.

required
num_rails int

Number of rails (rows) to use in the zigzag pattern.

required

Returns:

Type Description
str

Encoded ciphertext string.

cipher_rot13
Rite - Cryptography - Cipher - Rot13 Cipher Module

Provides functionality to encode and decode text using the Rot13 cipher.

The Rot13 cipher shifts each letter 13 places in the alphabet and is symmetric (i.e., encoding and decoding are the same operation).

References
Functions
decode_rot13_cipher
decode_rot13_cipher(text: str) -> str

Decode text using the Rot13 cipher.

This is functionally identical to encoding, as Rot13 is symmetric.

Parameters:

Name Type Description Default
text str

The Rot13-encoded text.

required

Returns:

Type Description
str

The decoded text.

encode_rot13_cipher
encode_rot13_cipher(text: str) -> str

Encode or decode text using the Rot13 cipher.

Since Rot13 is symmetric, this function is used for both encoding and decoding.

Parameters:

Name Type Description Default
text str

The input text to encode or decode.

required

Returns:

Type Description
str

The transformed text.

cipher_scytale
Rite - Cryptography - Cipher - Scytale Cipher Module

The Scytale cipher wraps text around a rod (cylinder) of fixed diameter, writing along its length and reading across its circumference.

References
Functions
decode_scytale_cipher
decode_scytale_cipher(encoded_text: str, diameter: int) -> str

Decode a Scytale-encoded string.

Parameters:

Name Type Description Default
encoded_text str

The ciphertext to decode.

required
diameter int

Number of columns used during encoding.

required

Returns:

Type Description
str

Decoded plaintext string.

encode_scytale_cipher
encode_scytale_cipher(text: str, diameter: int) -> str

Encode a string using the Scytale cipher.

Parameters:

Name Type Description Default
text str

The input plaintext.

required
diameter int

Number of columns (characters per wrap-around).

required

Returns:

Type Description
str

Encoded ciphertext.

cipher_transposition
Rite - Cryptography - Cipher - Transposition Cipher Module

Provides functionality to encode and decode text using a columnar transposition cipher.

The transposition cipher rearranges the characters of the plaintext based on a fixed number of columns (key).

References
Functions
decode_transposition_cipher
decode_transposition_cipher(encoded_text: str, key: int, strip_padding: bool = True) -> str

Decode text from a simple columnar transposition cipher.

Parameters:

Name Type Description Default
encoded_text str

The transposed text to decode.

required
key int

The number of columns used in encoding.

required
strip_padding bool

Whether to strip trailing padding spaces (default: True).

True

Returns:

Type Description
str

The decoded (original) text.

encode_transposition_cipher
encode_transposition_cipher(text: str, key: int) -> str

Encode text using a simple columnar transposition cipher.

Parameters:

Name Type Description Default
text str

The plaintext to encode.

required
key int

The number of columns (i.e., transposition key).

required

Returns:

Type Description
str

The encoded (scrambled) text.

cipher_vigenere
Rite - Cryptography - Cipher - Vigenère Cipher Module

Provides functionality to encode and decode text using the Vigenère cipher.

The Vigenère cipher is a method of encrypting alphabetic text by using a simple form of polyalphabetic substitution based on a keyword.

References
Functions
decode_vigenere_cipher
decode_vigenere_cipher(encoded_text: str, key: str) -> str

Decode text from the Vigenère cipher.

Non-alphabet characters are preserved.

Parameters:

Name Type Description Default
encoded_text str

The ciphertext to decode.

required
key str

The keyword originally used to encode.

required

Returns:

Type Description
str

The decoded plaintext.

encode_vigenere_cipher
encode_vigenere_cipher(text: str, key: str) -> str

Encode text using the Vigenère cipher.

Non-alphabet characters are preserved.

Parameters:

Name Type Description Default
text str

The plaintext to encode.

required
key str

The keyword used to perform letter shifts.

required

Returns:

Type Description
str

The encoded ciphertext.

cipher_xor
Rite - Cryptography - Cipher - XOR Cipher Module

Provides functionality to encode and decode text using the XOR cipher.

The XOR cipher is a symmetric key encryption technique that uses the XOR (bitwise exclusive OR) operation between the plaintext and a repeating key.

References
Functions
decode_xor_cipher
decode_xor_cipher(text: str, key: str) -> str

Decode XOR-encoded text using the same key.

This function is equivalent to encode_xor_cipher().

Parameters:

Name Type Description Default
text str

The XOR-encoded string.

required
key str

The encryption key used for encoding.

required

Returns:

Type Description
str

The decoded plaintext string.

encode_xor_cipher
encode_xor_cipher(text: str, key: str) -> str

Encode or decode text using the XOR cipher.

Since XOR is symmetric, this function performs both encryption and decryption.

Parameters:

Name Type Description Default
text str

The plaintext or ciphertext string.

required
key str

The encryption key.

required

Returns:

Name Type Description
str

A string with the result of XOR encryption/decryption.

Note str

Non-printable characters may be present in the result.

hash

Hash Module

Cryptographic hash functions and message authentication.

This submodule provides hash functions including MD5, SHA family, BLAKE2, and SHA-3, as well as HMAC message authentication.

Examples

from rite.crypto.hash import hash_sha256, hash_blake2b hash_sha256("hello")[:16] '2cf24dba5fb0a30e' len(hash_blake2b("data")) 128

Functions
hash_sha256_hmac
hash_sha256_hmac(key: str | bytes, msg: str | bytes, encoding: str = 'utf-8') -> str

Compute SHA-256 HMAC of message with key.

Parameters:

Name Type Description Default
key str | bytes

Secret key for HMAC.

required
msg str | bytes

Message to authenticate.

required
encoding str

Text encoding if inputs are strings.

'utf-8'

Returns:

Type Description
str

Hexadecimal HMAC-SHA-256 string.

Examples:

>>> hmac_result = hash_sha256_hmac("secret", "message")
>>> len(hmac_result)
64
hash_sha512_hmac
hash_sha512_hmac(key: str | bytes, msg: str | bytes, encoding: str = 'utf-8') -> str

Compute SHA-512 HMAC of message with key.

Parameters:

Name Type Description Default
key str | bytes

Secret key for HMAC.

required
msg str | bytes

Message to authenticate.

required
encoding str

Text encoding if inputs are strings.

'utf-8'

Returns:

Type Description
str

Hexadecimal HMAC-SHA-512 string.

Examples:

>>> hmac_result = hash_sha512_hmac("secret", "message")
>>> len(hmac_result)
128
Modules
hash_blake2b
BLAKE2b Hash

Compute BLAKE2b hash (fast, secure alternative to SHA).

Examples

from rite.crypto.hash import hash_blake2b len(hash_blake2b("hello")) 128

Functions
hash_blake2b
hash_blake2b(data: str | bytes, digest_size: int = 64, encoding: str = 'utf-8') -> str

Compute BLAKE2b hash of data.

Parameters:

Name Type Description Default
data str | bytes

String or bytes to hash.

required
digest_size int

Hash size in bytes (1-64).

64
encoding str

Text encoding if data is string.

'utf-8'

Returns:

Type Description
str

Hexadecimal BLAKE2b hash string.

Examples:

>>> len(hash_blake2b("hello"))
128
>>> len(hash_blake2b("hello", digest_size=32))
64
>>> hash_blake2b("test")[:16]
'928b20366943e2ae'
Notes

BLAKE2b is faster than MD5, SHA-1, SHA-2, and SHA-3 while being at least as secure as SHA-3.

hash_blake2s
BLAKE2s Hash

Compute BLAKE2s hash (optimized for 8-32 bit platforms).

Examples

from rite.crypto.hash import hash_blake2s len(hash_blake2s("hello")) 64

Functions
hash_blake2s
hash_blake2s(data: str | bytes, digest_size: int = 32, encoding: str = 'utf-8') -> str

Compute BLAKE2s hash of data.

Parameters:

Name Type Description Default
data str | bytes

String or bytes to hash.

required
digest_size int

Hash size in bytes (1-32).

32
encoding str

Text encoding if data is string.

'utf-8'

Returns:

Type Description
str

Hexadecimal BLAKE2s hash string.

Examples:

>>> len(hash_blake2s("hello"))
64
>>> len(hash_blake2s("hello", digest_size=16))
32
>>> hash_blake2s("test")[:16]
'6b5935c36085c006'
Notes

BLAKE2s is optimized for 8-32 bit platforms and produces smaller hashes than BLAKE2b.

hash_md5
MD5 Hash

Compute MD5 hash (for non-cryptographic purposes).

Examples

from rite.crypto.hash import hash_md5 hash_md5("hello") '5d41402abc4b2a76b9719d911017c592'

Functions
hash_md5
hash_md5(data: str | bytes, encoding: str = 'utf-8') -> str

Compute MD5 hash of data.

Parameters:

Name Type Description Default
data str | bytes

String or bytes to hash.

required
encoding str

Text encoding if data is string.

'utf-8'

Returns:

Type Description
str

Hexadecimal MD5 hash string.

Examples:

>>> hash_md5("hello")
'5d41402abc4b2a76b9719d911017c592'
>>> hash_md5(b"hello")
'5d41402abc4b2a76b9719d911017c592'
Warning

MD5 is cryptographically broken. Use only for checksums, not for security purposes.

hash_sha1
SHA-1 Hash

Compute SHA-1 hash (for non-cryptographic purposes).

Examples

from rite.crypto.hash import hash_sha1 hash_sha1("hello") 'aaf4c61'

Functions
hash_sha1
hash_sha1(data: str | bytes, encoding: str = 'utf-8') -> str

Compute SHA-1 hash of data.

Parameters:

Name Type Description Default
data str | bytes

String or bytes to hash.

required
encoding str

Text encoding if data is string.

'utf-8'

Returns:

Type Description
str

Hexadecimal SHA-1 hash string.

Examples:

>>> hash_sha1("hello")
'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d'
>>> hash_sha1(b"hello")
'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d'
Warning

SHA-1 is deprecated for cryptographic use. Use SHA-256 or SHA-512 for security purposes.

hash_sha256
SHA-256 Hash

Compute SHA-256 hash and HMAC.

Examples

from rite.crypto.hash import hash_sha256 len(hash_sha256("hello")) 64 hash_sha256_hmac("key", "message")[:16] '6e40a5e7a8b7'

Functions
hash_sha256
hash_sha256(data: str | bytes, encoding: str = 'utf-8') -> str

Compute SHA-256 hash of data.

Parameters:

Name Type Description Default
data str | bytes

String or bytes to hash.

required
encoding str

Text encoding if data is string.

'utf-8'

Returns:

Type Description
str

Hexadecimal SHA-256 hash string (64 characters).

Examples:

>>> len(hash_sha256("hello"))
64
>>> hash_sha256(b"hello")[:16]
'2cf24dba5fb0a30e'
hash_sha256_hmac
hash_sha256_hmac(key: str | bytes, msg: str | bytes, encoding: str = 'utf-8') -> str

Compute SHA-256 HMAC of message with key.

Parameters:

Name Type Description Default
key str | bytes

Secret key for HMAC.

required
msg str | bytes

Message to authenticate.

required
encoding str

Text encoding if inputs are strings.

'utf-8'

Returns:

Type Description
str

Hexadecimal HMAC-SHA-256 string.

Examples:

>>> hmac_result = hash_sha256_hmac("secret", "message")
>>> len(hmac_result)
64
hash_sha384
SHA-384 Hash

Compute SHA-384 hash.

Examples

from rite.crypto.hash import hash_sha384 len(hash_sha384("hello")) 96

Functions
hash_sha384
hash_sha384(data: str | bytes, encoding: str = 'utf-8') -> str

Compute SHA-384 hash of data.

Parameters:

Name Type Description Default
data str | bytes

String or bytes to hash.

required
encoding str

Text encoding if data is string.

'utf-8'

Returns:

Type Description
str

Hexadecimal SHA-384 hash string (96 characters).

Examples:

>>> hash_sha384("hello")[:32]
'59e1748777448c69de6b800d7a33'
>>> len(hash_sha384("test"))
96
hash_sha3_256
SHA-3-256 Hash

Compute SHA-3-256 hash.

Examples

from rite.crypto.hash import hash_sha3_256 len(hash_sha3_256("hello")) 64

Functions
hash_sha3_256
hash_sha3_256(data: str | bytes, encoding: str = 'utf-8') -> str

Compute SHA-3-256 hash of data.

Parameters:

Name Type Description Default
data str | bytes

String or bytes to hash.

required
encoding str

Text encoding if data is string.

'utf-8'

Returns:

Type Description
str

Hexadecimal SHA-3-256 hash string (64 characters).

Examples:

>>> len(hash_sha3_256("hello"))
64
>>> hash_sha3_256("test")[:16]
'36f028580bb02cc8'
hash_sha3_512
SHA-3-512 Hash

Compute SHA-3-512 hash.

Examples

from rite.crypto.hash import hash_sha3_512 len(hash_sha3_512("hello")) 128

Functions
hash_sha3_512
hash_sha3_512(data: str | bytes, encoding: str = 'utf-8') -> str

Compute SHA-3-512 hash of data.

Parameters:

Name Type Description Default
data str | bytes

String or bytes to hash.

required
encoding str

Text encoding if data is string.

'utf-8'

Returns:

Type Description
str

Hexadecimal SHA-3-512 hash string (128 characters).

Examples:

>>> len(hash_sha3_512("hello"))
128
>>> hash_sha3_512("test")[:16]
'1e2e9fc2002b002d'
hash_sha512
SHA-512 Hash

Compute SHA-512 hash and HMAC.

Examples

from rite.crypto.hash import hash_sha512 len(hash_sha512("hello")) 128 hash_sha512_hmac("key", "message")[:16] 'b42af09057bac1e2'

Functions
hash_sha512
hash_sha512(data: str | bytes, encoding: str = 'utf-8') -> str

Compute SHA-512 hash of data.

Parameters:

Name Type Description Default
data str | bytes

String or bytes to hash.

required
encoding str

Text encoding if data is string.

'utf-8'

Returns:

Type Description
str

Hexadecimal SHA-512 hash string (128 characters).

Examples:

>>> len(hash_sha512("hello"))
128
>>> hash_sha512(b"hello")[:16]
'9b71d224bd62f378'
hash_sha512_hmac
hash_sha512_hmac(key: str | bytes, msg: str | bytes, encoding: str = 'utf-8') -> str

Compute SHA-512 HMAC of message with key.

Parameters:

Name Type Description Default
key str | bytes

Secret key for HMAC.

required
msg str | bytes

Message to authenticate.

required
encoding str

Text encoding if inputs are strings.

'utf-8'

Returns:

Type Description
str

Hexadecimal HMAC-SHA-512 string.

Examples:

>>> hmac_result = hash_sha512_hmac("secret", "message")
>>> len(hmac_result)
128

hash_blake2b

BLAKE2b Hash

Compute BLAKE2b hash (fast, secure alternative to SHA).

Examples

from rite.crypto.hash import hash_blake2b len(hash_blake2b("hello")) 128

Functions
hash_blake2b
hash_blake2b(data: str | bytes, digest_size: int = 64, encoding: str = 'utf-8') -> str

Compute BLAKE2b hash of data.

Parameters:

Name Type Description Default
data str | bytes

String or bytes to hash.

required
digest_size int

Hash size in bytes (1-64).

64
encoding str

Text encoding if data is string.

'utf-8'

Returns:

Type Description
str

Hexadecimal BLAKE2b hash string.

Examples:

>>> len(hash_blake2b("hello"))
128
>>> len(hash_blake2b("hello", digest_size=32))
64
>>> hash_blake2b("test")[:16]
'928b20366943e2ae'
Notes

BLAKE2b is faster than MD5, SHA-1, SHA-2, and SHA-3 while being at least as secure as SHA-3.

hash_blake2s

BLAKE2s Hash

Compute BLAKE2s hash (optimized for 8-32 bit platforms).

Examples

from rite.crypto.hash import hash_blake2s len(hash_blake2s("hello")) 64

Functions
hash_blake2s
hash_blake2s(data: str | bytes, digest_size: int = 32, encoding: str = 'utf-8') -> str

Compute BLAKE2s hash of data.

Parameters:

Name Type Description Default
data str | bytes

String or bytes to hash.

required
digest_size int

Hash size in bytes (1-32).

32
encoding str

Text encoding if data is string.

'utf-8'

Returns:

Type Description
str

Hexadecimal BLAKE2s hash string.

Examples:

>>> len(hash_blake2s("hello"))
64
>>> len(hash_blake2s("hello", digest_size=16))
32
>>> hash_blake2s("test")[:16]
'6b5935c36085c006'
Notes

BLAKE2s is optimized for 8-32 bit platforms and produces smaller hashes than BLAKE2b.

hash_md5

MD5 Hash

Compute MD5 hash (for non-cryptographic purposes).

Examples

from rite.crypto.hash import hash_md5 hash_md5("hello") '5d41402abc4b2a76b9719d911017c592'

Functions
hash_md5
hash_md5(data: str | bytes, encoding: str = 'utf-8') -> str

Compute MD5 hash of data.

Parameters:

Name Type Description Default
data str | bytes

String or bytes to hash.

required
encoding str

Text encoding if data is string.

'utf-8'

Returns:

Type Description
str

Hexadecimal MD5 hash string.

Examples:

>>> hash_md5("hello")
'5d41402abc4b2a76b9719d911017c592'
>>> hash_md5(b"hello")
'5d41402abc4b2a76b9719d911017c592'
Warning

MD5 is cryptographically broken. Use only for checksums, not for security purposes.

hash_sha1

SHA-1 Hash

Compute SHA-1 hash (for non-cryptographic purposes).

Examples

from rite.crypto.hash import hash_sha1 hash_sha1("hello") 'aaf4c61'

Functions
hash_sha1
hash_sha1(data: str | bytes, encoding: str = 'utf-8') -> str

Compute SHA-1 hash of data.

Parameters:

Name Type Description Default
data str | bytes

String or bytes to hash.

required
encoding str

Text encoding if data is string.

'utf-8'

Returns:

Type Description
str

Hexadecimal SHA-1 hash string.

Examples:

>>> hash_sha1("hello")
'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d'
>>> hash_sha1(b"hello")
'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d'
Warning

SHA-1 is deprecated for cryptographic use. Use SHA-256 or SHA-512 for security purposes.

hash_sha256

SHA-256 Hash

Compute SHA-256 hash and HMAC.

Examples

from rite.crypto.hash import hash_sha256 len(hash_sha256("hello")) 64 hash_sha256_hmac("key", "message")[:16] '6e40a5e7a8b7'

Functions
hash_sha256
hash_sha256(data: str | bytes, encoding: str = 'utf-8') -> str

Compute SHA-256 hash of data.

Parameters:

Name Type Description Default
data str | bytes

String or bytes to hash.

required
encoding str

Text encoding if data is string.

'utf-8'

Returns:

Type Description
str

Hexadecimal SHA-256 hash string (64 characters).

Examples:

>>> len(hash_sha256("hello"))
64
>>> hash_sha256(b"hello")[:16]
'2cf24dba5fb0a30e'
hash_sha256_hmac
hash_sha256_hmac(key: str | bytes, msg: str | bytes, encoding: str = 'utf-8') -> str

Compute SHA-256 HMAC of message with key.

Parameters:

Name Type Description Default
key str | bytes

Secret key for HMAC.

required
msg str | bytes

Message to authenticate.

required
encoding str

Text encoding if inputs are strings.

'utf-8'

Returns:

Type Description
str

Hexadecimal HMAC-SHA-256 string.

Examples:

>>> hmac_result = hash_sha256_hmac("secret", "message")
>>> len(hmac_result)
64

hash_sha384

SHA-384 Hash

Compute SHA-384 hash.

Examples

from rite.crypto.hash import hash_sha384 len(hash_sha384("hello")) 96

Functions
hash_sha384
hash_sha384(data: str | bytes, encoding: str = 'utf-8') -> str

Compute SHA-384 hash of data.

Parameters:

Name Type Description Default
data str | bytes

String or bytes to hash.

required
encoding str

Text encoding if data is string.

'utf-8'

Returns:

Type Description
str

Hexadecimal SHA-384 hash string (96 characters).

Examples:

>>> hash_sha384("hello")[:32]
'59e1748777448c69de6b800d7a33'
>>> len(hash_sha384("test"))
96

hash_sha3_256

SHA-3-256 Hash

Compute SHA-3-256 hash.

Examples

from rite.crypto.hash import hash_sha3_256 len(hash_sha3_256("hello")) 64

Functions
hash_sha3_256
hash_sha3_256(data: str | bytes, encoding: str = 'utf-8') -> str

Compute SHA-3-256 hash of data.

Parameters:

Name Type Description Default
data str | bytes

String or bytes to hash.

required
encoding str

Text encoding if data is string.

'utf-8'

Returns:

Type Description
str

Hexadecimal SHA-3-256 hash string (64 characters).

Examples:

>>> len(hash_sha3_256("hello"))
64
>>> hash_sha3_256("test")[:16]
'36f028580bb02cc8'

hash_sha3_512

SHA-3-512 Hash

Compute SHA-3-512 hash.

Examples

from rite.crypto.hash import hash_sha3_512 len(hash_sha3_512("hello")) 128

Functions
hash_sha3_512
hash_sha3_512(data: str | bytes, encoding: str = 'utf-8') -> str

Compute SHA-3-512 hash of data.

Parameters:

Name Type Description Default
data str | bytes

String or bytes to hash.

required
encoding str

Text encoding if data is string.

'utf-8'

Returns:

Type Description
str

Hexadecimal SHA-3-512 hash string (128 characters).

Examples:

>>> len(hash_sha3_512("hello"))
128
>>> hash_sha3_512("test")[:16]
'1e2e9fc2002b002d'

hash_sha512

SHA-512 Hash

Compute SHA-512 hash and HMAC.

Examples

from rite.crypto.hash import hash_sha512 len(hash_sha512("hello")) 128 hash_sha512_hmac("key", "message")[:16] 'b42af09057bac1e2'

Functions
hash_sha512
hash_sha512(data: str | bytes, encoding: str = 'utf-8') -> str

Compute SHA-512 hash of data.

Parameters:

Name Type Description Default
data str | bytes

String or bytes to hash.

required
encoding str

Text encoding if data is string.

'utf-8'

Returns:

Type Description
str

Hexadecimal SHA-512 hash string (128 characters).

Examples:

>>> len(hash_sha512("hello"))
128
>>> hash_sha512(b"hello")[:16]
'9b71d224bd62f378'
hash_sha512_hmac
hash_sha512_hmac(key: str | bytes, msg: str | bytes, encoding: str = 'utf-8') -> str

Compute SHA-512 HMAC of message with key.

Parameters:

Name Type Description Default
key str | bytes

Secret key for HMAC.

required
msg str | bytes

Message to authenticate.

required
encoding str

Text encoding if inputs are strings.

'utf-8'

Returns:

Type Description
str

Hexadecimal HMAC-SHA-512 string.

Examples:

>>> hmac_result = hash_sha512_hmac("secret", "message")
>>> len(hmac_result)
128

random

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
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
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
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
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
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.

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
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
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
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
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
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.

uuid

Identity Module

This module provides unique identifier generation similar to Python's uuid module.

Functions will include: - UUID generation and utilities

Example

from rite.identity import generate_uuid generate_uuid() 'a1b2c3d4-e5f6-...'

Functions
is_valid_uuid
is_valid_uuid(uuid_to_test: str, version: int = 4) -> bool

Check if uuid_to_test is a valid UUID.

Parameters:

Name Type Description Default
uuid_to_test str

String to validate as UUID

required
version int

UUID version to check (1, 2, 3, or 4)

4

Returns:

Type Description
bool

True if valid UUID, False otherwise

Example

is_valid_uuid('550e8400-e29b-41d4-a716-446655440000') True is_valid_uuid('invalid') False

Modules
uuid_from_name
Rite - UUID Module
Functions
from_name
from_name(namespace, name)

Generates a UUID based on a namespace and a name.

Parameters:

Name Type Description Default
namespace UUID

The namespace UUID.

required
name str

The name from which to generate the UUID.

required
Returns
uuid.UUID: A UUID generated from the namespace and name.
uuid_get_version
Rite - UUID Module
Functions
get_version
get_version(uuid_obj)

Extracts the version of a UUID.

Parameters:

Name Type Description Default
uuid_obj UUID

The UUID object.

required
Returns
int: The version number of the UUID.
uuid_hex
UUID Hexadecimal Generation

Generate random UUIDs as hexadecimal strings.

Functions
uuid_hex
uuid_hex() -> str

Generate a random UUID and return it as a 32-character hexadecimal string.

Returns:

Type Description
str

UUID as a 32-character hexadecimal string (no hyphens)

Example

len(uuid_hex()) 32

uuid_is_random
Rite - UUID Module
Functions
is_random_uuid
is_random_uuid(uuid_obj)

Checks if the UUID is randomly generated (UUID4).

Parameters:

Name Type Description Default
uuid_obj UUID

The UUID object.

required
Returns
bool: True if the UUID is a random UUID, False otherwise.
uuid_is_valid
UUID Validation

Validate UUID strings.

Functions
is_valid_uuid
is_valid_uuid(uuid_to_test: str, version: int = 4) -> bool

Check if uuid_to_test is a valid UUID.

Parameters:

Name Type Description Default
uuid_to_test str

String to validate as UUID

required
version int

UUID version to check (1, 2, 3, or 4)

4

Returns:

Type Description
bool

True if valid UUID, False otherwise

Example

is_valid_uuid('550e8400-e29b-41d4-a716-446655440000') True is_valid_uuid('invalid') False

uuid_random
UUID Object Generation

Generate random UUID objects.

Functions
uuid_random
uuid_random() -> uuid.UUID

Generate a random UUID (UUID4).

Returns:

Type Description
UUID

A randomly generated UUID object

Example

isinstance(uuid_random(), uuid.UUID) True

uuid_string
UUID String Generation

Generate random UUIDs as formatted strings.

Functions
uuid_string
uuid_string() -> str

Generate a random UUID and return it as a string in standard form.

Returns:

Type Description
str

UUID as a string in standard form (with hyphens)

Example

'-' in uuid_string() True len(uuid_string()) 36

uuid_from_name

Rite - UUID Module
Functions
from_name
from_name(namespace, name)

Generates a UUID based on a namespace and a name.

Parameters:

Name Type Description Default
namespace UUID

The namespace UUID.

required
name str

The name from which to generate the UUID.

required
Returns
uuid.UUID: A UUID generated from the namespace and name.

uuid_get_version

Rite - UUID Module
Functions
get_version
get_version(uuid_obj)

Extracts the version of a UUID.

Parameters:

Name Type Description Default
uuid_obj UUID

The UUID object.

required
Returns
int: The version number of the UUID.

uuid_hex

UUID Hexadecimal Generation

Generate random UUIDs as hexadecimal strings.

Functions
uuid_hex
uuid_hex() -> str

Generate a random UUID and return it as a 32-character hexadecimal string.

Returns:

Type Description
str

UUID as a 32-character hexadecimal string (no hyphens)

Example

len(uuid_hex()) 32

uuid_is_random

Rite - UUID Module
Functions
is_random_uuid
is_random_uuid(uuid_obj)

Checks if the UUID is randomly generated (UUID4).

Parameters:

Name Type Description Default
uuid_obj UUID

The UUID object.

required
Returns
bool: True if the UUID is a random UUID, False otherwise.

uuid_random

UUID Object Generation

Generate random UUID objects.

Functions
uuid_random
uuid_random() -> uuid.UUID

Generate a random UUID (UUID4).

Returns:

Type Description
UUID

A randomly generated UUID object

Example

isinstance(uuid_random(), uuid.UUID) True

uuid_string

UUID String Generation

Generate random UUIDs as formatted strings.

Functions
uuid_string
uuid_string() -> str

Generate a random UUID and return it as a string in standard form.

Returns:

Type Description
str

UUID as a string in standard form (with hyphens)

Example

'-' in uuid_string() True len(uuid_string()) 36

Submodules

Hashing

Cryptographic hash functions.

Hash Module

Cryptographic hash functions and message authentication.

This submodule provides hash functions including MD5, SHA family, BLAKE2, and SHA-3, as well as HMAC message authentication.

Examples

from rite.crypto.hash import hash_sha256, hash_blake2b hash_sha256("hello")[:16] '2cf24dba5fb0a30e' len(hash_blake2b("data")) 128

Modules

hash_md5

MD5 Hash

Compute MD5 hash (for non-cryptographic purposes).

Examples

from rite.crypto.hash import hash_md5 hash_md5("hello") '5d41402abc4b2a76b9719d911017c592'

Functions
hash_md5
hash_md5(data: str | bytes, encoding: str = 'utf-8') -> str

Compute MD5 hash of data.

Parameters:

Name Type Description Default
data str | bytes

String or bytes to hash.

required
encoding str

Text encoding if data is string.

'utf-8'

Returns:

Type Description
str

Hexadecimal MD5 hash string.

Examples:

>>> hash_md5("hello")
'5d41402abc4b2a76b9719d911017c592'
>>> hash_md5(b"hello")
'5d41402abc4b2a76b9719d911017c592'
Warning

MD5 is cryptographically broken. Use only for checksums, not for security purposes.

hash_sha1

SHA-1 Hash

Compute SHA-1 hash (for non-cryptographic purposes).

Examples

from rite.crypto.hash import hash_sha1 hash_sha1("hello") 'aaf4c61'

Functions
hash_sha1
hash_sha1(data: str | bytes, encoding: str = 'utf-8') -> str

Compute SHA-1 hash of data.

Parameters:

Name Type Description Default
data str | bytes

String or bytes to hash.

required
encoding str

Text encoding if data is string.

'utf-8'

Returns:

Type Description
str

Hexadecimal SHA-1 hash string.

Examples:

>>> hash_sha1("hello")
'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d'
>>> hash_sha1(b"hello")
'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d'
Warning

SHA-1 is deprecated for cryptographic use. Use SHA-256 or SHA-512 for security purposes.

hash_sha256

SHA-256 Hash

Compute SHA-256 hash and HMAC.

Examples

from rite.crypto.hash import hash_sha256 len(hash_sha256("hello")) 64 hash_sha256_hmac("key", "message")[:16] '6e40a5e7a8b7'

Functions
hash_sha256
hash_sha256(data: str | bytes, encoding: str = 'utf-8') -> str

Compute SHA-256 hash of data.

Parameters:

Name Type Description Default
data str | bytes

String or bytes to hash.

required
encoding str

Text encoding if data is string.

'utf-8'

Returns:

Type Description
str

Hexadecimal SHA-256 hash string (64 characters).

Examples:

>>> len(hash_sha256("hello"))
64
>>> hash_sha256(b"hello")[:16]
'2cf24dba5fb0a30e'
hash_sha256_hmac
hash_sha256_hmac(key: str | bytes, msg: str | bytes, encoding: str = 'utf-8') -> str

Compute SHA-256 HMAC of message with key.

Parameters:

Name Type Description Default
key str | bytes

Secret key for HMAC.

required
msg str | bytes

Message to authenticate.

required
encoding str

Text encoding if inputs are strings.

'utf-8'

Returns:

Type Description
str

Hexadecimal HMAC-SHA-256 string.

Examples:

>>> hmac_result = hash_sha256_hmac("secret", "message")
>>> len(hmac_result)
64

hash_sha384

SHA-384 Hash

Compute SHA-384 hash.

Examples

from rite.crypto.hash import hash_sha384 len(hash_sha384("hello")) 96

Functions
hash_sha384
hash_sha384(data: str | bytes, encoding: str = 'utf-8') -> str

Compute SHA-384 hash of data.

Parameters:

Name Type Description Default
data str | bytes

String or bytes to hash.

required
encoding str

Text encoding if data is string.

'utf-8'

Returns:

Type Description
str

Hexadecimal SHA-384 hash string (96 characters).

Examples:

>>> hash_sha384("hello")[:32]
'59e1748777448c69de6b800d7a33'
>>> len(hash_sha384("test"))
96

hash_sha512

SHA-512 Hash

Compute SHA-512 hash and HMAC.

Examples

from rite.crypto.hash import hash_sha512 len(hash_sha512("hello")) 128 hash_sha512_hmac("key", "message")[:16] 'b42af09057bac1e2'

Functions
hash_sha512
hash_sha512(data: str | bytes, encoding: str = 'utf-8') -> str

Compute SHA-512 hash of data.

Parameters:

Name Type Description Default
data str | bytes

String or bytes to hash.

required
encoding str

Text encoding if data is string.

'utf-8'

Returns:

Type Description
str

Hexadecimal SHA-512 hash string (128 characters).

Examples:

>>> len(hash_sha512("hello"))
128
>>> hash_sha512(b"hello")[:16]
'9b71d224bd62f378'
hash_sha512_hmac
hash_sha512_hmac(key: str | bytes, msg: str | bytes, encoding: str = 'utf-8') -> str

Compute SHA-512 HMAC of message with key.

Parameters:

Name Type Description Default
key str | bytes

Secret key for HMAC.

required
msg str | bytes

Message to authenticate.

required
encoding str

Text encoding if inputs are strings.

'utf-8'

Returns:

Type Description
str

Hexadecimal HMAC-SHA-512 string.

Examples:

>>> hmac_result = hash_sha512_hmac("secret", "message")
>>> len(hmac_result)
128

hash_sha3_256

SHA-3-256 Hash

Compute SHA-3-256 hash.

Examples

from rite.crypto.hash import hash_sha3_256 len(hash_sha3_256("hello")) 64

Functions
hash_sha3_256
hash_sha3_256(data: str | bytes, encoding: str = 'utf-8') -> str

Compute SHA-3-256 hash of data.

Parameters:

Name Type Description Default
data str | bytes

String or bytes to hash.

required
encoding str

Text encoding if data is string.

'utf-8'

Returns:

Type Description
str

Hexadecimal SHA-3-256 hash string (64 characters).

Examples:

>>> len(hash_sha3_256("hello"))
64
>>> hash_sha3_256("test")[:16]
'36f028580bb02cc8'

hash_sha3_512

SHA-3-512 Hash

Compute SHA-3-512 hash.

Examples

from rite.crypto.hash import hash_sha3_512 len(hash_sha3_512("hello")) 128

Functions
hash_sha3_512
hash_sha3_512(data: str | bytes, encoding: str = 'utf-8') -> str

Compute SHA-3-512 hash of data.

Parameters:

Name Type Description Default
data str | bytes

String or bytes to hash.

required
encoding str

Text encoding if data is string.

'utf-8'

Returns:

Type Description
str

Hexadecimal SHA-3-512 hash string (128 characters).

Examples:

>>> len(hash_sha3_512("hello"))
128
>>> hash_sha3_512("test")[:16]
'1e2e9fc2002b002d'

hash_blake2b

BLAKE2b Hash

Compute BLAKE2b hash (fast, secure alternative to SHA).

Examples

from rite.crypto.hash import hash_blake2b len(hash_blake2b("hello")) 128

Functions
hash_blake2b
hash_blake2b(data: str | bytes, digest_size: int = 64, encoding: str = 'utf-8') -> str

Compute BLAKE2b hash of data.

Parameters:

Name Type Description Default
data str | bytes

String or bytes to hash.

required
digest_size int

Hash size in bytes (1-64).

64
encoding str

Text encoding if data is string.

'utf-8'

Returns:

Type Description
str

Hexadecimal BLAKE2b hash string.

Examples:

>>> len(hash_blake2b("hello"))
128
>>> len(hash_blake2b("hello", digest_size=32))
64
>>> hash_blake2b("test")[:16]
'928b20366943e2ae'
Notes

BLAKE2b is faster than MD5, SHA-1, SHA-2, and SHA-3 while being at least as secure as SHA-3.

hash_blake2s

BLAKE2s Hash

Compute BLAKE2s hash (optimized for 8-32 bit platforms).

Examples

from rite.crypto.hash import hash_blake2s len(hash_blake2s("hello")) 64

Functions
hash_blake2s
hash_blake2s(data: str | bytes, digest_size: int = 32, encoding: str = 'utf-8') -> str

Compute BLAKE2s hash of data.

Parameters:

Name Type Description Default
data str | bytes

String or bytes to hash.

required
digest_size int

Hash size in bytes (1-32).

32
encoding str

Text encoding if data is string.

'utf-8'

Returns:

Type Description
str

Hexadecimal BLAKE2s hash string.

Examples:

>>> len(hash_blake2s("hello"))
64
>>> len(hash_blake2s("hello", digest_size=16))
32
>>> hash_blake2s("test")[:16]
'6b5935c36085c006'
Notes

BLAKE2s is optimized for 8-32 bit platforms and produces smaller hashes than BLAKE2b.

UUID Generation

Generate and validate UUIDs.

Identity Module

This module provides unique identifier generation similar to Python's uuid module.

Functions will include: - UUID generation and utilities

Example

from rite.identity import generate_uuid generate_uuid() 'a1b2c3d4-e5f6-...'

Modules

uuid_random

UUID Object Generation

Generate random UUID objects.

Functions
uuid_random
uuid_random() -> uuid.UUID

Generate a random UUID (UUID4).

Returns:

Type Description
UUID

A randomly generated UUID object

Example

isinstance(uuid_random(), uuid.UUID) True

uuid_hex

UUID Hexadecimal Generation

Generate random UUIDs as hexadecimal strings.

Functions
uuid_hex
uuid_hex() -> str

Generate a random UUID and return it as a 32-character hexadecimal string.

Returns:

Type Description
str

UUID as a 32-character hexadecimal string (no hyphens)

Example

len(uuid_hex()) 32

uuid_string

UUID String Generation

Generate random UUIDs as formatted strings.

Functions
uuid_string
uuid_string() -> str

Generate a random UUID and return it as a string in standard form.

Returns:

Type Description
str

UUID as a string in standard form (with hyphens)

Example

'-' in uuid_string() True len(uuid_string()) 36

uuid_from_name

Rite - UUID Module
Functions
from_name
from_name(namespace, name)

Generates a UUID based on a namespace and a name.

Parameters:

Name Type Description Default
namespace UUID

The namespace UUID.

required
name str

The name from which to generate the UUID.

required
Returns
uuid.UUID: A UUID generated from the namespace and name.

uuid_is_valid

UUID Validation

Validate UUID strings.

Functions
is_valid_uuid
is_valid_uuid(uuid_to_test: str, version: int = 4) -> bool

Check if uuid_to_test is a valid UUID.

Parameters:

Name Type Description Default
uuid_to_test str

String to validate as UUID

required
version int

UUID version to check (1, 2, 3, or 4)

4

Returns:

Type Description
bool

True if valid UUID, False otherwise

Example

is_valid_uuid('550e8400-e29b-41d4-a716-446655440000') True is_valid_uuid('invalid') False

uuid_is_random

Rite - UUID Module
Functions
is_random_uuid
is_random_uuid(uuid_obj)

Checks if the UUID is randomly generated (UUID4).

Parameters:

Name Type Description Default
uuid_obj UUID

The UUID object.

required
Returns
bool: True if the UUID is a random UUID, False otherwise.

uuid_get_version

Rite - UUID Module
Functions
get_version
get_version(uuid_obj)

Extracts the version of a UUID.

Parameters:

Name Type Description Default
uuid_obj UUID

The UUID object.

required
Returns
int: The version number of the UUID.

Secure Random

Cryptographically secure random generation.

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

Ciphers

Classical cipher implementations.

Rite - Cryptography - Cipher Module

This module serves as the central import hub for all classical ciphers implemented in the Rite cryptography toolkit. It exposes both encode and decode functions for a variety of historical and educational ciphers.

Available ciphers
  • Atbash
  • Autokey
  • Baconian
  • Caesar
  • Four-Square
  • Playfair
  • Rail Fence
  • Rot13
  • Scytale
  • Transposition
  • Vigenère
  • XOR
References

Modules

cipher_caesar

Rite - Cryptography - Cipher - Caesar Cipher Module

Provides functionality to encode and decode text using the Caesar cipher.

The Caesar cipher is a substitution cipher that shifts characters by a fixed offset.

References
Functions
decode_caesar_cipher
decode_caesar_cipher(encoded_text: str, shift: int) -> str

Decode Caesar cipher text by reversing the shift.

Parameters:

Name Type Description Default
encoded_text str

The ciphertext to decode.

required
shift int

The original shift used during encoding.

required

Returns:

Type Description
str

Decoded plaintext string.

encode_caesar_cipher
encode_caesar_cipher(text: str, shift: int) -> str

Encode text using the Caesar cipher.

Only alphabetic characters are shifted. Case is preserved. Other characters remain unchanged.

Parameters:

Name Type Description Default
text str

The plaintext string to encode.

required
shift int

The number of positions to shift (can be positive or negative).

required

Returns:

Type Description
str

Encoded ciphertext string.

cipher_rot13

Rite - Cryptography - Cipher - Rot13 Cipher Module

Provides functionality to encode and decode text using the Rot13 cipher.

The Rot13 cipher shifts each letter 13 places in the alphabet and is symmetric (i.e., encoding and decoding are the same operation).

References
Functions
decode_rot13_cipher
decode_rot13_cipher(text: str) -> str

Decode text using the Rot13 cipher.

This is functionally identical to encoding, as Rot13 is symmetric.

Parameters:

Name Type Description Default
text str

The Rot13-encoded text.

required

Returns:

Type Description
str

The decoded text.

encode_rot13_cipher
encode_rot13_cipher(text: str) -> str

Encode or decode text using the Rot13 cipher.

Since Rot13 is symmetric, this function is used for both encoding and decoding.

Parameters:

Name Type Description Default
text str

The input text to encode or decode.

required

Returns:

Type Description
str

The transformed text.

cipher_vigenere

Rite - Cryptography - Cipher - Vigenère Cipher Module

Provides functionality to encode and decode text using the Vigenère cipher.

The Vigenère cipher is a method of encrypting alphabetic text by using a simple form of polyalphabetic substitution based on a keyword.

References
Functions
decode_vigenere_cipher
decode_vigenere_cipher(encoded_text: str, key: str) -> str

Decode text from the Vigenère cipher.

Non-alphabet characters are preserved.

Parameters:

Name Type Description Default
encoded_text str

The ciphertext to decode.

required
key str

The keyword originally used to encode.

required

Returns:

Type Description
str

The decoded plaintext.

encode_vigenere_cipher
encode_vigenere_cipher(text: str, key: str) -> str

Encode text using the Vigenère cipher.

Non-alphabet characters are preserved.

Parameters:

Name Type Description Default
text str

The plaintext to encode.

required
key str

The keyword used to perform letter shifts.

required

Returns:

Type Description
str

The encoded ciphertext.

cipher_atbash

Rite - Cryptography - Cipher - Atbash Cipher Module

Provides functionality to encode and decode text using the Atbash cipher.

The Atbash cipher is a classical substitution cipher that reverses the alphabet (A ↔ Z, B ↔ Y, ..., a ↔ z).

References
Functions
decode_atbash_cipher
decode_atbash_cipher(text: str) -> str

Decode text using the Atbash cipher.

This is functionally identical to encoding, since Atbash is symmetric.

Parameters:

Name Type Description Default
text str

The Atbash-encoded string.

required

Returns:

Type Description
str

The decoded string (same as encoding).

encode_atbash_cipher
encode_atbash_cipher(text: str) -> str

Encode (or decode) text using the Atbash cipher.

Since Atbash is symmetric, the same function can be used for encoding and decoding.

Parameters:

Name Type Description Default
text str

Input string to encode or decode.

required

Returns:

Type Description
str

The Atbash-encoded/decoded string.

cipher_xor

Rite - Cryptography - Cipher - XOR Cipher Module

Provides functionality to encode and decode text using the XOR cipher.

The XOR cipher is a symmetric key encryption technique that uses the XOR (bitwise exclusive OR) operation between the plaintext and a repeating key.

References
Functions
decode_xor_cipher
decode_xor_cipher(text: str, key: str) -> str

Decode XOR-encoded text using the same key.

This function is equivalent to encode_xor_cipher().

Parameters:

Name Type Description Default
text str

The XOR-encoded string.

required
key str

The encryption key used for encoding.

required

Returns:

Type Description
str

The decoded plaintext string.

encode_xor_cipher
encode_xor_cipher(text: str, key: str) -> str

Encode or decode text using the XOR cipher.

Since XOR is symmetric, this function performs both encryption and decryption.

Parameters:

Name Type Description Default
text str

The plaintext or ciphertext string.

required
key str

The encryption key.

required

Returns:

Name Type Description
str

A string with the result of XOR encryption/decryption.

Note str

Non-printable characters may be present in the result.

Examples

Hashing

from rite.crypto import hash_sha256, hash_blake2b

# SHA-256 hash
data = "sensitive data"
hashed = hash_sha256(data)

# BLAKE2b hash
hashed_b = hash_blake2b(data.encode())

UUID Operations

from rite.crypto import (
    uuid_random,
    uuid_hex,
    uuid_is_valid,
    uuid_from_name
)

# Generate random UUID
random_id = uuid_random()

# Get hex representation
hex_id = uuid_hex(random_id)

# Validate UUID
is_valid = uuid_is_valid(hex_id)

# Generate UUID from name
namespace_uuid = uuid_random()
name_uuid = uuid_from_name(namespace_uuid, "example.com")

Secure Random Generation

from rite.crypto import random_bytes, random_hex, random_int

# Generate random bytes
secret_key = random_bytes(32)

# Generate random hex string
token = random_hex(16)

# Generate random integer
nonce = random_int(0, 1000000)