# What Is The Difference Between Encryption And Hashing?

## Table of content:

- What is Hashing?
- What is Collision?
- Types of Hashing Algorithms
- Applications of Hashing
- What is Encryption?
- Applications of Encryption
- Hashing vs Encryption

Hashing and Encryption might sound very similar when you know their final purposes but they are very different from each other. Hashing is done using a hash function and its main motive is to ensure data or file integrity, whereas encryption uses keys to make sure of data confidentiality. Now, Let’s try to know more about how Hashing and Encryption work.

What Is Data Independence In DBMS? Understand Its Importance With Examples

# What is Hashing?

Hashing is an algorithm that takes in any size of data and converts it into a unique length value with the help of a hash function. It is a one-way process, i.e., once hashing is done it cannot be converted back to the original input. Usually, the size of input strings is greater than the strings we get after using the hashing functions. We often use hashing in our data structures and also in password verification.

*Did you know hashing is used while compiling? Yes! All the keywords such as if, else, switch, break, for, while etc, are stored in a **hash table**, it helps the compiler differentiate from other identifiers and successfully compile the program.*

# Understanding Hashing with an example

Let’s assume you own a stall in an exhibition and your sole purpose is to make money and the things you have are water, lemon, sugar, tea powder, glasses, and a stove. You decided to make dishes out of it and sell them to earn money. What all dishes can you make? Ingredients are inputs, the procedure is hashing and the final output is hashed value/string. You can make 3 dishes out of 7 ingredients — lemonade, black tea and sugar water.

# Characteristics of a good hash function

- Deterministic: A good hash function is always Deterministic. If you add the same inputs (sugar+water) your output should be the same (sugar water).
- Distinct: It should produce distinctive hash outputs for similar inputs. The only difference between lemonade and sugar water is lemons but still, they form 2 different dishes.
- Irreversible: Hash digest (or Hashed output) cannot be retrieved back to original strings (input). Just like how It is not possible to get back the lemons from the lemonade, it is not possible to reverse engineer any hash values.

## Basic hash functions

- Identity Hash Function: This is used when the data is small (like integer values). The key itself is the hashed value. The cost of computing the hash value is effectively zero as the value itself is the hash value and there is no computation. There are no collisions as every key is distinct and so is the hash value
- Division Hashing: In this hashing algorithm, we map the key into one of the slots by dividing the remainder of the key by the table size. This is written as h(k) = k mod m, in mathematical logic. M is a number that allows it to generate distinctive hash values. The table size is usually a power of two, resulting in a distribution ranging from 0 to m-1. There are chances of collisions i.e., the same slot might be allocated to 2 different keys.
- Digit Folding: In this method, the number is split into several parts, and all parts are added up to get a new number. This method can give rise to lesser collisions, as the sum is mostly different.

# What is Collision?

It is a condition where 2 keys get the same hash value. A collision occurs when a newly inserted key matches to a slot in the hash table that is already occupied. Consider a hash function whose output is always the remainder when the key is divided by 5, f(x)= n mod 5, The has value for 5, 10,15 and all other multiples of 5 are same, this leads to a collision. We have different collision resolution techniques.

# 1. Open Hashing (Chaining)

Data is not stored in Hashtable’s Hash key but another data structure is used i.e linked list. If 2 or more hash codes point to the same index in a hash table, data is stored in linear form.

# 3. Closed Hashing

This is another technique in which there is no additional data structure. When a collision occurs the newly added hashcode is stored in the next possible empty cell. We can find the next possible cell in 3 different ways — Linear probing, Quadratic Probing, Double Hashing

Importance Of Data Transformation In Data Mining

# Types of Hashing Algorithms

There are many kinds of Hashing Algorithms. Let’s look at some of the popular ones -

- MD-5: It is a cryptographic hashing algorithm designed in 1991. This algorithm takes an input of any length and gives an output of 128-bits. It is not widely used now, as hackers have decoded the algorithm.
- RIPMEND (RACE Integrity Primitives Evaluation Message Digest): It has a family of algorithms, namely, RIPEMD-128, RIPEMD-160, RIPEMD-256 and RIPEMD-320. The number in the name is the output length of the message digest.
- SHA (Secure Hashing Algorithm): The algorithms in the SHA family are the most secure as the hash digest cannot be cracked by any bitwise or arithmetic operators. The later versions of this algorithm have also added randomness to the code. There are 2 types — SHA-1 and SHA-2. SHA-256, SHA-512, SHA-384 are versions of SHA-2. These algorithms are slower and more complex compared to MD5 and RIPEMD.

# Applications of Hashing

- Data Storage — Personal and confidential information — Passwords, bank details
- Digital Signature — It is a cryptographic application used to generate random strings
- String Matching Algorithms — Rabin-Karp
- Message Digest
- Document and File Management

15 Popular JAVA Frameworks Use In 2022

# What is Encryption?

Encryption is a process of converting plain text (Original text) to unreadable (gibberish) text, also called ciphertext, using an encryption key that can be decoded later using a decryption key. The objective of encryption is to protect the data from unauthorised parties. It is the most advanced method to ensure data security in modern communication systems. The major difference between encryption and hashing is that hash codes cannot be decrypted to obtain the original text or file, whereas the encryption process can be decrypted. Some examples of encryption algorithms are — RSA, AES (Advanced Encryption Standard), IDEA, Twofish encryption algorithms

There are two different types of encryption — Symmetric and Asymmetric Encryption

# Symmetric Encryption Algorithm

It is as simple as having 2 keys for one lock and only the people who have access to that key can open it. In Symmetric encryption, we have one security key that both sender and receiver have and that key is used to encrypt and decrypt the message. Hence, it is called a public key. The sender encrypts the message using the public key and the receiver uses the same public key to decrypt the message.

# Asymmetric Encryption Algorithm

Unlike symmetric encryption, here we have 2 different keys — Public key and Private Key. A public key is available for everyone and is used to encrypt the message whereas a Private key is available only at the receiver’s end and is used for decrypting the message. It is slower compared to symmetric encryption and requires more processing power for computation as there is an extra key.

# Applications of Encryption

- Emails
- End-to-end encryption in messenger Applications like WhatsApp, Instagram, Google Duo
- Message Authentication
- Data Exchange between servers and browsers
- Digital Certificates
- Database encryption

Top 50 Selenium Interview Questions And Answers For 2022

# Hashing vs Encryption

# Hashing

- Definition — Original Input file is hashed to get
- It is a one-way function
- the original message cannot be retrieved back
- The purpose of hashing is data integrity
- Types of Hashing Algorithms — MD5, RIPEMD, SHA etc.
- Applications — Digital Signature, Storage of personal information

# Encryption

- Definition — It is a reversible process, that can be decoded back to its original format.
- It is a two-way function.
- The original message can be obtained back by the decryption process.
- The purpose of encryption is Data confidentiality
- Two types of encryption — Symmetric and Asymmetric
- Applications — Messaging platforms, data exchange between servers

# Summing up…

We cannot replace hashing with encryption, and vice-versa. Both of them have their own applications and methodologies. With the increase in cyber-attacks and the need for cybersecurity, more hashing and encryption algorithms are being created to protect data. In case of any data theft or leak, hashing and encryption of data can cause no harm as no one can understand the text and it is not easy to break it either.

You might also be interested in reading: