Introduction to Hash
The hash function is the most commonly used and basic algorithm in cryptography. It can generate a globally unique identifier for any data.
The output of a hash function is often called a Hash Value or Digest or Checksum.
Effect
The hash function can ensure that the data we obtain is complete and authentic.
For example, create an empty hello_world.txt file and encrypt the file using the sha256
algorithm. If we want to make this file public for people to download, but due to network insecurity or other reasons, the file is incomplete or accidentally tampered with during downloading, then in the end the file will not be authentic and correct.
Normally, the website where the file is downloaded will provide the URL of the downloaded file and attach a summary of the current file. When the download is successful, we use the same hashing algorithm to generate the summary to hash the file. If the output hash value and The document summary provided by the website is the same, so proving that this document is true and complete is the target document we need.
[joe@mx crypto]$ touch hello_world.txt
[joe@mx crypto]$ openssl dgst -sha256 hello_world.txt
SHA2-256(hello_world.txt)= e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
Modify the content of the file and hash it again, and the output digest will be different.
[joe@mx crypto]$ echo "rooch" >> hello_world.txt
[joe@mx crypto]$ openssl dgst -sha256 hello_world.txt
SHA2-256(hello_world.txt)= f590572de3c69705a0cad30159ab9f728d28f118d108f7a9b5d025282dd34d30
A hash function can accept an input of any length and produce a fixed-length output.
The hash values output above are all expressed in Base16 or hexadecimal form, so every two digits represent a hexadecimal number. There are 64 digits above, which means that the digest size is 32 bytes.
*-256
means that the output of this hash algorithm is 256 bits, that is, 32 bytes. By analogy, the output of a hash algorithm ending with *-512
is fixed at 64 bytes.
Security properties
There are three main properties that make up a hash function.
-
Pre-image Resistance means that the corresponding input cannot be restored based on the output of the hash function, provided that the input space is large enough and unpredictable.
-
Second Pre-image Resistance, that is, given an input and its hash value, it is impossible to find a new input different from the input, so that the two inputs produce the same hash value.
For example, in the above example, no matter who creates the empty hello_world.txt file, the digest of the same hash function will be the same. Once the input is slightly modified, the digest must be different.
- Collision Resistance, this property ensures that two different inputs with the same hash value cannot be found. This document mainly introduces some hash libraries and methods used by Rooch.
Public and private keys
The private key is a set of randomly generated numbers in the range 2^256
, which means that the input space is large enough that it is almost impossible to generate the same private key.
The public key can be obtained by calculating the private key, but the private key cannot be calculated from the public key.
The address usually corresponds to our account. The address is obtained by intercepting a part or doing some splicing. It is usually impossible to calculate the corresponding public key from the address.
These one-way hash functions provide the most basic and important guarantee for the implementation of blockchain.
Some hash functions provided by Rooch
- sha2_256
- sha3_256
- blake2b256
- keccak256
- base58
- base58check
- bech32
- ecdsa_k1
- ed25519
For more details, please read the source code (opens in a new tab)