Skip to content
IT glossary
AlgorithmsUpdated December 3, 2026

Hash function

Хеш-функция

It turns data such as a string or a file into a compact value called a hash. The same input gives the same hash under the same settings, but different inputs can share a hash.

What a hash function does

A hash function computes a value from its input. That value, called a hash, is often a fixed-size integer or byte sequence. SHA-256, for example, produces 256 bits whether you give it one byte or a large file. The result is repeatable for the same input and settings. It is not necessarily unique: when there are more possible inputs than outputs, some distinct inputs must share an output. That's a collision.

From a hash to a lookup

Say a dictionary uses usernames as keys. Rather than scan every username, it hashes the requested key and uses the result to choose where to look. A simple implementation with m buckets might use hash(key) % m as the bucket index. This creates two ways for keys to end up together: they can have the same hash, or their different hashes can map to the same bucket. With 10 buckets, hashes 12 and 22 both map to bucket 2. A table that stores a list of entries in each bucket then checks the actual keys for equality.

With well-distributed hashes and enough buckets for the number of entries, a lookup examines a constant number of entries on average. In a table that uses lists for its buckets, a lookup can examine all n entries if they pile into one bucket. Hashing the key is a separate cost: if the function reads every byte of a string, doubling the string length doubles the number of bytes it reads.

Different jobs need different hashes

  • Hash tables and sets need fast hashing and a good spread of keys across buckets. A function that just adds up the bytes of a string is a poor fit for anagram-heavy data: rearranging the same letters leaves the sum unchanged.
  • File integrity checks can use a cryptographic hash such as SHA-256. Compare the file's digest with an expected digest from a trusted source. A match with a digest supplied by the same untrusted source as the file doesn't establish authenticity.
  • Password storage calls for a dedicated password hashing function such as Argon2id with a salt. A single SHA-256 call is designed to be fast, which makes it a poor choice when the goal is to make each password guess costly.

Common interview traps

A hash isn't encrypted data: there is no decryption step that recovers the input. Also, don't assume a language's built-in hash is a persistent identifier. For example, Python's hash() for strings can return different results across processes. If you're implementing keys for a hash-based collection, remember the one-way contract: equal keys must have equal hashes, but matching hashes don't make two keys equal. Keep the fields used for hashing and equality stable while the key is in the collection.