DEV Community

Cover image for Don’t Get Salted: A Beginner’s Guide to Hashing Algorithms
Saif Matab
Saif Matab

Posted on

Don’t Get Salted: A Beginner’s Guide to Hashing Algorithms

Ever wondered how websites securely store your passwords or verify downloads without actually holding the original data? The secret lies in a fascinating technique called hashing. Unlike a tasty snack (don’t get salted!), hashing algorithms don’t add anything to your data. Instead, they transform it into a unique, fixed-size value called a hash — like a digital fingerprint. This fingerprint ensures secure storage and verification without revealing the original data itself.

But how exactly does this magic work?

Imagine you have a secret message you want to share with a friend. A basic hashing algorithm might involve scrambling the letters in a specific way. For example, the word “password” might be transformed into “drowssap” using a simple substitution cipher. This scrambled version, the hash, is much less sensitive than the original message. Even if someone intercepts the hash, they wouldn’t be able to easily decipher the original password without knowing the specific scrambling technique.

In the world of computers, hashing algorithms are far more sophisticated, using complex mathematical functions to create unique and unpredictable hashes. These functions are one-way, meaning you can easily generate a hash from any data, but it’s nearly impossible to reverse the process and recreate the original data from the hash alone.

There are different types of hashing algorithms, each with varying strengths and weaknesses. Some popular examples include MD5 and SHA-256. These algorithms are used in various computer science applications, playing a crucial role in:

  • Secure Password Storage: Websites don’t store your actual password. Instead, they store the hash of your password. When you log in, the website generates the hash of your entered password and compares it to the stored hash. If they match, you’re granted access!
  • Data Integrity: Hashing algorithms can be used to verify that data hasn’t been tampered with during transmission or storage. By comparing the hash of the received data with the original hash, you can ensure its authenticity.
  • Digital Signatures: Hashing is a key component of digital signatures, which allow for secure electronic document signing.

Let’s see a simple example using Python:

def simple_hash(data):
  hash_value = 0
  for char in data:
    hash_value += ord(char)  # Convert each character to its ASCII code and sum them
  return hash_value

# Example : 
data = "password"
hash = simple_hash(data)
print(f"Original data: {data}")
print(f"Hashed value: {hash}")
Enter fullscreen mode Exit fullscreen mode

This code defines a very basic hashing function (not recommended for real-world use) that simply sums the ASCII codes of each character in the data. It demonstrates the core concept of transforming data into a unique value. In reality, secure hashing algorithms use complex mathematical operations to create more robust and collision-resistant hashes.

So, the next time you log in to a website or download a file, remember the invisible power of hashing algorithms working behind the scenes to keep your data safe and secure!

Thanks for reading! If you’re interested in learning more about computer science, feel free to check out my social media links in my bio. There, I share more tech-related content and insights.

Top comments (0)