Getting Started with Solidity|Lecture 28: Hash

I’ve recently been re-learning solidity, consolidating the details, and writing a “Solidity Minimalist Primer” for beginners to use (programming guys can find another tutorial), updated 1-3 times a week.

All code and tutorials are open source at github:github.com/AmazingAng/WTFSolidity

A hash function is a cryptographic concept that converts a message of arbitrary length into a fixed-length value, also known as a hash. In this talk, let’s briefly introduce the hash function and its application in solidity

Nature of Hash

A good hash function should have the following characteristics:

Unidirectionality: The forward operation from the input message to its hash is simple and uniquely determined, while the reverse is very difficult and can only be enumerated by brute force.

Sensitivity: The input message changes a little bit to its hash a lot.

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

Homogeneity: The probability of each hash being taken should be roughly the same.

Crash Resistance: Weak Crash Resistance: Given a message x, finding another message x’ makes hash(x) = hash(x’) difficult.

Strong Crash Resistance: Finding arbitrary x and x’ makes hash(x) = hash(x’) difficult.

Hash app

Generate a unique identifier for data

Cryptographic signatures

Secure encryption

Keccak256

The Keccak256 function is the most commonly used hash function in solidity, and it’s very simple to use:

Generate a unique identifier for data

We can use keccak256 to generate some unique identifiers for the data. For example, if we have several different types of data: uint, string, address, we can use the abi.encodePacked method to encode them, and then use keccak256 to generate a unique identity:

Weak crash resistance

Let’s use keccak256 to demonstrate the weak crash resistance we talked about earlier, i.e., given a message x, finding another message x’ makes it difficult to hash(x) = hash(x’).

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

You can try 10 times and see if you can get lucky and collide.

Strong crash resistance

Let’s use keccak256 to demonstrate the strong crash resistance mentioned earlier, i.e., finding arbitrarily different x and x’, making hash(x) = hash(x’) difficult.

We construct a function strong, take two different string arguments, string1 and string2, and determine if their hashes are the same:

You can try 10 times and see if you can get lucky and collide.

Summary

In this talk, we introduced what a hash function is and how to use Keccak256, the most commonly used hash function in Solidity.

View Original
This page may contain third-party content, which is provided for information purposes only (not representations/warranties) and should not be considered as an endorsement of its views by Gate, nor as financial or professional advice. See Disclaimer for details.
  • Reward
  • Comment
  • Repost
  • Share
Comment
0/400
No comments
  • Pin
Trade Crypto Anywhere Anytime
qrCode
Scan to download Gate App
Community
  • 简体中文
  • English
  • Tiếng Việt
  • 繁體中文
  • Español
  • Русский
  • Français (Afrique)
  • Português (Portugal)
  • Bahasa Indonesia
  • 日本語
  • بالعربية
  • Українська
  • Português (Brasil)