See Hash Functions in Action: A Practical Example of SHA-256 and MD5 Hashing

James MathengeJames Mathenge
3 min read

Hash functions like SHA-256 and MD5 are extensively used in real-world applications. In this article, we will showcase the hashing process through hands-on examples. Read on to gain a practical understanding of cryptographic hash functions.

Introduction

In previous articles, we learned the theory behind hash functions and their importance in computing. Now let's see some widely used hash functions in action!

We will be demonstrating SHA-256 and MD5 - two popular cryptographic hash functions employed in systems worldwide. By hashing sample inputs, we will verify core properties like deterministic outputs and the avalanche effect.

Gaining firsthand experience with hashing allows a deeper insight into how these ubiquitously relied-upon algorithms operate. So let's get started!

SHA-256 Hashing Demonstration

The SHA-2 or Secure Hash Algorithm 2 family contains several hash functions like SHA-256. SHA-256 is one of the most commonly used cryptographic hash functions today.

Let's take the input "Hello World" and hash it using an online SHA-256 generator.

Input: "Hello World"

SHA-256 Output: 
b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9

The generator returned a 256-bit (64-character) hexadecimal hash value. Even a small change in the input text produces an entirely different hash:

Input: "Hello World!" 

SHA-256 Output:
c0535e4be2b79ffd93291305436bf889314e4a3faec05ecffcbb7df31ad9e51a

This demonstrates SHA-256's deterministic nature and avalanche effect wherein a minor input change cascades into a completely different hash value.

MD5 Hashing Example

The MD5 or Message-Digest Algorithm 5 hash function was designed in 1991. Although not deemed fully secure today, it remains widely adopted.

Let's hash the input "Hello World" using an online MD5 generator:

Input: "Hello World" 

MD5 output: 
b10a8db164e0754105b7a99be72e3fe5

We get a 128-bit hexadecimal hash value. As expected, changing the input to "Hello World!" produces a wholly distinct hash:

Input: "Hello World!"

MD5 output: 
f9a0cf9f4ce1d2ed1e5ed5e3d4d19949

Once again, we verified that different inputs yield different MD5 hash values.

Hashing a File

Now, let's hash a sample text file instead of a string.

I created a file article.txt containing this article text. Hashing it with SHA-256 returns:

SHA-256("article.txt") = 
7dfaa1bc6108d7a6b584abd1421334851247fca8dd5c0c75bad7d0d9c6a3f5e7

If I now modify the text by adding a space somewhere, the SHA-256 hash becomes:

SHA-256("modified article.txt") =
03d2911b4f0468cd29e1f4928204378dcf548118f5a77ed65a53fc4dda2bd4fd

This verifies that the avalanche effect holds even for files - a tiny change results in an entirely different hash value.

Conclusion

Through practical examples, we observed how SHA-256 and MD5 operate on sample inputs. We verified key properties like deterministic outputs and sensitivity to input changes that make hash functions indispensable in many real-world applications.

Hashing is a crucial concept that safeguards data security and integrity in systems we interact with every day. Gaining firsthand experience solidifies intuition about hash functions. In upcoming articles, we'll explore even more implementations, uses, and inner workings of hash functions.

10
Subscribe to my newsletter

Read articles from James Mathenge directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

James Mathenge
James Mathenge

A highly skilled professional dev who possesses a unique combination of expertise in both Finance and Software Development