Hashing algorithms: MD5, SHA-1, SHA-256, HMAC, digital signatures

CompTIA Security+ Preparation · lesson 8 of 45 · 25 min

Hashing Fundamentals

A hash function takes input of any size and produces a fixed-size output (digest). Properties of a cryptographic hash function:

One-way (pre-image resistance) — cannot reverse the hash to get the original input.

Deterministic — same input always produces the same hash.

Avalanche effect — a tiny change in input produces a completely different hash.

Collision resistance — computationally infeasible to find two different inputs that produce the same hash.

Hashing is used for: integrity verification, password storage, digital signatures, file fingerprinting.

HMAC and Digital Signatures

HMAC (Hash-based Message Authentication Code) — combines a hash function with a secret key. Provides both integrity and authentication. The recipient must have the same secret key to verify. Used in: API authentication, TLS, JWT tokens.

HMAC formula: HMAC(key, message) = H((key XOR opad) || H((key XOR ipad) || message))

Digital signature — uses asymmetric cryptography to provide integrity, authentication, and non-repudiation.

Signing process: 1. Hash the message. 2. Encrypt the hash with the sender’s private key. The result is the digital signature.

Verification: 1. Decrypt the signature with the sender’s public key to get the original hash. 2. Hash the received message. 3. Compare — if they match, the message is authentic and unmodified.

Non-repudiation — the sender cannot deny signing the message because only they have the private key.

Password Hashing

Passwords should never be stored in plaintext. They should be hashed with a slow, salted algorithm designed for passwords.

Salt — a random value added to the password before hashing. Prevents rainbow table attacks (precomputed hash tables). Each password gets a unique salt.

bcrypt — designed for password hashing. Includes salt automatically. Has a work factor (cost) that can be increased as hardware gets faster.

scrypt — memory-hard password hashing. Resistant to GPU and ASIC attacks.

Argon2 — winner of the Password Hashing Competition. Current best practice for password storage.

Why not MD5/SHA-256 for passwords? They are too fast — attackers can compute billions of hashes per second. Password hashing algorithms are intentionally slow.

Exam Tip

Know the difference: HMAC provides integrity + authentication (requires shared secret key). Digital signatures provide integrity + authentication + non-repudiation (uses asymmetric key pair). Hashing alone provides integrity only.

Key Terms

Key Terms

Knowledge Check — 12 Questions

Security+ — Hashing & Digital Signatures

Which property means a tiny change in input produces a completely different hash?

  • Collision resistance
  • Pre-image resistance
  • Avalanche effect
  • Determinism

Answer: Avalanche effect
The avalanche effect ensures that even a single bit change in input produces a completely different hash output.

MD5 should NOT be used for security purposes because:

  • It is too slow
  • It produces collision attacks — two different inputs can produce the same hash
  • It requires a key
  • It is too new
  • Answer: It produces collision attacks — two different inputs can produce the same hash

MD5 has known collision vulnerabilities — attackers can craft two different files with the same MD5 hash. Use SHA-256 instead. HMAC provides:

  • Integrity only
  • Integrity and authentication
  • Integrity, authentication, and non-repudiation
  • Confidentiality only
  • Answer: Integrity and authentication

HMAC uses a shared secret key — both parties can verify the message was not tampered with AND came from someone with the key. Digital signatures provide non-repudiation because:

  • They use a shared secret key
  • Only the signer has the private key used to create the signature
  • They encrypt the entire message
  • They use a hash only
  • Answer: Only the signer has the private key used to create the signature

Non-repudiation: since only the signer has their private key, they cannot deny creating the signature. A salt in password hashing prevents:

  • Brute force attacks
  • Rainbow table attacks (precomputed hash lookups)
  • SQL injection
  • Phishing
  • Answer: Rainbow table attacks (precomputed hash lookups)

Salts make each password hash unique — precomputed rainbow tables are useless because they do not account for the random salt.

Why should bcrypt be used instead of SHA-256 for password storage?

  • bcrypt is more secure against collision attacks
  • bcrypt is intentionally slow, making brute force attacks much harder
  • SHA-256 requires a salt
  • bcrypt produces longer hashes

Answer: bcrypt is intentionally slow, making brute force attacks much harder
SHA-256 is too fast — attackers can compute billions per second. bcrypt’s work factor makes each hash computation slow.

In digital signature verification, the recipient uses the sender’s:

  • Private key to decrypt the signature
  • Public key to decrypt the signature
  • Shared secret key
  • Own private key
  • Answer: Public key to decrypt the signature

The signature is encrypted with the sender’s private key — anyone with the sender’s public key can verify (decrypt) it.

The current best practice password hashing algorithm (winner of the Password Hashing Competition) is _____.

Answer: Argon2
Argon2 won the Password Hashing Competition and is the current recommended algorithm for password storage.

SHA-256 produces a hash of:

  • 128 bits
  • 160 bits
  • 256 bits
  • 512 bits
  • Answer: 256 bits

SHA-256 (part of the SHA-2 family) produces a 256-bit (64 hexadecimal character) hash output.

Which provides integrity, authentication, AND non-repudiation?

  • HMAC
  • Hashing alone
  • Digital signature
  • Symmetric encryption

Answer: Digital signature
Digital signatures provide all three: integrity (hash), authentication (only the key owner could sign), non-repudiation (cannot deny signing).

The signing process for a digital signature is:

  • Encrypt the message with the private key
  • Hash the message, then encrypt the hash with the private key
  • Hash the message with a shared key
  • Encrypt the message with the public key
  • Answer: Hash the message, then encrypt the hash with the private key

Signing: hash the message → encrypt the hash with the sender’s private key. The encrypted hash is the digital signature. Argon2 is described as “memory-hard” because:

  • It requires a lot of CPU time
  • It requires significant memory, making GPU/ASIC attacks expensive
  • It stores passwords in memory
  • It uses memory encryption
  • Answer: It requires significant memory, making GPU/ASIC attacks expensive

Memory-hard algorithms require large amounts of RAM per computation — GPUs and ASICs have limited memory bandwidth, making attacks expensive.


Previous lesson: Encryption algorithms: AES, RSA, ECC, DH, DSA — strengths and use cases
Back to CompTIA Security+ Preparation
Next lesson: Authentication factors: something you know/have/are; MFA, passwordless