Skip to content

explain like I’m five: what is difference between hashing and encryption?

It’s a common point of confusion, but encryption and hashing are two distinct operations. Let’s clarify:

  1. Encryption:
  • Purpose: Encryption is the process of converting data into a code to prevent unauthorized access. The main point of encryption is that you can decrypt the data, turning it back into its original form.
  • Usage: Used when data needs to be retrieved in its original form by the intended recipient. For instance, if you’re sending a confidential message, you’d encrypt the message so that only the intended recipient, who has the decryption key, can read it.
  • Keys: Encryption uses keys (like a 256-bit key) for both the encryption and decryption processes. There are two main types of encryption: symmetric (one key for both encryption and decryption) and asymmetric (a pair of keys: public key for encryption and private key for decryption).
  1. Hashing:
  • Purpose: Hashing is the process of converting data (like a password or a message) into a fixed-size string of bytes, which typically looks random. The output, called a hash value or simply hash, is unique (ideally) to the given inputs. Unlike encryption, hashing is one-way: you can’t get the original data back from its hash.
  • Usage: Commonly used for verifying data integrity and storing passwords. For example, instead of storing user passwords directly, systems store their hashes. When a user logs in, the system hashes the entered password and checks if it matches the stored hash.
  • Hash Functions: Hash functions (like SHA-256) take an input and return a fixed-size string, regardless of input length. This string should change significantly with even a minor change in input.

In the context of cookies or any other data:

  • If you want to hide the actual content of the cookie but might need to access the original content later, you’d encrypt it.
  • If you just want to ensure the content hasn’t been tampered with, you’d generate a hash of the content (possibly adding a “salt” for extra security) and check against that hash later.

Remember: If you’re hashing passwords or sensitive data, always use a well-established and secure hashing algorithm (e.g., bcrypt for passwords) and consider salting the hashes to prevent rainbow table attacks.