contentdigest
Build and verify the HTTP Content-Digest and Repr-Digest fields of
RFC 9530, and read and answer the
Want-Content-Digest / Want-Repr-Digest preference fields. Typed, streaming, zero
dependencies.
Install
pip install contentdigest
Build and check a field
from contentdigest import make_digest, parse, verify
body = b'{"hello": "world"}'
field = make_digest(body)
print(field)
# sha-256=:X48E9qOokqqrvdts8nOJRJN3OWDUoyWxBf7kbu9DBPE=:
print(make_digest(body, algos=["sha-512", "sha-256"]).split(", ")[1] == field)
# True
print(parse(field)["sha-256"].hex()[:16])
# 5f8f04f6a3a892aa
print(verify(field, body), verify(field, b"tampered"))
# True False
The value is the same for both fields, so the string works as Content-Digest or as
Repr-Digest. What changes is which bytes you hash: Content-Digest covers the message
content as sent (after content coding, only the range you send), Repr-Digest covers the
whole selected representation (RFC 9530 sections 2 and 3).
Stream a large body
Digester builds a value from chunks. Verifier takes the received field first, as headers
arrive before the body, then the chunks.
from contentdigest import Digester, Verifier
digester = Digester(["sha-256", "sha-512"])
for chunk in (b'{"hello": ', b'"world"}'):
digester.update(chunk)
field = digester.value()
print(field.split(", ")[0])
# sha-256=:X48E9qOokqqrvdts8nOJRJN3OWDUoyWxBf7kbu9DBPE=:
verifier = Verifier(field)
print(verifier.algorithms)
# ('sha-256', 'sha-512')
for chunk in (b'{"hello": ', b'"world"}'):
verifier.update(chunk)
print(verifier.verify())
# True
Which algorithms count
from contentdigest import ACTIVE_ALGORITHMS, DigestError, make_digest, verify
print(sorted(ACTIVE_ALGORITHMS))
# ['sha-256', 'sha-512']
body = b'{"hello": "world"}'
legacy = make_digest(body, algos=["md5"])
print(legacy)
# md5=:Sd/dVLAcvNLSq16eXua5uQ==:
try:
verify(legacy, body)
except DigestError as error:
print(type(error).__name__)
# DigestError
print(verify(legacy, body, accept=["md5"]))
# True
All eight algorithms of the IANA registry are built in: sha-256 and sha-512
(ACTIVE_ALGORITHMS), and md5, sha, unixsum, unixcksum, adler and crc32c
(DEPRECATED_ALGORITHMS); SUPPORTED_ALGORITHMS is both. make_digest builds any of them
when asked. verify and Verifier check only the algorithms in accept, which defaults to
the Active ones, because RFC 9530 says the Deprecated ones MUST NOT be relied on where an
attacker could change the content. Widen accept only to catch accidental corruption.
Preference fields
from contentdigest import choose_algorithm, make_want, parse_want
print(parse_want("sha-512=3, sha-256=10, unixsum=0"))
# {'sha-512': 3, 'sha-256': 10, 'unixsum': 0}
print(choose_algorithm("sha-512=3, sha-256=10, unixsum=0"))
# sha-256
print(choose_algorithm("sha=10"))
# None
print(make_want({"sha-512": 10, "sha-256": 5}))
# sha-512=10, sha-256=5
choose_algorithm(field, supported=ACTIVE_ALGORITHMS) returns the supported algorithm with
the highest weight above 0, the first one in the field on a tie, and None when the field
is absent or names none of them. Preferences are only a hint: on None you may still send
any digest, or none. A malformed preference field raises DigestError; catch it if you
want to treat such a field as absent. parse_want reads a field and make_want writes one.
Command line
$ contentdigest body.json
sha-256=:X48E9qOokqqrvdts8nOJRJN3OWDUoyWxBf7kbu9DBPE=:
$ contentdigest -a sha-512 -a sha-256 body.json
$ contentdigest --verify "sha-256=:X48E9qOokqqrvdts8nOJRJN3OWDUoyWxBf7kbu9DBPE=:" body.json
match
The file is read in chunks; - or no file reads standard input. With --verify, the exit
status is 0 on a match, 1 on a mismatch and 2 on bad input; -a then sets the accepted
algorithms. python -m contentdigest does the same.
The exact rules
- Fields are parsed with a complete RFC 9651 structured-field parser that passes every
parsing test of the httpwg test suite. A field may be a
str, ASCIIbytes(as ASGI servers hand headers over) or an iterable of field lines, which are joined with commas. - Every member of a digest field must be a byte sequence, and every weight of a preference
field an integer from 0 to 10. Anything else makes the whole field invalid and raises
DigestError(aValueError), as RFC 9651 requires. Parameters are ignored. A repeated key keeps its last value. An empty field is an empty dict, the same as an absent field. - Byte sequences decode as RFC 9651 section 4.2.7 asks: missing
=padding and non-zero pad bits are accepted. So is one=beyond the padding needed, which some RFC 9530 examples print. More padding than that,=inside the data and characters outside the base64 alphabet are rejected. verifyandVerifierrequire every accepted member present to match, compare in constant time and run every comparison, and raiseDigestErrorwhen no member is accepted, since then there is nothing to check.- Bodies are any bytes-like object. Wrong argument types raise
TypeError, including a bare string where a collection of algorithm names is expected. - The checksums
unixsum(BSDsum),unixcksum(POSIXcksum),adlerandcrc32care big-endian, which is what reproduces the RFC 9530 Appendix D sample values. They are pure Python, so they are much slower than the hashes.
Why
RFC 9530 replaced the Digest field of RFC 3230 with Content-Digest and Repr-Digest,
which carry their digests as RFC 9651 structured-field dictionaries. Getting them right
means parsing structured fields properly, knowing which registered algorithms are safe to
rely on, and hashing while the body streams. This package does that one job, ships type
information and has no dependencies. (The unrelated python-digest on PyPI is an HTTP
Digest authentication helper.)
License
MIT. See LICENSE.
Links
- Website: https://lianes8studio.com
- GitHub: https://github.com/JaviLianes8
- LinkedIn: https://www.linkedin.com/in/jlianes/
Metadata
Release files for contentdigest 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| contentdigest-0.2.0.tar.gz | 28.8 kB | Without provenance | Details for contentdigest-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| contentdigest-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for contentdigest-0.2.0-py3-none-any.whl |
Total release size: 45.9 kB
Release files / contentdigest-0.2.0.tar.gz
| Download URL | contentdigest-0.2.0.tar.gz |
|---|---|
| Size | 28.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a1b82644be9bc24796b74d77c11605eae290c1226b2a2ae59b20be997ab4869d
|
|
BLAKE2b-256 checksum How to use checksums |
554e3bda8699e6bd35dd2467e21384cc763dbd0b94d0a03c5e02b3b67dc95a87
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.2
|
Release files / contentdigest-0.2.0-py3-none-any.whl
| Download URL | contentdigest-0.2.0-py3-none-any.whl |
|---|---|
| Size | 17.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
953ac91fe13453721fa460be811ad1a7b72deb44677c9afb5696caf3510a76f4
|
|
BLAKE2b-256 checksum How to use checksums |
deaafa0bfeec6708f8d74ea3b6451caa17869fa0ed38f88039e4f2fe174a6db6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.2
|
Security for contentdigest 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.