How JavaScript Uses Hashing

What the heck is Hashing???... #whattheheck #dontknow #letsfindout

What do coders mean when they talk about hashing? 

It's actually not as complicated as you might think and simply put, refers to a process where we can convert a given key into another value.

An example of why we would want to use hashing:

Let's just say that we have a user that stores their password into our database, we would be responsible for keeping that password safe and secure and there is a way to achieve this. We would be able to use hashing that allows us to generate a key that masks the password code of the user. This way the password is somewhat hidden and disguised which is one of the many ways we can go about keeping it safe within our database.

How exactly would we be able to achieve this process?

Well we can use a hash function to generate a unique and new value to a key by using a mathematical algorithm. The converted value is known as a 'hash value', 'hash' or whatever floats your boat.🚢 I'm sure that there are many known terms amongst coders for hash values.

Hashing is used in data encryption where passwords can be stored in the form of their hashes so even though a database is breached, the password will not be accessible, which is exactly what we need and want for the integrity of our database to be maintained.

There are various crypto hashes that are available for us to use such as MD5, SHA-1 & SHA-2...which are a few well known cryptographic hashes to use.

Hashing is commonly used to implement hash tables:

Hash tables are used to store key/value pairs in the form of a list which allows us to access any of these elements by using the table index.

We are able to use a hash function to map the keys to the size of the tableand the hash value becomes the index for the given element.

One of the main advantages to using hash tables, is speed, especially when handling large amount of data entries. It is also faster because it points straight to the exact location of where the record is stored.

Don't confuse hashing and encryption for the same thing because they do have their differences:

Hashing uses a one-way function that scamples plain text to produce a unique message digest whereas encryption is a two-way function that is encrypted but it can be decrypted using the proper decryption key. Hence it is better practice to use hashing as the hashing process cannot be reversed to reveal the original password.

JavaScript often uses Map objects that are implemented using hash tables:

A hashmap is basically a data structure that serves as a map, the map object holds key-value pairs and also remembers the original location of the keys which can help us to easily map and find them.

Maps and objects operate different:

A map stores keys that are in a specific order which is useful to find certain keys within the order.
Maps can store any type of value as a key being objects, functions or primitives. Maps really come in handy with increased data entries or removals as it is optimised accordingly. The entries of a map can be found in a constant time frame.

Object however don't store keys within a particular order and the keys may only be string or symbols opposed to maps that are far more flexible storing values of any kind. The number of entries in a object need to be counted which can be quite time consuming.

It is also important to be aware that objects have a prototype, so there is a risk of keys colliding if not dealt with carefully.

Let's see our hashing is used with objects and maps:

There are two ways of constructing a hash instance, the first way is to use a regular JavaScript object with the 'new' keyword or the second, is to use a hash function that passes a plain JavaScript object or a hash to any of them to clone it which will keep the original object intact.

That's all for today folks...until next time...👌












Comments

Popular Posts