Understanding the Two Hash Functions of Ethereum: on_hash160 vs on_sha256
The Ethereum Blockchain Uses Two Hash Functions to Store and Check the Transactions, Each Its Own Strengths and Weaknesses. In this article, we will break down the differentences between the on_hash160 and the on_sha256, explaining when to use each other and in what situations.
What are the hash functions?
Hash Functions Take Input Data Of Any Size And Produce A Fixed Size String (OR Hash) That Repressents This Data Uniquely. In the context of Ethereum, Both on_hash160 and on_sha256 are used to authenticate messages and data integrity.
on_hash160:
* Version: Introduction to bip 0012
* Description: hash function based on sha-256
* Key Features:
+ Uses a 20 -bit input block (128 bits)
+ Signed with Private Key Using ECDSA (Digital Signature Algorithm
+ More Resistant to Collisions and Prerequisit Attacks Due to its fixed length
* Why use on_hash160?
- HIGER Security: on_hash160 is Safer than on_sha256, which makes a better choice for sensitive transactions.
- Easier to Implement: Because on_hash160 uses sha-256, which has been widely adopted and understood, the implementation of the on_hash160 is relatively simpler.
- Better Resistance to Collisions:
fixed 20 Bytes length sacrifices Stronger Protection Against Attacks Trying to Change the Entry Data.
on_sha256:
* Version: Introduction to BIP 0013
* Description: hash function Based on Sha-256 (Just like ecdsa)
* Key Features:
+ Uses an Entrance block with variable length or up to 32 bytes
+ Signed with Private Key Using ECDSA
+ Less Resistant to Collisions and Prereability Attacks Due to its Dynamic Length
* Why use on_sha256?
- Simplified Implementation: Because on_sha256 uses SHA-256, which is well established, implementation can be Easier than on_hash160.
- Better Resistance to Prereability Attacks: The Variable Length Entry Block Provides Stronger Protection Against Attacks That Try to Change the Entry Data.
When to use on_hash160:
- Sensitive transactions: When you have to do with sensitive information, such as personal data or financial transactions, the superior security of the on_hash160 is a better choice.
- Longer input Blocks: If you have to store Longer Input Blocks (for Example, Larger Data Structures), the fixed length of the on_hash160 sacrifices More Collision Protection.
When to use on_sha256:
- Variable Length Entries: When Dealing with Variable Length Entry Blocks, Such as Images or Other Media, Op_Sha256 is a better choice.
- Existing infrastructure: If you alreaty use the existing infrastructure based on ecdsa (for example, wallets, libraries), the implementation of on_sha256 can be simpler.
Example or scripting unlocking:
Here are some examples of bip 199 scripts that use both on_hash160 and on_sha256 to authenticate messages:
On_hash160:
`C.
Unlock contract (
Bytes32 _input,
Address _privatekey,
Bytes _signature
) {
// Check Signature Using ECDSA (ECDSA is Used Here)
Request ecdsaverify (_signature, _privatekey, _input);
}
Function _ecdsaverify (Bytes memory _signature, _privatekey, bytes32 _input) Public {
// ... (ECDSA Verification Logic)
// Check the Entrance Using the SA-256
Requires Sha256 (_input). equals (_signature);
}
On_sha256:
C.
Unlock contract (
Bytes32 _input,
Address _privatekey,
Bytes _signature
) {
// Check Signature Using ECDSA (ECDSA is Used Here)
Request ecdsaverify (_signature, _privatekey, _input);
// Check the Entrance Using the SA-256
Requires Sha256 (_input). equals (_signature);
}
`
In Conclusion, While Both on_hash160 and on_sha256 Offer Unique Strengths and Weaknesses, The Choice Between Them Depends on your specific use.