Skip to content
AITroveRead. Build. Understand.
Make this comfortable

Python artifact hashes: verify owned bytes against a separately trusted expectation

Last updated: 30 Sept 20264 min read
tutorial
IntermediateBy AITrove Editorial

A cryptographic digest identifies the exact bytes of an artifact relative to an expected digest selected through a separate trust path.

Download Python source kit

Operation contract

The verifier accepts at most 4096 received bytes and a 64-character lowercase hexadecimal SHA-256 expectation. It hashes the bytes and compares the result with the expectation. The owned receipt artifact passes; changing one byte fails. A malformed expectation is rejected as a schema error rather than being treated as a mismatched release. The example does not download, unpack or install anything.

Failure and ownership boundary

A digest stored beside an untrusted artifact does not establish the publisher’s identity, because an attacker can replace both. A release pipeline needs an authenticated expectation, dependency selection and archive/install boundaries too. Python wheel metadata: inspect an owned built artifact before installation, Python wheel installation: test the built artifact outside its source directory and Python HMAC envelopes: authenticate exact bytes and separate replay policy describe distinct checks. SHA-256 collision resistance is not a guarantee that the contents are safe to execute.

Working program

python
import hashlib
import hmac

def artifact_matches(payload, expected):
    if type(payload) is not bytes or len(payload) > 4096:
        raise ValueError("artifact byte cap")
    if type(expected) is not str or len(expected) != 64 or any(char not in "0123456789abcdef" for char in expected):
        raise ValueError("digest format")
    actual = hashlib.sha256(payload).hexdigest()
    return hmac.compare_digest(actual, expected)

owned_artifact = b"receipt-schema:v1\namount:integer\n"
trusted_expectation = hashlib.sha256(owned_artifact).hexdigest()
print("owned bytes:", artifact_matches(owned_artifact, trusted_expectation))
print("altered bytes:", artifact_matches(owned_artifact + b"x", trusted_expectation))
try:
    artifact_matches(owned_artifact, "not-a-digest")
except ValueError:
    print("digest schema rejected")

Output

Output
owned bytes: True
altered bytes: False
digest schema rejected

Costs and limits

Hashing n bytes takes O(n) byte work, with bounded hash state. This program already receives the full bytes in memory, so its reception allocation is O(n); a large-artifact implementation should hash bounded chunks while enforcing a total-byte cap. Verification here does not examine archive members or dependency graphs.

Common Mistakes

  • The expected digest needs a trust source separate from received bytes.
  • Matching a digest does not mean an artifact is safe to import or install.

Connected lessons

Python wheel metadata: inspect an owned built artifact before installation, Python wheel installation: test the built artifact outside its source directory, Python HMAC envelopes: authenticate exact bytes and separate replay policy.

Check this related boundary

Python wheel RECORD: verify member bytes before trusting a local archive, Python packaging: inspect an imported module origin before using it.

python
artifact-digest-check
Storage details