Ethereums Understand two hash functions: op_hash160 against op_sha256
The Ethereum blockchain uses two hash functions to save and check transactions, each strengths and weaknesses. In this article we will break down the differences between op_hash160 and op_sha256 to explain when to use one one on top of each other and in which situations.
What are hash functions?
HASH functions absorb input data in every size and create a string with a fixed size (or hash) that clearly shows this data. In the context of Ethereum, both OP_Hash160 and OP_SHA256 are used for news authentication and data integrity.
op_hash160:
* Version: introduced in GDP 0012
* Description:
hash function based on SHA-256
* key features:
+ Use a 20-byte input block (128 bit)
+ Signed with the private key using ECDSA (elliptical curve digital signatural gorithm)
+ Due to its fixed length, more resistant to collisions and preparatory attacks
* Why use op_hash160?
op_sha256:
* Version: introduced in GDP 0013
* Description: hash function based on SHA-256 (like ECDSA)
* key features:
+ Use an input block with variable length of up to 32 bytes
+ Signed with the private key with ECDSA
+ Less resistant to collisions and preparatory attacks due to its dynamic length
* Why use op_sha256?
When uses op_hash160:
When uses op_sha256:
Example unlock scripts:
Here are some examples of GDP 199 scripts that use both OP_Hash160 and OP_SHA256 for news authentication:
Op_hash160:
`C
Contract appearance (
bytes32 _inPut,
Address _Privatekey,
Bytes _Signature
) {
// Check signature with ECDSA (ECDSA is used here)
ECDSASASACE (_Signature, _Privatekey, _inPut) need;
}
Function _ecdsasAce (bytes memory _Signature, address _privatekey, bytes32 _inPut) Public {
// ... (ECDSA -check logic)
// Check input with SHA-256
require SHA256 (_inPut). Equals (_Signature);
}
Op_sha256:
`C
Contract appearance (
bytes32 _inPut,
Address _Privatekey,
Bytes _Signature
) {
// Check signature with ECDSA (ECDSA is used here)
ECDSASASACE (_Signature, _Privatekey, _inPut) need;
// Check input with SHA-256
require SHA256 (_inPut). Equals (_signature);
}
While both OP_Hash160 and OP_SHA256 offer unique strengths and weaknesses, the choice depends between your specific application.