What Is a Hash Function? A Clear, Practical Explainer

A hash function takes an input of any size and returns a fixed-size value called a hash, digest, or fingerprint. The same input always produces the same output, but the output reveals almost nothing about the input it came from.

The core idea

Think of a hash function as a deterministic machine: feed it the text hello and it returns one specific value; feed it a 4 GB video file and it returns a value of the exact same length. That fixed length is a defining trait. The popular SHA-256 algorithm, for example, always emits 256 bits (written as 64 hexadecimal characters) no matter whether the input was a single byte or a gigabyte.

Because the function is deterministic, hashing the same data twice on two different machines yields identical results. That property is what makes hashing useful for comparison: instead of checking whether two large files are equal byte by byte, you compare their short hashes.

SHA-256("hello")  = 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
SHA-256("hellp")  = fdd7585e08c4e2afd71dcabdb4636c89d557a3f42db9e2040c8bbd1708aa4ce7

Notice how changing a single letter completely transforms the output. This is the avalanche effect: a tiny change in the input flips roughly half the output bits in an unpredictable way.

The properties that matter

Not all hash functions are built for the same job, but cryptographic hash functions are expected to satisfy several properties.

  • Deterministic — identical input always maps to identical output.
  • Fixed output size — the digest length is constant regardless of input size.
  • Fast to compute — a hash of ordinary data should be quick (though, as you will see, "slow on purpose" is a feature for passwords).
  • Preimage resistance — given a hash, it is computationally infeasible to find an input that produces it. The function only runs forward.
  • Collision resistance — it should be infeasible to find two different inputs that produce the same hash.

That last point deserves attention. Because inputs are unlimited in size but outputs are fixed, collisions (two inputs sharing one hash) must exist mathematically. A good cryptographic hash makes finding them impractical. When researchers learn to produce collisions cheaply, the algorithm is considered broken for security use, which is what happened to MD5 and SHA-1.

Hashing is not encryption

This is the single most common misconception. Encryption is reversible: with the right key, ciphertext can be turned back into the original plaintext. Hashing is a one-way operation by design. There is no key and no "unhash" step, because the original data is not stored inside the digest. A hash is a fingerprint, not a locked box.

It also is not the same as encoding. Base64, for instance, is a reversible representation, not a fingerprint. If you can decode it, it is not a hash. For a deeper look at that distinction, see Base64 encoding explained.

Where hash functions are used

Hashing shows up across software in ways that are easy to miss.

File and data integrity

Download pages often publish a SHA-256 value next to a file. After downloading, you hash your copy and compare; a match means the bytes arrived intact and untampered. You can do this in the browser with a file checksum verifier, or generate digests of text and snippets with a hash generator.

Verifying messages with a key

A plain hash proves data has not changed, but anyone can recompute it. To also prove who produced it, a secret key is mixed in, producing a Hash-based Message Authentication Code (HMAC). HMAC underpins API request signing and JWT signatures; you can experiment with it using an HMAC generator.

Data structures and deduplication

Hash tables (the engine behind dictionaries, maps, and sets in most languages) use a fast non-cryptographic hash to decide where to store each key, giving near-constant-time lookups on average. Content-addressed systems such as Git identify every commit and file by its hash, so identical content is stored only once.

Choosing the right algorithm

"Which hash should I use?" depends entirely on the goal. Using the wrong category is a frequent source of bugs and security holes.

GoalReasonable choiceAvoid
General integrity / fingerprintsSHA-256, SHA-3, BLAKE2MD5, SHA-1 (broken)
Message authenticationHMAC-SHA-256Raw hash with appended key
Password storagebcrypt, scrypt, Argon2, PBKDF2Any plain fast hash
Hash table keysBuilt-in language hashSHA-256 (needlessly slow)

MD5 and SHA-1 still appear in legacy systems and as non-security checksums, but they should never guard anything that matters, because practical collision attacks exist against both. If you encounter an unknown digest and want to guess its algorithm by length and format, a hash identifier can help narrow it down.

Common pitfalls

Several mistakes recur often enough to be worth calling out.

  1. Hashing passwords with a fast algorithm. SHA-256 is fast, which is exactly why it is wrong for passwords: attackers can try enormous numbers of guesses per second on commodity hardware. Passwords need slow, salted algorithms such as bcrypt or Argon2 that are deliberately expensive to compute. See password security best practices for the full reasoning.
  2. Forgetting the salt. A salt is random data added to each password before hashing, so identical passwords produce different digests and precomputed "rainbow table" attacks fail.
  3. Treating a hash as a secret. A digest does not protect the original value if that value is short or predictable. Hashing a phone number or a small set of known inputs can be reversed by simply hashing every possibility.
  4. Comparing hashes with a non-constant-time check. In security-sensitive code, comparing digests byte by byte can leak timing information; use a constant-time comparison instead.

A quick mental model

If you remember one thing: a hash function is a one-way fingerprint that is identical for identical input and wildly different for anything else. It proves sameness and detects change, but it does not hide data the way encryption does, and the "best" hash is whichever one fits the specific task. When in doubt, reach for SHA-256 for integrity, HMAC for authentication, and a purpose-built slow hash for passwords.

Frequently Asked Questions

No. Encryption is reversible with a key, so ciphertext can be turned back into the original data. A hash function is one-way by design: it produces a fixed-size fingerprint and offers no way to recover the original input.

Yes, these are called collisions, and they must exist because inputs are unlimited in size while outputs are fixed. A strong cryptographic hash function makes finding a collision computationally infeasible, which is why broken algorithms like MD5 and SHA-1 are unsafe.

Use a slow, salted algorithm built for passwords such as bcrypt, scrypt, Argon2, or PBKDF2. Fast general-purpose hashes like SHA-256 are unsuitable because attackers can test enormous numbers of guesses per second. See /blog/password-security-best-practices.

SHA-256 is part of the SHA-2 family, is well-studied, has no known practical collision attacks, and produces a consistent 256-bit digest. That makes it a safe default for file integrity checks, digital signatures, and general fingerprinting.

A plain hash verifies that data has not changed, but anyone can recompute it. An HMAC mixes in a secret key, so it also proves the message came from someone who holds that key, which is why it is used for API signing and token verification.