A cryptographic hash function is a deterministic algorithm that maps variable-length binary data to a fixed-length bit string, called a hash value or digest. The chosen function determines the output length; inputs can be files, messages or other bytes, not just readable text. It is intended to be one-way, rather than reversible.
How It Works
A cryptographic hash function mixes input data through defined operations to produce its digest. Identical input bytes processed with the same function always produce the same output. Small changes ordinarily produce substantially different digests, so even a formatting or encoding change can affect the result.
Hashing is not encryption: encryption is designed to be reversible with the appropriate key, whereas hashing is not. Ordinary non-cryptographic hashes also map data to hash values, but they are not designed to resist deliberate attacks. A cryptographic digest alone neither conceals the underlying data nor authenticates its sender.
Security Properties
The security of a cryptographic hash function depends on three distinct computational goals. Preimage resistance means that, given a digest, finding any input that produces it should be computationally difficult. It does not stop an attacker from testing a short list of likely inputs. Second-preimage resistance means that, given a particular input, finding a different input with the same digest should be difficult; this addresses targeted substitution. Collision resistance means that finding any pair of different inputs with equal digests should be difficult. Unlike a second-preimage attack, the attacker can choose both inputs. In some signature workflows, a collision can let a signature on one message validate another, enabling forgery.
These are security goals, not mathematical guarantees. Collisions must exist because variable-length inputs have more possible values than the finite output space. A digest is therefore not a guaranteed unique identifier, and matching digests are not logical proof that the inputs are identical.
Advertisement — FinchTrade.
FinchTrade services
Get startedIntegrity Checks and Digital Signatures
To check a file or message, recompute its digest and compare it with a trusted reference using the same algorithm. A mismatch reveals a difference; a match is useful evidence only when the reference is trustworthy and the algorithm is suitable. An attacker who can replace both the data and its reference hash can defeat this check.
Many digital-signature schemes use message digests as part of signing and verification. The signature, verified with a trusted public key, supplies authentication; an unkeyed digest alone does not.
Password Verification
Password verification stores a password-derived verifier rather than the plaintext password. A submitted password is processed with the same scheme and stored parameters, and the result is checked against the verifier. A fast, general-purpose cryptographic hash function alone is unsuitable because attackers can rapidly test likely passwords.
Use a purpose-built, deliberately costly password-hashing scheme with a unique salt for each stored password. The salt is stored with the verifier and counters reusable precomputed attacks, including rainbow tables. It does not eliminate password guessing; the scheme's cost makes each guess more expensive.
Algorithm Choice
Choosing a cryptographic hash function depends on the application's required security properties, performance needs and surrounding protocol. SHA-256, for example, produces a 256-bit digest. SHA-1's demonstrated collision weaknesses make it unsuitable where collision resistance is required. Cryptanalytic advances can undermine previously accepted algorithms, so output length alone is not enough to judge security.