cdm_data_loaders.utils.checksums ¶
Algorithm-agnostic checksum helpers.
ChecksumEntry
dataclass
¶
A single expected checksum, tied to a specific algorithm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
algorithm
|
str
|
hashlib algorithm name the checksum was generated with |
required |
value
|
str
|
expected digest, as lowercase hex |
required |
Source code in src/cdm_data_loaders/utils/checksums.py
Hasher ¶
Bases: Protocol
Minimal interface satisfied by both hashlib hash objects and Crc64NvmeHasher.
Source code in src/cdm_data_loaders/utils/checksums.py
update ¶
Update the hash object with data, a bytes-like object.
Repeated calls are equivalent to a single call with the concatenation of all the arguments: m.update(a); m.update(b) is equivalent to m.update(a+b).
Source code in src/cdm_data_loaders/utils/checksums.py
hexdigest ¶
Return the digest of the data passed to the update() method so far.
The digest is returned as a string object of double length, containing only hexadecimal digits.
Returns:
| Type | Description |
|---|---|
str
|
digest of data so far, as a string containing only hexadecimal digits |
Source code in src/cdm_data_loaders/utils/checksums.py
Crc64NvmeHasher ¶
Adapts awscrt's stateless crc64nvme(data, prev) function to a hashlib-like interface.
Source code in src/cdm_data_loaders/utils/checksums.py
HashingReader ¶
Wraps a file-like object, computing a hash of everything read through it.
Transparently proxies attribute access (e.g. close) to the wrapped
object so it remains compatible with s3transfer's fileobj introspection.
Deliberately does not implement seek/tell itself, so non-seekable
streams (such as requests' response.raw) are still treated as
non-seekable by anything inspecting the wrapper.
Source code in src/cdm_data_loaders/utils/checksums.py
__init__ ¶
Initialise a hashing wrapper around a readable, file-like object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw
|
Any
|
the underlying file-like object to wrap; must implement |
required |
algorithm
|
str
|
hashlib algorithm name to use for hashing, e.g. "md5" |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
if |
Source code in src/cdm_data_loaders/utils/checksums.py
read ¶
Read up to amt bytes from the wrapped object, updating the running hash.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
amt
|
int
|
number of bytes to read; -1 reads all remaining data, defaults to -1 |
-1
|
Returns:
| Type | Description |
|---|---|
bytes
|
the bytes read |
Source code in src/cdm_data_loaders/utils/checksums.py
__getattr__ ¶
Proxy any attribute not defined on this wrapper to the wrapped object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
str
|
attribute name being accessed |
required |
Returns:
| Type | Description |
|---|---|
Any
|
the attribute value from the wrapped object |
Raises:
| Type | Description |
|---|---|
AttributeError
|
if |
Source code in src/cdm_data_loaders/utils/checksums.py
hexdigest ¶
Return the hex digest of all bytes read so far.
Returns:
| Type | Description |
|---|---|
str
|
hex digest of the accumulated hash |
b64digest ¶
Return the base64 digest of all bytes read so far.
Only supported for algorithms whose hasher implements b64digest
(currently crc64nvme), matching S3's ChecksumCRC64NVME representation.
Returns:
| Type | Description |
|---|---|
str
|
base64-encoded digest of the accumulated hash |
Raises:
| Type | Description |
|---|---|
AttributeError
|
if the underlying algorithm's hasher does not support base64 digests |
Source code in src/cdm_data_loaders/utils/checksums.py
compute_stream_checksum ¶
compute_stream_checksum(
stream: IO[bytes],
algorithm: str,
chunk_size: int = HASH_CHUNK_BYTES,
) -> str
Compute a checksum from an open binary stream using any algorithm new_hasher supports.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stream
|
IO[bytes]
|
readable binary stream |
required |
algorithm
|
str
|
hashlib algorithm name, or "crc64nvme" |
required |
chunk_size
|
int
|
read chunk size in bytes |
HASH_CHUNK_BYTES
|
Returns:
| Type | Description |
|---|---|
str
|
hex digest, or base64 digest for crc64nvme (matching S3's format) |
Source code in src/cdm_data_loaders/utils/checksums.py
compute_file_checksum ¶
Compute a checksum for a local file using any algorithm new_hasher supports.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str | Path
|
path to the file |
required |
algorithm
|
str
|
hashlib algorithm name, or "crc64nvme" |
required |
Returns:
| Type | Description |
|---|---|
str
|
hex digest, or base64 digest for crc64nvme (matching S3's format) |
Source code in src/cdm_data_loaders/utils/checksums.py
validate_checksum_fn ¶
Validate a checksum/hash algorithm name against hashlib + extra algorithms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
checksum_fn
|
str
|
name of the algorithm to validate, e.g. "md5" or "sha256" |
required |
include_extras
|
bool
|
whether to include the non-hashlib algorithms, defaults to True |
True
|
Returns:
| Type | Description |
|---|---|
str
|
the validated algorithm name, lowercased |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the algorithm is not supported |
Source code in src/cdm_data_loaders/utils/checksums.py
new_hasher ¶
Construct a Hasher for any supported algorithm (hashlib or crc64nvme).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
algorithm
|
str
|
algorithm name |
required |
include_extras
|
bool
|
whether to include the non-hashlib algorithms, defaults to True |
True
|
Returns:
| Type | Description |
|---|---|
Hasher
|
description |