- Title:
- Coding Interview Prep: Hashing in JavaScript
- Image:
-
- Message:
-
Hashing is the process of taking a value, a password for example, and applying a mathematical operation. The mathematical operation is called a hashing algorithm or a hashing function and the resulting value is called a hash, hash value, or digest message.
Hashing a value will always produce the same hash value/digest message because you are applying a mathematical operation to a value. This is useful because it means that instead of storing passwords in a database as plain text you can hash them and store the hash value/digest methods — this obviously increases security. Additionally, hashing can be used for version control. You can compare the hash value/digest message from two files and if they are the same you know that nothing has been changed in the files.
A hash table is a data structure that stores key-value pairs. Hash tables use a hashing function that takes a value, performs a mathematical operation, and produces a hash value that is an integer. The hash table will use this integer as the index of the value of the key being stored.
This is much easier to understand with an example so let’s imagine that you want your hash table to store vehicle registration numbers and vehicle owners. The registration numbers are the key and the owners are the value. The key (the registration number) is passed to the hashing function and the hashing function produces an integer which is the index in the table where the value (the owner) is stored. So when you want to know who owns a vehicle you can hash the registration number and the table knows the index in the hash table at which the owner can be found.
One of the biggest benefits of using a hash table is performance. Instead of searching one by one through an array or similar data structure, you could use a hash table and enable your program to operate O(1) instead of binary search’s O(log n). (For more information about Big O Notation in web development please check out my blog here).
Hashing and encryption look very similar however, there are a couple of key differences between them.
Hashing provides a mapping between an input which can be of any length to an output which usually a fixed-length or smaller. Encryption provides mapping between an input and output, both of which can of any length.
Good encryption looks like random noise and can often result in the identical value being produced for different inputs. However, hashing produces one output per input and the chances of different inputs producing the same output (otherwise known as a collision) are astronomically low if your inputs are small.
Passwords are a good use case for hashing because you don’t actually need the password plain text. When authenticating a user, all you need is the hash value of the stored password and the hash value of the submitted password and if they are the same, the passwords match.
On the other hand, credit card numbers are a good use case for encryption because you need the plain text number but definitely don’t want to store a plain text credit card number in your database. Instead, you can encrypt the card number, store the encrypted data and key your encryption key as safe as possible.
Hashing is an important topic with far-reaching ramifications and potential. It is relied upon by cryptocurrencies like Bitcoin and has other interesting applications. We have just started to scratch the surface here with understanding how it works and I am looking forward to exploring it further.
- Date of publication:
- Wed, 01/27/2021 - 11:39
- Link:
-
Click on the link - it will be copied to clipboard
- Source:
- medium.com
Coding Interview Prep: Hashing in JavaScript
- Section: