How JavaScript uses Hashing

Daniel Leitch
Nerd For Tech
Published in
5 min readFeb 10, 2021

--

TLDR — Hashing has to do with taking a certain value (for example your password), applying some kind of mathematical operation to it (MD5, SHA-1, SHA-2) called a hashing algorithm or a hashing function, and getting the resulting changed value (known as a hash or hash value or digest message). This is commonly used for passwords and checking document accuracy. See the below image:

Source

They are three main uses for hashing namely document hashing for ensuring accuracy, password hashing for increased security and hash tables for indexing data.

1. Document Hashing for Ensuring Accuracy

Here is the problem document hashing solves: let us say you have a large legal document being emailed around and the receipt has no way to tell if someone has altered one of the many pages of the documents. Perhaps someone added a ‘0’ to the sales price or took out a clause. How would you know?

Source

Document hashing can solve this problem as the sender could produce a hash value of the document by typing:

(Example in Lunix)#Input
sha256sum /path/to/file
#RESULTING HASH VALUE
c01b39c7a35ccc3b081a3e83d2c71fa9a767ebfeb45c69f08e17dfe3ef375a7b

Once done the sender could send the file as per normal along with the hash value. The receipt of the document could repeat the above steps and then compare the hash values. Should they not be the same you know that the document is not the same and that was sent it has been changed in some way, see example below.

Source

A real-world example of this can be seen on Ubuntu’s website where their ISO for their operating system comes with a hash value so that should you want to check you can be sure that what you have is the exact same one they released. See here for more of this

2. Password Hashing for Increased Security

For security reasons, you may want to store passwords as a hash value (it is industry standard to store passwords in a hash value). These guards against the possibility that someone who gains unauthorized access to your database can retrieve the passwords of every user in the system. Hashing performs a one-way transformation on a password, turning the password into another String, called the hashed password. “One-way” means that it is practically impossible to go the other way — to turn the hashed password back into the original password.

See this Node.js example below.

// Node.js using the crypto moduleconst crypto = require ( 'crypto' ); //line 1const hash = crypto.createHash( 'sha256' ); //line 2hash.update( 'some data to hash' ); //line 4console .log(hash.digest( 'hex' )); //line 5// Prints:// 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50

When a member attempts to log in the Personalization module takes the supplied password, performs a similar one-way hash, and compares it to the database value. If the passwords match, then login is successful if not a login error will occur. See the below example.

Source

3. Hash Tables for Quick Data Indexing

You can also have hashing functions that receive a string or number value as input and produce an integer as output. These types of hashing algorithms are used to create hash tables. Hash tables store key-value pairs. For example, imagine that you would like to store the names and telephone numbers of a group of people. The key could be the person’s name and the value could be the telephone number (see the image below).

Source

To create a hash table to store this information, the key would be passed to the hashing function. The hashing function would produce an integer number as output. This number would be used as the index of where the value would be stored in the hash table. When you want to look up a person’s telephone number, you can hash that person’s name and know exactly where (at which index) in the hash table the number can be found.

Collisions

It is possible that, when filling up the hash table, you get collisions. A collision is when two or more keys have the same hash value and are thus given the same index number in the hash table. Collisions can be handled by using either separate chaining (if the position is already full, place the value in a list associated with that index) or open addressing (if the position is already taken, place the value in another open position in the hash table).

cs.usfca.edu

This tool above can help visualize how each of the different open addressing methods works. A top tip would be to slow it down and look at the math taking place and to understand what is going on and then to input that same number in again. Watch how it resolves the collision with open addressing.

Thanks for reading this I hope it helped you understand the basics of hashing.

Check me out on LinkedIn, GitHub

Resources

Hashing vs Encryption Differences

Oracle.com

--

--

Daniel Leitch
Nerd For Tech

I'm a Front-end Developer 🚀 and Linux Enthusiast