Prefixed
provide.foundation.crypto.prefixed
¶
TODO: Add module docstring.
Functions¶
format_checksum
¶
Calculate checksum with algorithm prefix.
Returns checksums in the format "algorithm:hexdigest" (e.g., "sha256:abc123..."). This format enables self-describing checksums that include the algorithm used.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
Data to checksum |
required |
algorithm
|
str
|
Hash algorithm (sha256, sha512, blake2b, blake2s, md5, adler32) |
DEFAULT_ALGORITHM
|
Returns:
| Type | Description |
|---|---|
str
|
Prefixed checksum string (e.g., "sha256:abc123...") |
Raises:
| Type | Description |
|---|---|
ValueError
|
If algorithm is not supported |
Example
data = b"Hello, World!" format_checksum(data, "sha256") 'sha256:dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f' format_checksum(data, "adler32") 'adler32:1c49043e'
Source code in provide/foundation/crypto/prefixed.py
is_strong_checksum
¶
Check if a checksum uses a cryptographically strong algorithm.
Strong algorithms are suitable for security-critical applications. Weak algorithms like MD5 and Adler32 should only be used for non-security purposes like data integrity checks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
checksum_str
|
str
|
Prefixed checksum string |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if using a strong algorithm (sha256, sha512, blake2b, blake2s) |
Example
is_strong_checksum("sha256:abc123") True is_strong_checksum("md5:abc123") False is_strong_checksum("adler32:deadbeef") False
Source code in provide/foundation/crypto/prefixed.py
normalize_checksum
¶
Normalize a checksum string to prefixed format.
Ensures the checksum is in the standard "algorithm:value" format and validates both the algorithm and value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
checksum_str
|
str
|
Checksum string to normalize |
required |
Returns:
| Type | Description |
|---|---|
str
|
Normalized checksum with prefix |
Raises:
| Type | Description |
|---|---|
ValueError
|
If checksum format is invalid |
Example
normalize_checksum("sha256:ABC123") 'sha256:abc123'
Source code in provide/foundation/crypto/prefixed.py
parse_checksum
¶
Parse algorithm and value from a prefixed checksum string.
Requires prefixed format ("algorithm:hexvalue"). This enables validation of both the algorithm and the checksum value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
checksum_str
|
str
|
Prefixed checksum string |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, str]
|
Tuple of (algorithm, hex_value) |
Raises:
| Type | Description |
|---|---|
ValueError
|
If checksum format is invalid or algorithm is unsupported |
Example
parse_checksum("sha256:abc123") ('sha256', 'abc123') parse_checksum("invalid") ValueError: Checksum must use prefixed format (algorithm:value)
Source code in provide/foundation/crypto/prefixed.py
verify_checksum
¶
Verify data against a prefixed checksum string.
Automatically extracts the algorithm from the checksum string and performs verification using the appropriate algorithm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
Data to verify |
required |
checksum_str
|
str
|
Expected prefixed checksum (e.g., "sha256:abc123...") |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if checksum matches, False otherwise |
Example
data = b"test data" checksum = format_checksum(data, "sha256") verify_checksum(data, checksum) True verify_checksum(b"wrong data", checksum) False