How JavaScript uses hashing

 


INTRODUCTION

The aim of this blog is to explain what hashing is and what it is used for and also discuss what a hash table is and what the benefits of this data structure are. Moreover, identify and explain  the differences  between hashing and encryption. Since in JavaScript, Map objects are often implemented using hash tables will investigate what a Map object is and how it can be used. Lastly, compare objects and maps. In the comparison, referring to how hashing is used for each.

What is hashing ?

Hashing is the transformation of a string of characters into a generally more limited fixed-length value or key that represents the first string. Hashing is used to index and retrieve items in a database because it is faster to find the item using the shorter hashed key than to find it using the original value. It is also utilized in numerous encryption algorithms.

Why do we use  hashing ?

Hashing gives a safer and flexible method for recovering information compared with some other information structure. It is faster than looking for arrays and lists. In the very reach, Hashing can recuperate data in 1.5 probes, whatever is saved in a tree. Hashing, unlike other data structures, doesn't define the speed. A balance between space and time must be maintained while hashing. 

There are two  ways of keeping up this balance. 

1. Controlling speed by choosing the space to be allocated for the hash table 

2. Controlling space by picking a speed of recuperation

Hashed passwords can't be changed, taken, or imperilled. No well-recognized and effective key or encryption scheme exists that can be misused. Additionally, there is no compelling reason to stress if a hash code is taken since it can't be applied elsewhere. 

Two files can be compared for equality easily through hashing. There is no need to open the two documents individually. Hashing compares them word-by-word and the computed hash value instantly tells in the event that they are distinct. This advantage can be used for verification of a file after it has been shifted to a new place.




What a hash table is and what the benefits of this data structure are


Hash table is a data structure which stores data in an array format of fixed in size. In hash table, the data is stored in the form of key / value pair. Using of hash function, it computes an index of an array where the data value to be stored. If the table size is referred to as H_TableSize, then the table have a range from 0 to H_TableSize-1 indexes in which each index is unique.

What the difference is between hashing and encryption.


Breaking Down Hashing 

At the point when we talk about hashing, we're discussing a one-way process that utilizes an algorithm to take data and convert it to a fixed length known as a hash value(otherwise called a hash digest). The length of the hash produced is generally fixed and smaller than the first content or string;  though it varies widely with even the smallest variations in input. It is humanly impossible to revert a good hashing value to its original structure. 

There are diverse hashing algorithms utilized in hashing. Here are the most significant ones: 

MD5: MD5 used to be the most mainstream hash algorithm which converted a 16-byte hash an incentive to a 32-byte hexadecimal number. It has been deprecated from use on account of weaknesses found in it, however it can in any case be utilized as a checksum to verify data integrity only  against unintentional corruption.

SHA-0: SHA-0 is the first SHA algorithm of the three groups of SHA algorithms. SHA-0 has been deprecated from use because of its susceptibilities. This algorithm was before long replaced by the SHA-1 algorithm. 

SHA-1: SHA-1 is the successor of the SHA-0 and became the most widely adopted algorithm of the SHA family. It produces a 160-bit (20-byte) hash value known as a message digest — typically rendered as a hexadecimal number that’s 40 digits long. However, it was found to be insecure and since 2010, many organizations recommended its replacement with SHA-2 algorithms. In 2017, all major browsers deprecated the use of SHA-1. 

SHA-2: After being deemed insecure by major platforms, SHA-2 replaced SHA-1 and became the most widely adopted hashing algorithm. It might be carrying the family name but SHA-2 is significantly different from its predecessor. The SHA-2 family consists of SIX hash functions — SHA-224, SHA-256, SHA-384, SHA-512, SHA-512/224, SHA-512/256 — with digests (hash values) that are 224, 256, 384 or 512 bits


Uses of Hashing: 

  • In contrast to encryption, hashing serves  as a checksum to guarantee that a specific piece of data or a document hasn't been modified. 
  • Hashing is the most reasonable way to safely store passwords. By putting away passwords in a good hash design, it's practically impossible for anybody to get to your raw data. 
  • Hashing is useful in comparing a value and with a stored value hence evading duplication. This can be done by putting away the hash with a salt, and then with any future login attempts, hash the passwords that the clients enter and compare it and the put away hash. 
  • Hashing is utilized in an variety of digital certificates, including SSL certificates. Hashing helps you find specific data in a huge database.
  • Hashing algorithms are utilized like a digital certificate in cryptographic applications.

Breaking Down Encryption: 

Encryption is the process of turning a data into a series of unreadable characters which are not of a fixed length. The key difference between encryption and hashing lies in the fact that in case of encryption, the unreadable data can be decrypted to display the original plaintext data with the help of the right key, whereas in hashing, this cannot be done at all.

Encryption of data is done through the use of cryptographic keys. The data is encrypted before it’s transmitted and decrypted by the user. Based on the nature of the keys, encryption can be done in two ways — namely, symmetric encryption and asymmetric encryption.

Symmetric Encryption: In case of symmetric encryption, the keys used for both encryption and decryption are the same. That is, the data can be encrypted and decrypted using the same cryptographic key.
Asymmetric Encryption: In this case, the keys used for encryption and decryption are different. The key used for encryption is known as the public key, whereas the key used for decryption is the private key. As the name suggest, public key is known to every user that visits the website, whereas the private key is only available to the intended recipient or party.

Uses of Encryption

At its core, encryption is all about asserting identity and protecting data integrity:

The origin of encrypted messages can be traced, thus facilitating authentication of the message source.
In case the data gets leaked, it’s easy to trace the source. In other words, it’s easy to trace who did it and when, thus making auditing for accountability easy. It helps in resolving security breaches efficiently.
As the name suggests, encryption encrypts a data in such a way that only intended parties with the right private key can read the data or find the information in the data.
Encrypted messages cannot be exchanged or read by another person — it can only be read by the intended recipient.

What a Map object is and how it can be used, and Comparing objects and maps.


Map is a data structure which helps in storing the data in the form of pairs. The pair consists of a unique key and a value mapped to the key. It helps prevent duplicity.
Object follows the same concept as that of map i.e. using key-value pair for storing data. But there are slight differences which makes map a better performer in certain situations.

Few basic differences are as follows:

  1. In Object, the data-type of the key-field is restricted to integer, strings, and symbols. Whereas in Map, the key-field can be of any data-type (integer, an array, even an object!)
  2. In the Map, the original order of elements is preserved. This is not true in case of objects.
  3. The Map is an instance of an object but the vice-versa is not true.
Example
 <script> 
    let map = new Map([ 
        [5, 4], 
        [7, 9] 
    ]); 
  
    //output:true 
    console.log(map instanceof Object);  
  
    //output:false 
    let obj = new Object(); 
    console.log(obj instanceof Map);  
</script> 
Output:

true
false
Let’s dive deep into some more concepts which makes Object different than Map.


Declaration:

In JavaScript, there are many ways to create an object. For example:

let obj = {}; 
let obj = {1:"Object Name", 2:"Test"}; 
console.log(obj); 

Output:

Use of constructor:


let  obj = new Object(); //Empty object 
let  obj = new Object; 
console.log(obj); 
Output:



Using object.create
function fruits() { 
        this.name = 'fruit 1'; 
        this.season = 'summer'; 
        } 
  
        function apple() { 
        fruits.call(this); 
        } 
  
        apple.prototype = Object.create(fruits.prototype); 
        const app = new apple(); 
        console.log(app.name); 
        console.log(app.season); 




      • On the other hand, the Map has only one way of creation.


        let map = new Map();//Empty 

        console.log(map); 

        let map = new Map([[1, "Sam"], [2, "John"]]); // 1-> Sam, 2->John 

        console.log(map); 

        Output:


        Map(0)

        Map(2)

  1. Accessing Element:
    • Map uses its inbuilt get() method for accessing its elements.
      map.get(1);
    • Object simply uses the ‘key’ name with a dot operator to access its elements.
      obj.id;
      obj[id];
  2. Check if a key already exists:
    • Map uses it’s inbuilt has() function for this.
      map.has(1);//returns boolean value true or false.
    • Object uses ‘===’ operator for performing the task.
      var doExist = obj.1 === undefined; //returns boolean value.
  3. Adding new Element:
    • Map uses set() method to add new element.
      map.set(4, 5); //{5->4, 7->9, 4->5}
    • In object, it is done directly.
      obj["Demo"]="Map vs Object"; //{1->Object Name, 2->Test, Demo->Map vs Object}
  4. Getting the size:
    • Map automatically updates its size and get the easiest.
      console.log(map.size);
    • In object, the size needs to be calculated manually with the help object.keys().
      console.log(object.keys(obj).length);
Hence we can see Map is having better performance and less to write code structure which gives it an edge over Object. However, there are some scenarios which requires object to be used. Let us see.

WHEN AND WHERE TO USE OBJECT:

  • Objects are a great choice for situations where we need simple structure to store data and the type of keys needed is either an integer, strings or symbols.
  • Scenarios which needs the application of separate logic to individual property element, the object is definitely the choice.
  • JSON gives direct support for object but not with map(yet).
  • Map is completely hash whereas Object is more than that.
Although Map tends to have more advantages over objects, at the end the day it depends on the kind of data being used and the operation needs to be performed.
However, of all the advantages of map over object, map cannot replace object in JavaScript because Object is much more than a hash table. It shouldn’t be used just for the purpose of hashing if there exists another choice.


Comments

Popular posts from this blog

Big O notation basics for web developers

Interfaces In Object Oriented Programming Languages and Prototype-Based Languages