Blake2b Keyed Hashes Authenticate Bytes
hashlib.blake2b takes a key parameter, turning an ordinary digest into a keyed MAC whose digest_size you can shorten to fit tight headers.
Code
import hashlib
key = b"release-signing-key"
def tag(payload: bytes) -> str:
# blake2b 的 key 参数把普通摘要变成带密钥的 MAC,digest_size 可截短
return hashlib.blake2b(payload, key=key, digest_size=16).hexdigest()
manifest = b"artifact=app.tar.gz+count=42"
seal = tag(manifest)
print("tag:", seal)
# 同一密钥重算一致:内容没变
print("untouched matches:", tag(manifest) == seal)
# 内容哪怕动一个字节,摘要整体改变
print("tampered differs :", tag(b"artifact=app.tar.gz+count=43") != seal)Output
Captured from a sandboxed run of this snippet in the build pipeline.
tag: 1532fcec7680a811af392faa29b21720
untouched matches: True
tampered differs : TrueHow it works
Count the function tag as the entire surface area of Blake2b Keyed Hashes Authenticate Bytes.
Putting it to use
Entry signature: def tag(payload: bytes) -> str
Running the file prints labeled results such as tag:, untouched matches:, tampered differs : — each operation’s effect is visible in the output.
Import tag from the file and call it — the rest of the code is implementation detail.
Core lines
def tag(payload: bytes) -> str:
# blake2b 的 key 参数把普通摘要变成带密钥的 MAC,digest_size 可截短
return hashlib.blake2b(payload, key=key, digest_size=16).hexdigest()
Interactive Tools for This Topic
Argon2id Hash InfoEstimate Argon2id password-hash latency and memory usage, and check your parameters against OWASP 2024 minimum and recommended settings.bcrypt Cost EstimatorEstimate bcrypt cost factor latency and brute-force resistance, and get a recommended cost for your target hash time and attacker throughput.Byte Size ConverterShow one byte count in every unit side by side, using decimal (kB/MB/GB) or binary (KiB/MiB/GiB) standards.CORS Header Policy DebuggerSimulate browser CORS preflight and credential policies against server response headers to pinpoint blocked origins, methods, or headers and fix them.
FAQ
- Does the code pull in any external packages?
- No external packages are involved; every call resolves to a built-in API. The file defines `tag` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.