## Fundamental difference between Hashing and Encryption algorithms

difference between hashing and encryption in tabular form
hashing vs encryption vs encoding
hash encryption online
hashing algorithm
difference between hash and cryptographic hash
reversible encryption algorithm
encryption and hashing in c#

I see a lot of confusion between hashes and encryption algorithms and I would like to hear some more expert advice about:

1. When to use hashes vs encryptions

2. What makes a hash or encryption algorithm different (from a theoretical/mathematical level) i.e. what makes hashes irreversible (without aid of a rainbow tree)

Here are some similar SO Questions that didn't go into as much detail as I was looking for:

What is the difference between Obfuscation, Hashing, and Encryption? Difference between encryption and hashing

Well, you could look it up in Wikipedia... But since you want an explanation, I'll do my best here:

##### Hash Functions

They provide a mapping between an arbitrary length input, and a (usually) fixed length (or smaller length) output. It can be anything from a simple crc32, to a full blown cryptographic hash function such as MD5 or SHA1/2/256/512. The point is that there's a one-way mapping going on. It's always a many:1 mapping (meaning there will always be collisions) since every function produces a smaller output than it's capable of inputting (If you feed every possible 1mb file into MD5, you'll get a ton of collisions).

The reason they are hard (or impossible in practicality) to reverse is because of how they work internally. Most cryptographic hash functions iterate over the input set many times to produce the output. So if we look at each fixed length chunk of input (which is algorithm dependent), the hash function will call that the current state. It will then iterate over the state and change it to a new one and use that as feedback into itself (MD5 does this 64 times for each 512bit chunk of data). It then somehow combines the resultant states from all these iterations back together to form the resultant hash.

Now, if you wanted to decode the hash, you'd first need to figure out how to split the given hash into its iterated states (1 possibility for inputs smaller than the size of a chunk of data, many for larger inputs). Then you'd need to reverse the iteration for each state. Now, to explain why this is VERY hard, imagine trying to deduce `a` and `b` from the following formula: `10 = a + b`. There are 10 positive combinations of `a` and `b` that can work. Now loop over that a bunch of times: `tmp = a + b; a = b; b = tmp`. For 64 iterations, you'd have over 10^64 possibilities to try. And that's just a simple addition where some state is preserved from iteration to iteration. Real hash functions do a lot more than 1 operation (MD5 does about 15 operations on 4 state variables). And since the next iteration depends on the state of the previous and the previous is destroyed in creating the current state, it's all but impossible to determine the input state that led to a given output state (for each iteration no less). Combine that, with the large number of possibilities involved, and decoding even an MD5 will take a near infinite (but not infinite) amount of resources. So many resources that it's actually significantly cheaper to brute-force the hash if you have an idea of the size of the input (for smaller inputs) than it is to even try to decode the hash.

##### Encryption Functions

They provide a 1:1 mapping between an arbitrary length input and output. And they are always reversible. The important thing to note is that it's reversible using some method. And it's always 1:1 for a given key. Now, there are multiple input:key pairs that might generate the same output (in fact there usually are, depending on the encryption function). Good encrypted data is indistinguishable from random noise. This is different from a good hash output which is always of a consistent format.

##### Use Cases

Use a hash function when you want to compare a value but can't store the plain representation (for any number of reasons). Passwords should fit this use-case very well since you don't want to store them plain-text for security reasons (and shouldn't). But what if you wanted to check a filesystem for pirated music files? It would be impractical to store 3 mb per music file. So instead, take the hash of the file, and store that (md5 would store 16 bytes instead of 3mb). That way, you just hash each file and compare to the stored database of hashes (This doesn't work as well in practice because of re-encoding, changing file headers, etc, but it's an example use-case).

Use a hash function when you're checking validity of input data. That's what they are designed for. If you have 2 pieces of input, and want to check to see if they are the same, run both through a hash function. The probability of a collision is astronomically low for small input sizes (assuming a good hash function). That's why it's recommended for passwords. For passwords up to 32 characters, md5 has 4 times the output space. SHA1 has 6 times the output space (approximately). SHA512 has about 16 times the output space. You don't really care what the password was, you care if it's the same as the one that was stored. That's why you should use hashes for passwords.

Use encryption whenever you need to get the input data back out. Notice the word need. If you're storing credit card numbers, you need to get them back out at some point, but don't want to store them plain text. So instead, store the encrypted version and keep the key as safe as possible.

Hash functions are also great for signing data. For example, if you're using HMAC, you sign a piece of data by taking a hash of the data concatenated with a known but not transmitted value (a secret value). So, you send the plain-text and the HMAC hash. Then, the receiver simply hashes the submitted data with the known value and checks to see if it matches the transmitted HMAC. If it's the same, you know it wasn't tampered with by a party without the secret value. This is commonly used in secure cookie systems by HTTP frameworks, as well as in message transmission of data over HTTP where you want some assurance of integrity in the data.

##### A note on hashes for passwords:

A key feature of cryptographic hash functions is that they should be very fast to create, and very difficult/slow to reverse (so much so that it's practically impossible). This poses a problem with passwords. If you store `sha512(password)`, you're not doing a thing to guard against rainbow tables or brute force attacks. Remember, the hash function was designed for speed. So it's trivial for an attacker to just run a dictionary through the hash function and test each result.

Adding a salt helps matters since it adds a bit of unknown data to the hash. So instead of finding anything that matches `md5(foo)`, they need to find something that when added to the known salt produces `md5(foo.salt)` (which is very much harder to do). But it still doesn't solve the speed problem since if they know the salt it's just a matter of running the dictionary through.

So, there are ways of dealing with this. One popular method is called key strengthening (or key stretching). Basically, you iterate over a hash many times (thousands usually). This does two things. First, it slows down the runtime of the hashing algorithm significantly. Second, if implemented right (passing the input and salt back in on each iteration) actually increases the entropy (available space) for the output, reducing the chances of collisions. A trivial implementation is:

```var hash = password + salt;
for (var i = 0; i < 5000; i++) {
hash = sha512(hash + password + salt);
}
```

There are other, more standard implementations such as PBKDF2, BCrypt. But this technique is used by quite a few security related systems (such as PGP, WPA, Apache and OpenSSL).

The bottom line, `hash(password)` is not good enough. `hash(password + salt)` is better, but still not good enough... Use a stretched hash mechanism to produce your password hashes...

##### Another note on trivial stretching

Do not under any circumstances feed the output of one hash directly back into the hash function:

```hash = sha512(password + salt);
for (i = 0; i < 1000; i++) {
hash = sha512(hash); // <-- Do NOT do this!
}
```

The reason for this has to do with collisions. Remember that all hash functions have collisions because the possible output space (the number of possible outputs) is smaller than then input space. To see why, let's look at what happens. To preface this, let's make the assumption that there's a 0.001% chance of collision from `sha1()` (it's much lower in reality, but for demonstration purposes).

```hash1 = sha1(password + salt);
```

Now, `hash1` has a probability of collision of 0.001%. But when we do the next `hash2 = sha1(hash1);`, all collisions of `hash1` automatically become collisions of `hash2`. So now, we have hash1's rate at 0.001%, and the 2nd `sha1()` call adds to that. So now, `hash2` has a probability of collision of 0.002%. That's twice as many chances! Each iteration will add another `0.001%` chance of collision to the result. So, with 1000 iterations, the chance of collision jumped from a trivial 0.001% to 1%. Now, the degradation is linear, and the real probabilities are far smaller, but the effect is the same (an estimation of the chance of a single collision with `md5` is about 1/(2128) or 1/(3x1038). While that seems small, thanks to the birthday attack it's not really as small as it seems).

Instead, by re-appending the salt and password each time, you're re-introducing data back into the hash function. So any collisions of any particular round are no longer collisions of the next round. So:

```hash = sha512(password + salt);
for (i = 0; i < 1000; i++) {
hash = sha512(hash + password + salt);
}
```

Has the same chance of collision as the native `sha512` function. Which is what you want. Use that instead.

Fundamental difference between Hashing and Encryption algorithms, Well, you could look it up in Wikipedia But since you want an explanation, I'll do my best here: Hash Functions. They provide a mapping  The basic difference between hashing and encryption is that hashing converts the data to message digest or hash, which is a number generated from a string of text while encryption uses encryption algorithms and a key to convert the message to transmit into an unrecognizable format. Use of Keys. Hashing does not use any keys while Encryption uses keys.

A hash function could be considered the same as baking a loaf of bread. You start out with inputs (flour, water, yeast, etc...) and after applying the hash function (mixing + baking), you end up with an output: a loaf of bread.

Going the other way is extraordinarily difficult - you can't really separate the bread back into flour, water, yeast - some of that was lost during the baking process, and you can never tell exactly how much water or flour or yeast was used for a particular loaf, because that information was destroyed by the hashing function (aka the oven).

Many different variants of inputs will theoretically produce identical loaves (e.g. 2 cups of water and 1 tsbp of yeast produce exactly the same loaf as 2.1 cups of water and 0.9tsbp of yeast), but given one of those loaves, you can't tell exactly what combo of inputs produced it.

Encryption, on the other hand, could be viewed as a safe deposit box. Whatever you put in there comes back out, as long as you possess the key with which it was locked up in the first place. It's a symmetric operation. Given a key and some input, you get a certain output. Given that output, and the same key, you'll get back the original input. It's a 1:1 mapping.

Differences between encryption and hashing -- GCN, Hashing, however, is a one-way function that scrambles plain text to produce a unique message digest. With a properly designed algorithm,  AES is a symmetric encryption algorithm while PGP is an example of an asymmetric encryption algorithm used today. The difference between hashing and encryption Hashing is used to validate the integrity of the content by detecting all modifications and thereafter changes to a hash output.

Use hashes when you don't want to be able to get back the original input, use encryption when you do.

Hashes take some input and turn it into some bits (usually thought of as a number, like a 32 bit integer, 64 bit integer, etc). The same input will always produce the same hash, but you PRINCIPALLY lose information in the process so you can't reliably reproduce the original input (there are a few caveats to that however).

Encryption principally preserves all of the information you put into the encryption function, just makes it hard (ideally impossible) for anyone to reverse back to the original input without possessing a specific key.

Simple Example of Hashing

Here's a trivial example to help you understand why hashing can't (in the general case) get back the original input. Say I'm creating a 1-bit hash. My hash function takes a bit string as input and sets the hash to 1 if there are an even number of bits set in the input string, else 0 if there were an odd number.

Example:

```Input    Hash
0010     0
0011     1
0110     1
1000     0
```

Note that there are many input values that result in a hash of 0, and many that result in a hash of 1. If you know the hash is 0, you can't know for sure what the original input was.

By the way, this 1-bit hash isn't exactly contrived... have a look at parity bit.

Simple Example of Encryption

You might encrypt text by using a simple letter substitution, say if the input is A, you write B. If the input is B, you write C. All the way to the end of the alphabet, where if the input is Z, you write A again.

```Input   Encrypted
CAT     DBU
ZOO     APP
```

Just like the simple hash example, this type of encryption has been used historically.

The difference between Encryption, Hashing and Salting, Hashing is the practice of using an algorithm to map data of any size to a fixed length. This is called a hash value (or sometimes hash code or hash sums or even a  Basically, a hash is a number that is generated from the text through a hash algorithm. This number is smaller than the original text. The algorithm is designed in such a way that no two hashes are the same for two different texts. And it is impossible (almost!) to go back from the hash value to the original text.

Basic overview of hashing and encryption/decryption techniques are.

Hashing:

If you hash any plain text again you can not get the same plain text from hashed text. Simply, It's a one-way process.

Encryption and Decryption:

If you encrypt any plain text with a key again you can get same plain text by doing decryption on encrypted text with same(symetric)/diffrent(asymentric) key.

UPDATE: To address the points mentioned in the edited question.

1. When to use hashes vs encryptions

Hashing is useful if you want to send someone a file. But you are afraid that someone else might intercept the file and change it. So a way that the recipient can make sure that it is the right file is if you post the hash value publicly. That way the recipient can compute the hash value of the file received and check that it matches the hash value.

Encryption is good if you say have a message to send to someone. You encrypt the message with a key and the recipient decrypts with the same (or maybe even a different) key to get back the original message. credits

2. What makes a hash or encryption algorithm different (from a theoretical/mathematical level) i.e. what makes hashes irreversible (without aid of a rainbow tree)

Basically hashing is an operation that loses information but not encryption. Let's look at the difference in simple mathematical way for our easy understanding, of course both have much more complicated mathematical operation with repetitions involved in it

##### Encryption/Decryption (Reversible):

```4 + 3 = 7
```

This can be reversed by taking the sum and subtracting one of the addends

```7 - 3 = 4
```

Multiplication:

```4 * 5 = 20
```

This can be reversed by taking the product and dividing by one of the factors

```20 / 4 = 5
```

So, here we could assume one of the addends/factors is a decrpytion key and result(7,20) is an excrypted text.

##### Hashing (Not Reversible):

Modulo division:

```22 % 7 = 1
```

This can not be reversed because there is no operation that you can do to the quotient and the dividend to reconstitute the divisor (or vice versa).

Can you find an operation to fill in where the '?' is?

```1  ?  7 = 22
1  ?  22 = 7
```

So hash functions have the same mathematical quality as modulo division and looses the information.

credits

Hashing vs. Encryption vs. Encoding: Explained by Experts, Hashing vs Encryption: Understand the difference between Hashing, The algorithm is designed in such a way that no two hashes are the same for two different Fundamentally, it is the process of transforming your confidential data into an  Data is encrypted with encryption algorithms, which are also known as ciphers. One of the most important distinctions between encryption and hashing (which we will get to later) is that encryption is designed to go both ways. This means that once something has been encrypted with a key, it can also be decrypted.

My one liner... generally Interviewer wanted the below answer.

Hashing is one way . You can not get convert your data/ string from a hash code.

Encryption is 2 way - you can decrypt again the encrypted string if you have the key with you.

Differences between hash- and encryption algorithms , When do we use a hash algorithm and when do we use an encryption algorithm (​a cipher)?. Hash functions are typically used to map an  Shortly, the difference between hashing and encryption: Encryption provides two-way functionality that allows data to be converted to ciphertext and back to data using a key. Hashing provides one-way functionality that produces a fixed length hashcode from data that is difficult to reverse back to data.

What is the difference between hashing and encryption?, Hashing and Encryption, both are the cryptographic functions/techniques to As told earlier RSA Algorithm, works with 2 keys, namely public keys and private keys. The fundamental difference is that encryption must be reversible whereas​  Fundamental difference between Hashing and Encryption algorithms (8) Encryption The Purpose of encryption is to transform data in order to keep it secret E.g (Sending someone a secret text that they only should able to read,sending passwords through Internet).

What is the Difference Between Hashing and Encryption, The main difference between hashing and encryption is that the hashing output cannot be Comparison of Key Differences In hashing, data goes through the hashing algorithm and provides a unique output but it is not  The fundamental difference between hash and encryption techniques is that hash is irreversible while encryption is reversible. Hash algorithms generate a digest of fixed length output cipher text for a given input plain text. The output text cannot be converted back to input text.

The Real Difference Between Hashing and Encryption, Like hashing, encryption uses an algorithm to encode its plaintext. The major difference is the encryption algorithm integrates a key to decipher  Hashing is the practice of using an algorithm to map data of any size to a fixed length. This is called a hash value (or sometimes hash code or hash sums or even a hash digest if you’re feeling fancy). Whereas encryption is a two-way function, hashing is a one-way function.