header-langage
简体中文
English
Tiếng Việt
Scan to Download the APP
Partnered With:

Simple Introduction to Solidity|Lecture 28: Hash

Read this article in 6 Minutes
This article describes what a hash function is and how to use keccak256, the most commonly used hash function in solidity.
Original title: " A minimalist introduction to Solidity: 28. Hash 》
 Author of the original text: 0xAA 


I am re-learning solidity recently to consolidate Let’s talk about the details, and write a "Simple Introduction to Solidity" for beginners (programmers can find another tutorial), and update 1-3 lectures every week.


All codes and tutorials are open source on github: github.com/AmazingAng/WTFSolidity


Ha A hash function is a cryptographic concept that converts a message of any length into a fixed-length value, also known as a hash. In this lecture, we briefly introduce the hash function and its application in solidity


The nature of Hash


A good hash function should have the following characteristics:


One-way: forward operation from the input message to its hash It is simple and uniquely determined, but in turn is very difficult and can only be enumerated by violence.

Sensitivity: A small change in the input message has a large change in its hash.

Efficiency: The operation from the input message to the hash is efficient.

Uniformity: The probability of each hash value being taken should be basically equal.

Collision resistance: Weak collision resistance: Given a message x, it is difficult to find another message x' such that hash(x) = hash(x').

Strong collision resistance: finding any x and x' such that hash(x) = hash(x') is hard.


Application of Hash


Generate data unique identifier

encrypted signature

Security encryption 


Keccak256


Keccak256 function is solidity The most commonly used hash function in , the usage is very simple:



Generate data unique identifier


We can use keccak256 to generate some data Uniquely identifies. For example, we have several different types of data: uint, string, address, we can first use the abi.encodePacked method to pack and encode them, and then use keccak256 to generate a unique identifier:



weak collision resistance


We use keccak256 to demonstrate the weak collision resistance mentioned earlier, that is, given a message x, find another message x' such that hash(x) = hash(x') is hard.


Given a message 0xAA, we try to find another message so that their hash values are equal:


< /p>


You can try 10 times to see if you can be lucky on the collision.


Strong anti-collision


We use keccak256 to demonstrate the strong anti-collision mentioned earlier property, that is, it is difficult to find any distinct x and x' such that hash(x) = hash(x').


We construct a function strong, receive two different string parameters string1 and string2, and then judge whether their hashes are the same:



You can try 10 times and see Can you be lucky enough to collide?


Summary


In this lecture, we introduced what is a hash function and how Use solidity's most commonly used hash function keccak256.


Original link


Welcome to join the official BlockBeats community:

Telegram Subscription Group: https://t.me/theblockbeats

Telegram Discussion Group: https://t.me/BlockBeats_App

Official Twitter Account: https://twitter.com/BlockBeatsAsia

举报 Correction/Report
Choose Library
Add Library
Cancel
Finish
Add Library
Visible to myself only
Public
Save
Correction/Report
Submit