Checksums
provide.foundation.crypto.checksums
¶
TODO: Add module docstring.
Classes¶
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
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
verify_checksum_file
¶
verify_checksum_file(
checksum_file: Path | str,
base_dir: Path | str | None = None,
algorithm: str = DEFAULT_ALGORITHM,
stop_on_error: bool = False,
) -> tuple[list[str], list[str]]
Verify all files listed in a checksum file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
checksum_file
|
Path | str
|
Path to checksum file |
required |
base_dir
|
Path | str | None
|
Base directory for relative paths (defaults to checksum file dir) |
None
|
algorithm
|
str
|
Hash algorithm to use |
DEFAULT_ALGORITHM
|
stop_on_error
|
bool
|
Whether to stop on first verification failure |
False
|
Returns:
| Type | Description |
|---|---|
tuple[list[str], list[str]]
|
Tuple of (verified_files, failed_files) |
Raises:
| Type | Description |
|---|---|
ResourceError
|
If checksum file cannot be read |
Source code in provide/foundation/crypto/checksums.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 |