A hash function is a function assigning each key in an input set a value in an output set . Usually is much smaller: a long name or large integer becomes a small bucket index. Equal keys must receive equal values from the same hash function; different keys may also receive equal values. Such a hash collision means "look in the same place", and does not establish key equality. If , the pigeonhole principle makes some collisions unavoidable.
For example, modular arithmetic gives the hash function , with seven bucket indices . Store the seven keys : their values are respectively . In particular, although . A hash table is a data structure that uses these indices to locate records. With separate chaining, each bucket holds a linked list of records, so bucket retains all four keys . Inserting must not overwrite merely because their hash function values agree.
Searching for computes bucket , then compares the actual keys until it reaches . Searching for absent key reaches that same bucket and must reject every stored key. This data structure trades a search through all records for a search through one short chain, provided the hash function distributes keys adequately. A fixed hash function can perform poorly on patterned keys: already demonstrate the problem. A universal hash family introduces controlled randomness to limit the expected value of this extra work. Neither indexing nor a small collision probability inherently supplies encryption or message authentication; a cryptographic hash function has additional requirements such as collision resistance.
Articles by others on the same topic
A hash function is a mathematical algorithm that takes an input (or "message") and produces a fixed-size string of bytes, typically in the form of a hash value or hash code. The output is usually a numerical representation of the original data, and it is designed to uniquely correspond to the input data. Here are some key characteristics and properties of hash functions: 1. **Deterministic**: For a given input, a hash function will always produce the same output.
Applications:
- hash map which is a O(1) amortized implementation of a map
- creating unbreakable chains of data, e.g. for Git commits or Bitcoin.
- storing passwords on a server in a way that if the password database is stolen, attackers can't reuse them on other websites where the user used the same password: security.blogoverflow.com/2013/09/about-secure-password-hashing/
