Crypto
provide.foundation.crypto
¶
TODO: Add module docstring.
Functions¶
calculate_checksums
¶
Calculate multiple checksums for a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
File path |
required |
algorithms
|
list[str] | None
|
List of algorithms (defaults to sha256 and md5) |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Dictionary mapping algorithm name to hex digest |
Raises:
| Type | Description |
|---|---|
ResourceError
|
If file cannot be read |
ValidationError
|
If any algorithm is not supported |
Source code in provide/foundation/crypto/checksums.py
compare_hash
¶
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
format_hash
¶
Format a hash value for display.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hash_value
|
str
|
Hash value to format |
required |
group_size
|
int
|
Number of characters per group |
8
|
groups
|
int
|
Number of groups to show (0 for all) |
0
|
separator
|
str
|
Separator between groups |
' '
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted hash string |
Examples:
>>> format_hash("abc123def456", group_size=4, separator="-")
"abc1-23de-f456"
>>> format_hash("abc123def456", group_size=4, groups=2)
"abc1 23de"
Source code in provide/foundation/crypto/utils.py
get_hasher
¶
Get a hash object for the specified algorithm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
algorithm
|
str
|
Hash algorithm name |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Hash object from hashlib |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If algorithm is not supported |
Source code in provide/foundation/crypto/algorithms.py
hash_data
¶
Hash binary data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
Data to hash |
required |
algorithm
|
str
|
Hash algorithm |
DEFAULT_ALGORITHM
|
Returns:
| Type | Description |
|---|---|
str
|
Hex digest |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If algorithm is not supported |
Source code in provide/foundation/crypto/hashing.py
hash_file
¶
hash_file(
path: Path | str,
algorithm: str = DEFAULT_ALGORITHM,
chunk_size: int = DEFAULT_CHUNK_SIZE,
) -> str
Hash a file's contents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
File path |
required |
algorithm
|
str
|
Hash algorithm (sha256, sha512, md5, etc.) |
DEFAULT_ALGORITHM
|
chunk_size
|
int
|
Size of chunks to read at a time |
DEFAULT_CHUNK_SIZE
|
Returns:
| Type | Description |
|---|---|
str
|
Hex digest of file hash |
Raises:
| Type | Description |
|---|---|
ResourceError
|
If file cannot be read |
ValidationError
|
If algorithm is not supported |
Source code in provide/foundation/crypto/hashing.py
hash_name
¶
Generate a 64-bit hash of a string for fast lookup.
This is useful for creating numeric identifiers from strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
String to hash |
required |
Returns:
| Type | Description |
|---|---|
int
|
64-bit integer hash |
Source code in provide/foundation/crypto/utils.py
hash_stream
¶
hash_stream(
stream: BinaryIO,
algorithm: str = DEFAULT_ALGORITHM,
chunk_size: int = DEFAULT_CHUNK_SIZE,
) -> str
Hash data from a stream.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stream
|
BinaryIO
|
Binary stream to read from |
required |
algorithm
|
str
|
Hash algorithm |
DEFAULT_ALGORITHM
|
chunk_size
|
int
|
Size of chunks to read at a time |
DEFAULT_CHUNK_SIZE
|
Returns:
| Type | Description |
|---|---|
str
|
Hex digest |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If algorithm is not supported |
Source code in provide/foundation/crypto/hashing.py
hash_string
¶
Hash a text string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to hash |
required |
algorithm
|
str
|
Hash algorithm |
DEFAULT_ALGORITHM
|
encoding
|
str
|
Text encoding |
'utf-8'
|
Returns:
| Type | Description |
|---|---|
str
|
Hex digest |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If algorithm is not supported |
Source code in provide/foundation/crypto/hashing.py
is_secure_algorithm
¶
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
parse_checksum_file
¶
Parse a checksum file and return filename to hash mapping.
Supports common checksum file formats: - SHA256: "hash filename" or "hash filename" - MD5: "hash filename" or "hash filename" - SHA256SUMS: "hash filename" - MD5SUMS: "hash filename"
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
Path to checksum file |
required |
algorithm
|
str | None
|
Expected algorithm (for validation) |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Dictionary mapping filename to hash |
Raises:
| Type | Description |
|---|---|
ResourceError
|
If file cannot be read |
Source code in provide/foundation/crypto/checksums.py
quick_hash
¶
Generate a quick non-cryptographic hash for lookups.
This uses Python's built-in hash function which is fast but not cryptographically secure. Use only for hash tables and caching.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
Data to hash |
required |
Returns:
| Type | Description |
|---|---|
int
|
32-bit hash value |
Source code in provide/foundation/crypto/utils.py
validate_algorithm
¶
Validate that a hash algorithm is supported.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
algorithm
|
str
|
Hash algorithm name |
required |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If algorithm is not supported |
Source code in provide/foundation/crypto/algorithms.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
Source code in provide/foundation/crypto/prefixed.py
verify_data
¶
Verify data matches an expected hash.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
Data to verify |
required |
expected_hash
|
str
|
Expected hash value |
required |
algorithm
|
str
|
Hash algorithm |
DEFAULT_ALGORITHM
|
Returns:
| Type | Description |
|---|---|
bool
|
True if hash matches, False otherwise |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If algorithm is not supported |
Source code in provide/foundation/crypto/checksums.py
verify_file
¶
Verify a file matches an expected hash.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
File path |
required |
expected_hash
|
str
|
Expected hash value |
required |
algorithm
|
str
|
Hash algorithm |
DEFAULT_ALGORITHM
|
Returns:
| Type | Description |
|---|---|
bool
|
True if hash matches, False otherwise |
Raises:
| Type | Description |
|---|---|
ResourceError
|
If file cannot be read |
ValidationError
|
If algorithm is not supported |
Source code in provide/foundation/crypto/checksums.py
write_checksum_file
¶
write_checksum_file(
checksums: dict[str, str],
path: Path | str,
algorithm: str = DEFAULT_ALGORITHM,
binary_mode: bool = True,
) -> None
Write checksums to a file in standard format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
checksums
|
dict[str, str]
|
Dictionary mapping filename to hash |
required |
path
|
Path | str
|
Path to write checksum file |
required |
algorithm
|
str
|
Algorithm name (for comments) |
DEFAULT_ALGORITHM
|
binary_mode
|
bool
|
Whether to use binary mode indicator (*) |
True
|
Raises:
| Type | Description |
|---|---|
ResourceError
|
If file cannot be written |