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.

PythonSecurity Boundaries

Code

snippet
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 : True

How 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

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.