Медиа Центр

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?

  • Higher security: op_hash160 is safer than OP_SHA256, which makes it a better choice for sensitive transactions.

  • to implement more easily: Da op_hash160 SHA-256, which has been widespread and understood, the implementation of OP_Hash160 is relatively easier.

  • Better resistance to collisions: The fixed length of 20 bytes offers greater protection against attacks that try to change the input data.

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?

  • Simplified implementation: Da op_sha256 SHA-256, which is well established, implementation can be easier than OP_Hash160.

  • Better resistance to preparatory attacks: The input block with variable length offers greater protection against attacks that try to change the input data.

When uses op_hash160:

  • Sensitive transactions: When treating sensitive information such as personal data or financial transactions, the higher security of op_hash160 is a better choice.

  • Longer input blocks: If you have to save longer input blocks (e.g. larger data structures), the fixed length of op_hash160 offers more protection against collisions.

When uses op_sha256:

  • Inputs Variable length: When handling input blocks with variable length such as pictures or other media, op_sha256 is a better choice.

  • Existing infrastructure: If you use existing infrastructure that is based on ECDSA (e.g. item pockets, libraries), the implementation of op_sha256 can be easier.

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.