Hashing
provide.foundation.crypto.hashing
¶
TODO: Add module docstring.
Classes¶
Functions¶
hash_chunks
¶
Hash an iterator of byte chunks.
Useful for hashing data that comes in chunks, like from a network stream.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunks
|
Iterator[bytes]
|
Iterator yielding byte chunks |
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_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_file_multiple
¶
hash_file_multiple(
path: Path | str,
algorithms: list[str],
chunk_size: int = DEFAULT_CHUNK_SIZE,
) -> dict[str, str]
Hash a file with multiple algorithms in a single pass.
This is more efficient than calling hash_file multiple times.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
File path |
required |
algorithms
|
list[str]
|
List of hash algorithms |
required |
chunk_size
|
int
|
Size of chunks to read at a time |
DEFAULT_CHUNK_SIZE
|
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/hashing.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 |