Hashing¶
The rite.crypto.hash submodule provides wrappers around Python's hashlib for computing common cryptographic hashes.
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(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:
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:
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(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(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(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(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(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:
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:
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(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_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(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:
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(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:
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(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:
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:
options: show_root_heading: true show_source: false heading_level: 2