A good hashing algorithm is a function that will take any sized input and reduce it down to a nearly unique fixed-sized output.
Hashing has its uses outside of cryptography, most notably for data storage in hash tables (see hash function). For our purposes, we're going to be focused on hashing algorithms that are suitable for cryptography. 🔑
Let's take a look at some of the properties of cryptographic hashing algorithms.
Given the same input, they produce the same output
For example, no matter how many times I pass
Hello Worldinto the SHA256 hashing algorithm I will always get back
Given a slightly different input, a completely different output is returned
In the example above, we used
Hello World. If we were to change this slightly, the hash produced would look completely different!
sha256("Hello World"); // a591a6…9f146e sha256("hello World"); // db4067…4e7d7e sha256("Hello World"); // 60ab93…cf11b3 sha256("Hello World!"); // 7f83b1…6d9069
👆🏼 Whether we added an additional space, exclamation mark, or even changed capitalization, the output will always be completely different.
The returned results here are shown in hexadecimal format. It's a pretty common standard for displaying data in cryptography for convenience reasons: every two characters represents a byte and it is more compact than a long string of 1s and 0s with binary.
They should be one-way
Starting with an output like
ffd85b0d22019d89837d841224eb51166dbdb43f8f4fbd18baf96c78cd721d4cyou would have no way of finding out what input creates this output. The only thing you could do is guess, which is referred to as a brute-force attack. Good luck! That might take you a while.
Collisions should be extremely rare
It should nearly impractical to find two inputs that map to the same output. And once again, the only way to do so should be through guessing.
These properties make for some really awesome cryptographic solutions. We'll frequently refer to hashes when discussing blockchain. Cryptographic hashes are super cool! 😎
It's fun to try out SHA256. Try typing "SHA256 online" or "SHA256 generator" into Google to find a quick tool that will take messages and return the SHA256 hash. You should be able to come up with the hashes shown in the examples above and create your own!
Alchemy University offers free web3 development bootcamps that explain hashing and help developers master the fundamentals of web3 technology. Sign up for free, and start building today!
Updated 3 months ago