Lesson 2 of 5 Cryptography Fundamentals

Hashing, Salting and HMAC

Hash functions are the workhorses of cryptography, used for integrity, signatures and password storage. This lesson explains hash properties and algorithms, salting and slow password hashing, and the HMAC construction.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe the properties of cryptographic hash functions.
  • Identify current and deprecated hash algorithms.
  • Explain salting, peppers and slow password hashing.
  • Distinguish hashes, HMACs and digital signatures.

Hash functions

A cryptographic hash function takes input of any length and produces a fixed-length output called a digest or hash. A secure hash function has four properties. It is deterministic: the same input always gives the same digest. It is one-way (preimage-resistant): given a digest, it is infeasible to find an input that produces it. It is collision-resistant: it is infeasible to find two different inputs with the same digest. And it shows the avalanche effect: changing a single bit of input changes about half the bits of the output.

Hashing is not encryption: there is no key and no way to reverse it. Hashes are used to verify file integrity (comparing a download's digest with the publisher's), to identify files such as known malware, as the basis of digital signatures and HMACs, in forensic evidence handling, and to store passwords.

True or false

Hash functions

Exam skills 5.2.1

Check the properties of cryptographic hash functions.

Log in to try this activity Interactive activities are free with an account.

Hash algorithms

The following table summarises common hash algorithms and their status.

AlgorithmDigest lengthStatus
MD5128 bitsBroken: practical collisions; use only for non-security checks
SHA-1160 bitsBroken for collisions (demonstrated in 2017); deprecated for signatures
SHA-256, SHA-384, SHA-512 (SHA-2)256, 384, 512 bitsCurrent standard
SHA-3224 to 512 bitsCurrent; a different internal design from SHA-2

The following example shows the avalanche effect: two inputs differing only in a final full stop have unrelated SHA-256 digests.

$ printf 'Transfer 100 to account 42' | sha256sum
$ printf 'Transfer 100 to account 42.' | sha256sum

The lab for this module runs these commands and shows the two completely different digests.

Sort into groups

Hash algorithm status

Exam skills 5.2.1 · 5.2.5

Decide whether each hash algorithm is suitable for new security uses.

Log in to try this activity Interactive activities are free with an account.

Password storage and salting

Systems should never store passwords in plaintext or with reversible encryption. They store a hash, and at login they hash the entered password and compare. A plain hash is not enough, however. Identical passwords produce identical hashes, revealing which users share a password, and attackers use rainbow tables, huge precomputed tables of hashes of likely passwords, to look up unsalted hashes instantly.

A salt is a unique random value generated for each password, combined with it before hashing and stored with the hash. Salts make every hash unique even for identical passwords and make precomputed tables useless, so attackers must attack each hash separately. Salts do not need to be secret. A pepper is an additional secret value, the same for all passwords, stored separately from the database (for example in a hardware security module), so that a stolen database alone is not enough.

Because fast hashes such as SHA-256 let attackers test billions of guesses per second on graphics hardware, passwords are hashed with deliberately slow, tunable key derivation functions: bcrypt, scrypt, Argon2 (the winner of the Password Hashing Competition) or PBKDF2. Their cost (iterations, memory) is set so that a legitimate login takes a fraction of a second while mass guessing becomes very expensive.

The following diagram compares a hash published beside a download with a digital signature.

Why a hash on the same page as the download does not prove authenticity. It detects accidental corruption, but an attacker who controls the website replaces both the installer and the digest. A digital signature made with the vendor's private key, verified with the vendor's certificate or a key obtained another way, cannot be forged.
The scenario is illustrative.
Select all that apply

Storing passwords

Exam skills 5.2.2

Identify good practices for storing passwords.

Log in to try this activity Interactive activities are free with an account.

HMAC

A plain hash proves integrity only if the digest itself is protected: an attacker who changes a message can simply recompute its hash. A message authentication code (MAC) solves this by involving a secret key. HMAC (hash-based message authentication code) combines a cryptographic hash, such as SHA-256, with a shared secret key in a specific construction. Only someone with the key can produce or verify the correct HMAC, so a valid HMAC shows that the message was not altered and came from a holder of the key.

HMAC is widely used: in TLS and IPsec to protect each record or packet, in API request signing, in JSON Web Tokens signed with HS256, and in one-time password algorithms. Its limitation is that both parties hold the same key, so either could have created the code; HMAC therefore provides integrity and authenticity but not non-repudiation. The following comparison summarises the three integrity mechanisms.

Hash
KeyNone
Proves integrityOnly if the digest is protected
Proves originNo
Non-repudiationNo
HMAC
KeyShared secret key
Proves integrityYes
Proves originA key holder created it
Non-repudiationNo
Digital signature
KeyPrivate key to sign, public key to verify
Proves integrityYes
Proves originThe private key holder created it
Non-repudiationYes
Worked example

A stolen password database

Attackers steal a user table in which passwords are stored as unsalted SHA-1 hashes. Within hours, most passwords are published. Why, and what storage method would have resisted this?

Unsalted hashes can be looked up in rainbow tables and cracked in bulk, and SHA-1 is fast enough to test billions of guesses per second. Storing passwords with a unique salt and a slow algorithm such as Argon2 or bcrypt with an appropriate cost would have forced attackers to attack each password individually at great expense. Users should also be required to change their passwords and use MFA.

Worked example

Verifying a download

A vendor's website publishes the SHA-256 hash of its installer on the same page as the download link. An attacker who compromised the website replaces both. Does comparing the hash protect the user?

No. The hash detects accidental corruption, but an attacker who controls both the file and the published digest can replace both. Authenticity requires a digital signature made with the vendor's private key, which the attacker does not have, verified with the vendor's certificate or a public key obtained through a separate trusted channel.

Check yourself

Does a salt need to be kept secret?

Show answer

No. It is stored with the hash; its purpose is to make each hash unique so precomputed tables fail. A secret value stored separately is a pepper.

Exam Tips

Remember for the exam

  • Hashes are one-way; MD5 and SHA-1 are broken for collisions.
  • Salts defeat rainbow tables; use slow algorithms such as Argon2, bcrypt, scrypt or PBKDF2 for passwords.
  • HMAC gives integrity and authenticity between key holders, but not non-repudiation.