Hash MAC and signature selection
Choose the guarantee first
A hash maps bytes to a fixed-size digest; SHA-256 (SHA-2) and SHA3-256 are examples. A digest compared with an independently trusted expected digest detects corruption, but anyone can compute a new digest. HMAC combines a secret key and hash to authenticate data among key holders. A digital signature lets holders of a trusted public verification key check a signature made with the private signing key. None of these alone hides the message.
Match the trust relationship
For a downloaded public release, verify a signature against a trusted release key, not a key bundled by an untrusted sender. For two services sharing a secret, HMAC-SHA-256 may fit an established message-authentication protocol. For corruption checks against a trusted manifest, SHA-256 may suffice. Public-key encryption instead protects data for a recipient holding a private decryption key; it does not automatically authenticate the sender. Use standardized hybrid encryption or protocols, not a homemade composition.
Compare bytes with a library
This computes a digest of three ASCII bytes. The expected output is the standard SHA-256 digest of abc. The digest is not a password hash or proof of sender identity.
import hashlibprint(hashlib.sha256(b"abc").hexdigest())Use hmac.compare_digest for HMAC verification rather than a hand-written byte comparison. For Ed25519 use the library verify method; an invalid signature raises InvalidSignature.
Derive keys for distinct uses
A key derivation function (KDF) creates key material from input key material according to a defined construction. HKDF uses HMAC and supports context separation through an info input. An established protocol specifies salt, info, hash and output length; do not improvise these values for a new protocol. HKDF is not a password-hardening function. Prefer purpose-specific keys and keep private signing keys under separate access controls.
Practice choosing primitives
Choose a primitive for a trusted backup digest, a shared-secret message check, a public release signature and confidential invoice storage. Then verify RFC 8032 Ed25519 test 1 using its public key and signature. A changed message must fail. Explain why a successful signature does not establish that the signer is authorized to approve a release.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Hash MAC and signature selection — knowledge check
Apply cryptographic library principles to fictional scenarios.
What verifies a public release with a trusted public key?
What fits a shared-secret message authentication requirement?
What is HKDF not intended to do?
A valid signature proves release approval?
What does public-key encryption alone not establish?