November 23, 2026
Modern Hash-Based Protocol Design: Building Security Beyond Hash Functions
Hashing From First Principles β Part 5

By Chulanka Lakmana Thabrew
7 min read
A cryptographic hash function alone can't secure a system. Understanding how hashes work with authentication, key derivation, digital signatures, and commitments is where real cryptographic engineering begins.
Introduction: A Secure Hash Does Not Automatically Mean a Secure System
In the previous articles of this series, we explored the evolution of cryptographic hashing, from MD4 and MD5 to the modern SHA family.
We learned how SHA-256 and SHA-3 produce fixed-length digests, why collision resistance matters, and how cryptographic algorithms have evolved to address security weaknesses. But there is something more important I want to explore.
Imagine developing a secure communication system. You use SHA-256 to hash every message exchanged between two users. Does that automatically make the communication secure?
No, because an attacker might intercept a message, modify its contents, calculate a new SHA-256 digest, and send both the modified message and its new digest to the receiver.
The hash function itself has not been broken. The problem is that a normal hash doesn't authenticate who created the message. This is where modern hash-based protocol design becomes important.
In this article, we will explore how cryptographic hashes are combined with other mechanisms to provide authentication, integrity, key derivation, and stronger security guarantees.
1. What Is Hash-Based Protocol Design?
A cryptographic protocol is a set of rules that defines how different systems communicate and achieve security objectives.
These objectives can include:
- Data integrity.
- Authentication.
- Confidentiality.
- Secure key establishment.
- Replay protection.
- Verification of data authenticity.
Hash functions are important building blocks in many of these protocols. However, a hash function is not a complete security mechanism. For example:
Sender Receiver
| |
|------ Message ---------->|
| |
|------ SHA-256 Hash ------>|
| |
| Recalculate Hash
| |
| Compare DigestsSender Receiver
| |
|------ Message ---------->|
| |
|------ SHA-256 Hash ------>|
| |
| Recalculate Hash
| |
| Compare DigestsThis approach can detect accidental changes if the expected digest is obtained through a trusted channel. But it can't independently prevent an attacker from modifying both the message and its digest.
To solve this problem, we need mechanisms that provide authentication. Let's explore some of them.
2. HMAC: Hash-Based Message Authentication Code
One of the most important constructions built around cryptographic hash functions is HMAC. HMAC stands for Hash-Based Message Authentication Code.
It combines a cryptographic hash function with a secret key to provide message integrity and authentication. Unlike an ordinary hash, HMAC requires both communicating parties to know a shared secret key.
Conceptually:
Secret Key
|
v
Message ------> HMAC Function
|
v
HMAC Tag
|
v
Receiver
|
Recalculate HMAC
|
v
Verify Tag Secret Key
|
v
Message ------> HMAC Function
|
v
HMAC Tag
|
v
Receiver
|
Recalculate HMAC
|
v
Verify TagIf the message or authentication tag is modified, verification should fail, assuming a secure implementation and an uncompromised key.
How Does HMAC Work?
HMAC uses a hash function, a secret key, and two predefined padding values called the inner pad and outer pad. Its construction is:
HMAC(K, m) = H( (K XOR opad) || H((K XOR ipad) || m))
Where:
- K & m represent the secret key and the message.
- H represents the cryptographic hash function.
- ipad & opad represent the inner padding & the outer padding.
- || represents concatenation and XOR represents exclusive OR.
The construction applies the hash function twice, using different key-derived inputs. This design helps protect against attacks that can affect naΓ―ve constructions such as simply hashing a secret key followed by a message.
Practical Python Example
Python supports HMAC through its hmac module.
import hmac
import hashlib
secret_key = b"my_secret_key"
message = b"Transfer $100"
tag = hmac.new(
secret_key,
message,
hashlib.sha256
).hexdigest()
print("HMAC:", tag)
HMAC: 288bbce81c4de739f2b0ea6679c8969bbeb24691be1a8a707523c91ae6474216import hmac
import hashlib
secret_key = b"my_secret_key"
message = b"Transfer $100"
tag = hmac.new(
secret_key,
message,
hashlib.sha256
).hexdigest()
print("HMAC:", tag)
HMAC: 288bbce81c4de739f2b0ea6679c8969bbeb24691be1a8a707523c91ae6474216To verify the message:
received_tag = tag
expected_tag = hmac.new(
secret_key,
message,
hashlib.sha256
).hexdigest()
if hmac.compare_digest(received_tag, expected_tag):
print("Message authenticated")
else:
print("Authentication failed")
Message authenticatedreceived_tag = tag
expected_tag = hmac.new(
secret_key,
message,
hashlib.sha256
).hexdigest()
if hmac.compare_digest(received_tag, expected_tag):
print("Message authenticated")
else:
print("Authentication failed")
Message authenticatedThe compare_digest() function is important because it helps avoid timing leaks associated with ordinary equality comparisons. HMAC is widely used in authentication systems, APIs, and secure communication protocols.
Important: HMAC provides authentication and integrity, not encryption. It doesn't hide the contents of a message.
3. HKDF: Turning Shared Secrets Into Cryptographic Keys
Another important hash-based construction is HKDF, which stands for HMAC-based Key Derivation Function.
Imagine two systems establish a shared secret through a key-exchange mechanism. Can they directly use that secret for every cryptographic operation?
It is generally better to derive separate cryptographic keys for different purposes. For example:
- One key for message authentication.
- Another key for encryption.
- Another key for a separate communication channel.
HKDF helps achieve this. It uses HMAC to transform input keying material into cryptographically suitable key material.
HKDF has two main stages:
- Extract: Converts input keying material into a pseudorandom key.
- Expand: Uses that pseudorandom key and contextual information to generate one or more output keys.
Shared Secret
|
v
HKDF-Extract
|
v
Pseudorandom Key
|
v
HKDF-Expand
|
+------+------+
| | |
v v v
Key 1 Key 2 Key 3 Shared Secret
|
v
HKDF-Extract
|
v
Pseudorandom Key
|
v
HKDF-Expand
|
+------+------+
| | |
v v v
Key 1 Key 2 Key 3This is particularly useful in secure communication protocols. For example, TLS 1.3 uses HKDF as part of its key-schedule design. The important lesson is that a shared secret should be processed appropriately before being used as cryptographic keys.
4. Domain Separation: Preventing Confusion Between Cryptographic Uses
Here is another concept that deserves attention.
Imagine an application uses the same hash function for different purposes:
- Generating authentication tokens.
- Creating file identifiers.
- Calculating transaction commitments.
Using the same hash function is not automatically a problem.
However, reusing the same input structure across different cryptographic purposes can create ambiguity and unexpected interactions.
This is where domain separation becomes useful. Domain separation means explicitly distinguishing different cryptographic uses through unique prefixes, labels, or context information.
For example:
Hash("FILE-ID" || data)
Hash("AUTH-TOKEN" || data)
Hash("COMMITMENT" || data)Hash("FILE-ID" || data)
Hash("AUTH-TOKEN" || data)
Hash("COMMITMENT" || data)Even if the underlying data is identical, the inputs are different because their application contexts are different. This helps prevent one type of cryptographic output from being mistakenly interpreted as another.
Domain separation is particularly important in protocol design, key derivation, signature systems, and cryptographic commitments. It should be implemented according to the construction's specification rather than by casually adding strings to arbitrary hash inputs.
5. Hash Commitments: Proving Something Without Revealing It
Another fascinating application is a cryptographic commitment scheme.
Imagine you want to prove that you selected a particular value before a game or negotiation, but you don't want to reveal it immediately.
A commitment scheme allows you to commit to a value first and reveal it later. A simple hash-based commitment can be constructed as follows:
- Select a secret value.
- Generate a random nonce.
- Hash both together using an unambiguous encoding.
- Publish the resulting commitment.
- Reveal the value and nonce later.
- Allow the other party to verify the commitment.
Conceptually:
Secret Value + Random Nonce
|
v
Hash Function
|
v
Commitment
|
v
Published First
later:
Reveal Value + Nonce
|
v
Recalculate Hash
|
v
Verify CommitmentSecret Value + Random Nonce
|
v
Hash Function
|
v
Commitment
|
v
Published First
later:
Reveal Value + Nonce
|
v
Recalculate Hash
|
v
Verify CommitmentFor example, a simplified Python demonstration:
import hashlib
import secrets
secret = b"Option A"
nonce = secrets.token_bytes(32)
commitment = hashlib.sha256(
nonce + secret
).hexdigest()
print("Commitment:", commitment)
# Reveal stage
verification = hashlib.sha256(
nonce + secret
).hexdigest()
print("Valid:", commitment == verification)
Commitment: 5f535910b1f9665814ba1f06c8603e6eee95a0bd04cce3f2453eca1fe00ea34c
Valid: Trueimport hashlib
import secrets
secret = b"Option A"
nonce = secrets.token_bytes(32)
commitment = hashlib.sha256(
nonce + secret
).hexdigest()
print("Commitment:", commitment)
# Reveal stage
verification = hashlib.sha256(
nonce + secret
).hexdigest()
print("Valid:", commitment == verification)
Commitment: 5f535910b1f9665814ba1f06c8603e6eee95a0bd04cce3f2453eca1fe00ea34c
Valid: TrueThis is only an educational demonstration of the basic idea. A production commitment scheme needs an appropriate encoding and carefully selected security properties.
The random nonce is important because committing to predictable, low-entropy values without randomness may allow someone to guess the original value by testing possibilities.
Commitment schemes are useful in cryptographic protocols, auctions, voting systems, and other applications where information must be committed to before being revealed.
6. Common Mistakes in Hash-Based Protocol Design
Understanding cryptographic constructions is important, but knowing what not to do is equally valuable. Here are several mistakes developers should avoid.
1. Using plain hashing for authentication: SHA-256 alone doesn't prove who generated a message. Use an appropriate authentication mechanism such as HMAC or a digital signature.
2. Using MD5 or SHA-1 in new security-sensitive systems: Both have serious collision-resistance weaknesses. Choose modern, approved cryptographic algorithms.
3. Using ordinary hashes for password storage: SHA-256 is designed to be fast. Password storage should use dedicated algorithms such as Argon2id, scrypt, or bcrypt, with appropriate salts and parameters.
4. Reusing cryptographic keys across unrelated purposes: Separate keys should be derived for different uses through a suitable key-derivation mechanism.
5. Ignoring replay attacks: A valid authenticated message might be captured and sent again by an attacker. Protocols need mechanisms such as nonces, sequence numbers, or timestamps, with appropriate validation and state management.
6. Designing your own cryptographic construction: Combining algorithms without understanding their security properties can introduce vulnerabilities. Use established, reviewed standards and libraries.
8. What Makes a Secure Hash-Based Protocol?
A strong protocol requires more than a strong hash function. It needs a carefully designed combination of cryptographic components. Some important principles include:
- Use modern, well-studied cryptographic primitives.
- Authenticate messages, not just their hashes.
- Use secure key generation and key derivation.
- Separate keys and cryptographic purposes.
- Include replay protection where necessary.
- Define message formats and encodings unambiguously.
- Validate inputs and handle errors safely.
For example, TLS 1.3 combines several cryptographic mechanisms rather than relying on a single hash function. It uses key exchange, authenticated encryption, digital signatures, and HKDF as parts of its security design.
This demonstrates an important principle:
Real-world cryptographic security comes from how individual primitives work together, not simply from selecting a strong algorithm.
What's Next?
Throughout this series, we have explored the fundamentals of hash functions, their historical evolution, the weaknesses of earlier algorithms, and how modern cryptographic techniques use hashing to build secure protocols.
One important application we have not explored in detail is password security. Secure password storage is a critical responsibility for developers, as almost every online application relies on user authentication.
However, since I have already published a dedicated article covering this topic, I will avoid repeating the same material here. Instead, I invite you to check out that article for a deeper look at password hashing and secure password storage.
In Part 7, we will turn our attention to Merkle Trees. We will explore what they are, how they work, why they are important, and the internal operations that make them efficient for verifying large amounts of data.
Final Thoughts
Hash functions are powerful cryptographic building blocks, but they are not complete security solutions.
HMAC provides message authentication. HKDF helps derive cryptographic keys. Domain separation distinguishes different cryptographic purposes. Commitment schemes allow values to be committed to before disclosure. Merkle Trees enable efficient verification of large datasets.
Each construction solves a different problem.
This is Part 5 of my Hashing From First Principles series, where I explore hashing, cryptographic algorithms, security protocols, vulnerabilities, and their real-world applications as a cybersecurity undergraduate.
If you are interested in learning cryptography beyond the basics, follow along as I continue this journey. And I invite all to check all references also, for further.
Next: Merkle Trees: Understanding the Structure Behind Secure Data Verification
References and Further Reading
- RFC 2104 β HMAC: Keyed-Hashing for Message Authentication
- RFC 5869 β HMAC-based Extract-and-Expand Key Derivation Function (HKDF)
- RFC 8446 β The Transport Layer Security (TLS) Protocol Version 1.3
- NIST FIPS 202 β SHA-3 Standard
- NIST SP 800β56C β Key-Derivation Methods
- OWASP Cryptographic Storage Cheat Sheet
- Python Documentation β hmac