Hashing

What Is a Hash Function? A Plain-English Guide with Examples

Understand cryptographic hash functions with verified SHA-256 examples, collision and preimage resistance, and the difference between hashes and authentication.

hashingsecuritysha256md5cryptographydevops

What Is a Hash Function? A Plain-English Guide with Examples

A cryptographic hash function maps input bytes to a fixed-length digest. The same bytes give the same digest, while finding an input for a chosen digest should be computationally hard. A general-purpose hash function used in a hash table has different security requirements. Neither kind encrypts data or authenticates its source by itself.

That single idea powers an enormous amount of modern computing: password storage, file integrity checks, digital signatures, blockchains, and the data structures behind hash tables. This guide explains what a hash function is, how it works, what makes a good one, and where you'll run into hashing as a developer.

How a Hash Function Works

A hash function takes an input of any length and returns an output of fixed length. Feed it one character or a 4 GB video file — the SHA-256 hash is always 64 hexadecimal characters (256 bits).

hash("hello")        = 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
hash("hello world")  = b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
hash("Hello")        = 185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969

Notice two things:

  1. Determinism — SHA-256 of the same UTF-8 bytes, with no extra newline, always returns the same value.
  2. The avalanche effect — changing a single character (hello → Hello) produces a completely different hash. There's no partial similarity to exploit.

Internally, the function mixes the input bytes through rounds of bitwise operations, modular addition, and compression until the output is thoroughly scrambled and uniformly distributed.

The 5 Properties of a Cryptographic Hash Function

Not every hash function is suitable for security. A cryptographic hash function must satisfy five properties:

Property What it means
Deterministic The same input always yields the same output.
Fast to compute Computing the hash of any input is quick.
Pre-image resistant Given a hash, you can't feasibly find the original input.
Collision resistant You can't find two different inputs with the same hash.
Avalanche effect A tiny input change drastically changes the output.

When any of these break — as has happened with MD5 and SHA-1, where researchers found practical collisions — the algorithm is considered "broken" for security and should be replaced.

Hashing vs Encryption vs Encoding

These three are constantly confused. The difference is reversibility:

  • Cryptographic hashing is designed to be one-way. You can still guess likely inputs and compare digests. Used for change detection and inside password hashers and signatures.
  • Encryption is two-way. With the right key, ciphertext can be decrypted back to plaintext. Used to conceal data.
  • Encoding (like Base64) is not security at all — it's a reversible format change anyone can undo.

If someone says they "encrypted the passwords with SHA-256," they're mistaken — SHA-256 is hashing, not encryption.

Common Hash Algorithms

Algorithm Output size Status Typical use
MD5 128-bit ❌ Broken Legacy checksums only
SHA-1 160-bit ❌ Broken Deprecated
SHA-256 256-bit ✅ Secure General-purpose, certificates, blockchains
SHA-512 512-bit ✅ Secure SHA-2 applications using a 512-bit digest
bcrypt / Argon2 varies ✅ Secure Password hashing (deliberately slow)

A key nuance: for passwords, you actually want a slow hash like bcrypt or Argon2, not a fast one like SHA-256. Speed helps attackers brute-force billions of guesses, so password hashes are intentionally expensive to compute.

What Hash Functions Are Used For

  • Password storage — use a salted, tunable password hasher such as Argon2id, scrypt, or bcrypt. Plain SHA-256 is too fast. See OWASP's guidance.
  • File integrity — compare a SHA-256 digest against a value from a trusted source to detect changes. A digest posted by an attacker alongside a replacement file does not prove authenticity.
  • Digital signatures — you sign the hash of a document, not the whole document, because it's fixed-size.
  • Data structures — hash tables (dictionaries, maps) use non-cryptographic hashes to index data in near-constant time.
  • Deduplication & caching — identical files produce identical hashes; use a byte-by-byte comparison if a rare collision would matter.

Try It Yourself

The fastest way to understand hashing is to watch it happen. Paste any text into our SHA-256 Hash Generator and change one character — you'll see the entire output transform. Compare algorithms side by side with the MD5 and SHA-512 generators, and for keyed hashing (message authentication), see the HMAC Generator.

Frequently Asked Questions

What is a hash function in simple terms? It's a function that turns any data into a fixed-length "fingerprint." The same data always gives the same fingerprint, but you can't work backward from the fingerprint to the data.

How does a hash function work? It runs the input bytes through many rounds of mathematical mixing (bit shifts, modular addition, compression) until the output is uniformly scrambled and fixed in length.

Can a hash be reversed? No. A secure hash is one-way by design. The only way to "reverse" it is to guess inputs and hash them until one matches — which is why strong, slow hashing matters for passwords.

What makes a good cryptographic hash function? It should be deterministic and resist practical preimage and collision attacks. SHA-256 is a current general-purpose choice; MD5 and SHA-1 have practical collision attacks. NIST FIPS 180-4 specifies SHA-2 algorithms.

What's the difference between hashing and encryption? Hashing is one-way and used to verify data. Encryption is two-way (reversible with a key) and used to hide data so it can be read again later.

Related Reading

A cryptographic hash supplies a stable digest, but the surrounding protocol determines whether it detects accidents, authenticates a file, or safely handles passwords.