Do you understand the principles of SHA-256, MD-5, and hash functions?

Do you understand the principles of SHA-256, MD-5, and hash functions?

Editor’s note: This article comes from Blockchain Base Camp (ID: blockchain_camp), author: wagslane, translator: Huohuojiang, and is reprinted with permission from Odaily Planet Daily.

This article provides a brief introduction to hash functions, aiming to help readers understand why hash functions are used and their basic working principles. The specific proofs and implementation details will be omitted, and the focus will be on the high-level principles.

Why use hash functions?

Hash functions are widely used in various aspects of the Internet, mainly for secure storage of passwords, finding backup records, fast storage and retrieval of data, etc. For example, Qvault uses hashing to expand the master password into a private encryption key.

(Qvault: https://qvault.io/)

For a list of uses, see: https://en.wikipedia.or/

This article will focus on several important properties of hash functions, which can also be said to be its most important properties:

  • Hash functions deterministically scramble data;

  • The output size of a hash function is always the same, no matter what the input is;

  • It is impossible to retrieve the original data from the scrambled data (one-way function);

Deterministically scramble data

First, imagine a Rubik's Cube.

We start by restoring the Rubik's Cube. If we move the Rubik's Cube randomly, at the end, the Rubik's Cube will be in a completely different state than when we started. Likewise, if we start over and repeat the exact same moves, we will keep getting the exact same result. Although it may seem that the results are random, they are not. This is what "determinism" means.

"Determinism" plays a vital role in storing passwords securely. For example, let's say my password is "iLoveBitcoin".

I can scramble it using a hash function:

iLoveBitcoin→ “2f5sfsdfs5s1fsfsdf98ss4f84sfs6d5fs2d1fdf15”

Now, if someone sees this scrambled version, they won't know my original password! This is extremely important because it means that, as a website developer, I only have to store the hash (scrambled data) of a user's password to verify it.

When a user registers, I hash the password and store it in the database. When the user logs in, I simply hash the input again and compare the two hashes. Since a specific input always outputs the same hash, this method successfully verifies the password every time.

If a website stores passwords in plain text, it presents a huge security hole. If someone hacks into the website, they will have access to all of your emails and passwords and can try to use that information to log in on other websites.

No matter what the input is, the output size is always the same

If you hash a single word, the output will be a certain size (256 bits for a specific hash function, SHA-256). If you hash a book, the output will be the same size.

This is another important feature because it can save us computation time. A typical example is using hashes as keys in data maps. Data maps are simple structures used in computer science to store data.

When a program stores data in a map, it provides a key and a value to the map. When the program wants to access the value, it can provide the appropriate key to the map and receive the corresponding value. The advantage of data maps is that they can find data instantly. The key is used as an address that the computer can find immediately, so it doesn't have to spend hours searching through millions of records.

Because keys, like addresses, can't be too large. If I wanted to store books in a data map, I could hash the contents of the book and use the hash as the key. As a programmer, I can easily use the hash to find the contents of the book without having to sort through thousands of records by title, author, etc.

How does it work?

This is the hard part of this article, I will try to keep it simple, leaving out the actual implementation details and focusing on the basic concept of how computers work when they process data using hashing.

Let's take a look at an algorithm I wrote specifically for this purpose - LANEHASH:

We start with the data we want to hash

I converted letters and numbers into 1 and 0 (all data in the computer is stored in the form of 1 and 0, and different combinations of 1 and 0 represent different letters)

At this point, we transform the data through various pre-set steps. The steps can be arbitrary, but the important thing is that we need to follow the same steps every time we use LANEHASH so that our algorithm is deterministic. We shift the first 4 bits from the left to the right:

Interval every 1 bit:

We convert these two parts to base 10 numbers. Base 10 is the "normal" number system we all learned in school. (All binary data is actually numbers, and you can look up how to convert binary to decimal numbers online on other websites)

We multiply these two numbers:

Then square this number:

Convert the number back to binary:

After cutting off 9 bits from the right side, we get exactly 16 bits:

Then convert that binary data back to English:

As shown above, if the input is the same, then the final output will be the same. However, if any one letter is changed, the final result will also change drastically.

<<:  Ebang International’s “obsession”: Will Ebang be the next “Canaan”?

>>:  Where will Bitcoin mining go after the halving? This article gives you a comprehensive analysis

Recommend

Facial features of a kind-hearted person

Facial features of a kind-hearted person When we ...

What are the characteristics of an introvert?

God created different people and gave us differen...

4 types of ruthless and heartless faces, please stay away from them

As the saying goes, "You can know a person&#...

Palmistry characteristics of a good wife and mother

Palmistry characteristics of a good wife and moth...

Ethereum 2.0 is expected to be released on December 1

According to a blog post from the Ethereum Founda...

What are the characteristics of rich people's faces?

What are the characteristics of rich people's...

How to judge health, wisdom, personality and morality from the eyes

1. The eyes are clearly divided into black and wh...

Xiao Yueyue, the representative of “grassroots counterattack”

Recently, a group of videos suspected to be showi...

Judging from your appearance, you are a winner in life

Judging from your appearance, you are a winner in...

Physiognomy: A General Discussion of the Three Stops and Six Palaces

What are the three stops and six palaces in physi...

What is the wisdom line in palmistry?

What is Wisdom Line? Wisdom line (reason line): I...

Analysis of the facial features of women with round and flat noses

As one of the traditional physiognomy techniques, ...