Hashing is the process of converting a given key into another value. A **hash function** is used to generate the new value according to a mathematical algorithm. The result of a hash function is known as a **hash value** or simply, a **hash**.

A good hash function uses a **one-way** hashing algorithm, or in other words, the hash cannot be converted back into the original key.

### Collisions

Keep in mind that two keys can generate the same hash. This phenomenon is known as a **collision**. There are several ways to handle collisions, but that’s a topic for another time.

## Applications

Hashing is most commonly used to implement *hash tables*. A hash table stores key/value pairs in the form of a list where any element can be accessed using its index.

Since there is no limit to the number of key/value pairs, we can use a hash function to map the keys to the size of the table; the hash value becomes the index for a given element.

A simple hashing approach would be to take the modular of the key (assuming it’s numerical) against the table’s size:

index = key \text{ } MOD \text{ } tableSize*in**d**e**x*=*k**ey* *MO**D* *t**ab**l**e**S**i**ze*

This will ensure that the hash is always within the limits of the table size. Here is the code for such a hash function:1234567def hashModular(key, size): return key % sizelist_ = [None] * 10 # List of size 10key = 35index = hashModular(key, len(list_)) # Fit the key into the list sizeprint(“The index for key ” + str(key) + ” is ” + str(index))Run

Hashing is also used in data encryption. Passwords can be stored in the form of their hashes so that even if a database is breached, plaintext passwords are not accessible. MD5, SHA-1 and SHA-2 are popular cryptographic hashes.