0Pricing
Cloud & IT Cert Prep · Lesson

Hashing and Data Integrity

Learn how SHA-256, MD5, and HMAC produce fixed-length digests that verify data has not been altered in transit or storage.

Hashing and Data Integrity is a free Cloud & IT Cert Prep lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Cloud & IT Cert Prep learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is a Hash Function?

A cryptographic hash function is a mathematical algorithm that takes an input of any size and produces a fixed-length output called a hash, digest, or fingerprint. Hash functions are one-directional: given a hash, it should be computationally infeasible to determine the original input. They are deterministic: the same input always produces the same hash. Hash functions are the foundation of data integrity verification, digital signatures, password storage, and many other security mechanisms.

Properties of Secure Hash Functions

A secure cryptographic hash function must have four key properties. Pre-image resistance: given a hash output, it's computationally infeasible to find any input that produces it. Second pre-image resistance: given an input, it's infeasible to find a different input with the same hash. Collision resistance: it's infeasible to find any two different inputs that produce the same hash output. Avalanche effect: changing even a single bit of input causes a completely different hash output, making tampering detectable.

# Avalanche effect demonstration
echo -n 'Hello' | sha256sum
# 185f8db32...  (completely different when one char changes)
echo -n 'Hello!' | sha256sum
# 334d016f7...  (entirely different hash)

# Same input ALWAYS produces same hash (deterministic)
echo -n 'Security+' | sha256sum
echo -n 'Security+' | sha256sum
# Both outputs are identical

MD5: Broken and Deprecated

MD5 (Message Digest 5) produces a 128-bit hash and was once widely used. However, researchers demonstrated collision attacks against MD5 in 2004 — meaning two different inputs that produce the same hash can be found. This is catastrophic for integrity verification: an attacker could substitute a malicious file that has the same MD5 hash as the original. MD5 is completely broken for cryptographic security and must not be used for integrity verification, digital signatures, or password hashing. It is still sometimes seen for non-security checksums (legacy file verification), which is acceptable for detecting accidental corruption only.

SHA Family: SHA-1, SHA-256, SHA-3

The Secure Hash Algorithm (SHA) family is the most important set of hash functions for Security+. SHA-1 (160-bit) was the standard for years but was broken by a practical collision attack in 2017 (Google's SHAttered project) — now deprecated. SHA-256 (part of SHA-2 family, 256-bit output) is the current industry standard for digital signatures, TLS certificates, and file integrity verification. SHA-3 (Keccak) is NIST's alternative algorithm offering resistance to attacks that target SHA-2's structure, though SHA-256 remains the dominant choice for most applications.

# Common hash computations
echo -n 'password123' | sha256sum
# a665a4592...64 hex chars = 256 bits

echo -n 'password123' | sha512sum  
# b109f3bb...128 hex chars = 512 bits (SHA-512)

# File integrity check
sha256sum /path/to/downloaded_file.iso > file.sha256
sha256sum -c file.sha256
# file.iso: OK

Hash Functions for Password Storage

Storing passwords as plain text is never acceptable. When a user creates a password, a hash is stored in the database instead of the password itself. When the user logs in, their entered password is hashed and compared against the stored hash. However, standard SHA-256 is too fast for password storage — attackers use GPUs to test billions of hashes per second against stolen password databases. Purpose-built password hashing functions like bcrypt, PBKDF2, and Argon2 add computational cost (work factors) and memory requirements that make bulk cracking impractical.

# bcrypt hash example (Python-style pseudocode)
import bcrypt
password = b'mySecretPassword'
# Hash with work factor of 12 (2^12 = 4096 iterations)
hashed = bcrypt.hashpw(password, bcrypt.gensalt(rounds=12))
# Stored in DB: $2b$12$H0qS/LkPkAiH8KlxJb3lFuK...

# Verify (never compare raw hashes for bcrypt)
bcrypt.checkpw(password, hashed)  # Returns True

Salting: Defeating Rainbow Tables

A rainbow table is a precomputed lookup table mapping common passwords to their hashes — allowing an attacker to instantly look up a hash and find the original password. To defeat rainbow tables, password hashes must be salted. A salt is a random value added to the password before hashing, making each user's hash unique even if two users have the same password. The salt is stored alongside the hash (it doesn't need to be secret). Even if an attacker has your entire password database, precomputed rainbow tables are useless because each hash used a different salt.

# Salt demonstration
# Without salt: same password = same hash (rainbow table attack works)
# SHA256('password123') = always the same hash

# With salt: same password + random salt = unique hash
# SHA256('password123' + 'aBc8xZ') = unique hash for this user
# SHA256('password123' + 'mK2pYq') = completely different hash

# The salt is stored in the database alongside the hashed value
# Format: $salt$hash or combined as in bcrypt output

HMAC: Adding Authentication to Hashing

HMAC (Hash-based Message Authentication Code) is a construction that uses a hash function with a secret key to produce an authentication code. Unlike a plain hash, an HMAC can only be computed and verified by parties that know the secret key. This provides both integrity (detecting changes) and authentication (proving the message came from someone with the key). HMAC-SHA256 is widely used in API authentication (signing API requests), JWT tokens, and TLS MAC operations. It prevents an attacker who intercepts a message from replacing it with a crafted alternative.

# Compute HMAC-SHA256 with OpenSSL
openssl dgst -sha256 -hmac 'my-secret-api-key' -hex api_request.json
# Output: HMAC-SHA256(api_request.json)= 3d4e2f...

# Only someone with 'my-secret-api-key' can generate or verify this HMAC
# Used in AWS Signature V4 for API authentication
# Used in JWT HS256 algorithm for token integrity

Hash Functions in Digital Signatures

Hash functions play a central role in digital signatures. When signing a large document, you don't encrypt the entire document with the private key (which would be extremely slow). Instead, you: (1) compute the hash of the document (fast, produces a small fixed-length digest), then (2) sign the small hash with the private key. The signature is just an encrypted hash. Verification follows the same pattern: decrypt the signature to get the hash, independently hash the document, and compare. SHA-256 is mandatory in all modern PKI certificates; SHA-1 is forbidden in new certificates.

File Integrity Monitoring (FIM)

File Integrity Monitoring (FIM) uses hash functions to detect unauthorized changes to critical system files. At baseline, the FIM tool hashes all monitored files and stores the hashes securely. Periodically (or continuously), it rehashes the files and compares against the baseline. Any mismatch indicates a file was modified — potentially by malware or an unauthorized administrator action. FIM tools like Tripwire, AIDE (Linux), and Windows' own integrity checking use SHA-256 hashes. FIM is required by compliance frameworks like PCI-DSS for protecting cardholder data environments.

# AIDE (Advanced Intrusion Detection Environment) - Linux FIM
# Initialize baseline database
aide --init
# mv /var/lib/aide/aide.db.new.gz /var/lib/aide/aide.db.gz

# Check for changes (run via cron daily)
aide --check
# Output shows any files that were added, changed, or deleted
# File: /etc/passwd changed
# MD5 : old_hash != new_hash

Checksums vs Cryptographic Hashes

It's important to distinguish checksums from cryptographic hashes. Checksums like CRC32 are designed to detect accidental data corruption during transmission — they are fast and simple but can be easily forged by an attacker. Cryptographic hashes (SHA-256) are designed to be collision-resistant and tamper-evident even against adversarial manipulation. Use checksums when you only need to verify that a download wasn't corrupted in transit; use cryptographic hashes when you need to verify a file has not been intentionally tampered with by an adversary.

Hash Collision Attacks in Practice

Hash collision attacks have real-world impact. In 2017, researchers created two different PDF files with the same SHA-1 hash — demonstrating that SHA-1 certificates could be forged. In 2008, researchers used MD5 collisions to create a fake CA certificate that appeared legitimate. These attacks are why browsers reject MD5 and SHA-1 certificates and why certificate authorities must use SHA-256 minimum. Understanding collision attacks helps you explain why algorithm deprecation decisions are made and why 'but it still produces a hash' is not a sufficient argument for using broken algorithms.

Quick Check

Test your understanding of CompTIA Security+ (SY0-701) concepts from this lesson.

Lesson Recap

In this lesson you learned: hash functions produce fixed-length digests for integrity verification; MD5 and SHA-1 are broken and deprecated; SHA-256 is the current standard; salting defeats rainbow table attacks; and HMAC adds authentication to hashing using a shared secret key. Next up we explore Key Exchange and Hybrid Encryption.

Frequently asked questions

Is the “Hashing and Data Integrity” lesson free?

Yes — the full text of “Hashing and Data Integrity” is free to read here on the web, and the Cloud & IT Cert Prep course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Cloud & IT Cert Prep course, upgrade to CoddyKit PRO.

What will I learn in “Hashing and Data Integrity”?

Learn how SHA-256, MD5, and HMAC produce fixed-length digests that verify data has not been altered in transit or storage. You practise Cloud & IT Cert Prep with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Cloud & IT Cert Prep?

No prior experience is required. Cloud & IT Cert Prep on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Hashing and Data Integrity” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Cloud & IT Cert Prep lesson?

Yes. Every Cloud & IT Cert Prep lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Symmetric Encryption Algorithms
  2. Asymmetric Encryption and Key Pairs
  3. Hashing and Data Integrity
  4. Key Exchange and Hybrid Encryption
← Back to Cloud & IT Cert Prep