Sunday, November 10, 2024
STEM

Creative and Fun Uses of Hashing (Part 1)

In this post, I’ll demonstrate a fun and creative use of hashing. Before I explain briefly what is hashing for those unfamiliar, let me just give you an overview of my original intent of this exercise. We know that hash functions will give us unique values for a given input (letters, numbers, binaries), so I wanted to take it a step further by generating a hash of an input, then draw an image based on the hash value that’s guaranteed to be also visually unique! Basically creating a irreversible signature, meaning, only one specific input in the can generate that unique signature. That’s a big claim, but that’s exactly what I share below that you can also run from this page as watch the signatures in real-time.

Now, to understand why I thought hash would be a suitable method for this fun exercise, it’s worth recapping the uses of Hash.

Hash functions are essential for security, efficiency, and data organization in various real-world applications. They are widely used in various real-world scenarios due to their versatility and efficiency. Here are some uses:

  1. Data Retrieval Efficiency: Hashing provides a faster method for retrieving data compared to other data structures like lists and arrays making it ideal for large datasets.
  2. Secure Password Storage: Hashing is crucial for securely storing passwords. When a user creates an account, their password is hashed and stored. Even if the hash is stolen, it cannot be easily reversed to reveal the original password.
  3. File Integrity Verification: Hashing allows easy comparison of files for equality. By computing hash values, you can quickly determine if two files are identical without opening them individually. This feature is useful for verifying files after transferring them to a new location.
  4. Cryptographic Applications: Hash functions play a vital role in cryptography. They are used for digital signatures, message authentication codes (MACs), and ensuring data integrity. For example, blockchain technology relies heavily on hash functions.
  5. Efficient Data Storage: Hash functions generate keys for storing data in hash tables. When retrieving data, the hash function helps locate the corresponding entry in the table, leading to efficient data retrieval.

There are many hash functions ranging from simple to advanced. The more advanced hash functions such as SHA256 is the most secure and is used in my scripts.

How reliable is SHA-256 hash, meaning how often would there be a possible collision?

SHA-256 generates a 256-bit hash, which means there are 2^256 possible hash values. This is an astronomically large number, much larger than the number of atoms in the observable universe!

To give you an idea of how unlikely a collision is, consider this: if you were to hash a billion (i.e., 10^9) unique inputs, the probability of a collision would be about 4.3 * 10^-60. This is so small that it’s practically zero. In fact, you’re far more likely to be hit by an asteroid.

In practical terms, SHA-256 is extremely reliable and the likelihood of a collision to be negligible. The probability of a collision, which is when two different inputs produce the same hash output, is extremely low. No SHA-256 collision has been found yet. However, due to the birthday paradox, the probability of a collision increases to 50% once you’ve hashed about 2^128 inputs. But even this number is so large that it’s not feasible to reach in practice.

How the script is designed

The script gets an input from the user. It can be a letter, number, word, a sentence, or a mix of letters and numbers. No matter the length of the input, the input is ran through a SHA256 algorithm that always generates a 64 character hexadecimal sequence (hash value).

Next, the code takes that hash value and passes it to my custom function that uses simple drawing on canvas to draw out the sequence in order. The angle and distance of each line or stroke is dependent of the hash value making it unique to the input originally entered by the user!

Let’s take a look at some examples

If you enter a word “Pangram” (without quotes), you’ll get this SHA256 value: 90b1b41f786f721cf66b5e1c920e25a544fcb1618100c50599a81c94080748fc

and the following visual signature:

If you enter a sentence “The quick brown fox jumps over the lazy dog” (without quotes), you’ll get this SHA256 value: d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592

and the following visual signature:

Enough about theory and overview, time to take it out for a spin! You can run it below (nothing to download), just press Run below on the widget’s titlebar to run the script. Enter your text input and it will generate a hash value, then it will continue to draw a unique visual “signature” of that hash value. Once the drawing has finished, press Stop on the widget’s titlebar. It’ll turn to Run again, and you can run it again to try out different inputs. For the exact same input, the drawn “signature” will be the same everytime! Hence, I call it a “signature” because it’s virtually impossible to get the same signature from different outputs in this code.

The console below the drawing in the widget will also show the actual hexadecimal hash value for each input that’s being drawn as a signature in real-time. Remember, the case matters because ‘a’ and ‘A’ have different ordinal values therefore they will generate different hash values and signatures. Try it out yourself 😉

In the next blog post, I’ll share another creative use of hashing that takes a city’s real-time weather information, its latitude, and then draws a unique fractal image based on that! This creates a unique “weather signature” that can only be replicated with the same weather parameters and location!

Thanks for reading!

I hope this was educational and interesting. You can get the full source code from my Patreon shop here (secure transaction).


Related:

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top
+