Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Blockchain by (4.1k points)

I'm trying to implement a signature in a Solidity contract and I'm having problems when it comes to comparing hashes. I calculate the hash with the following code in solidity:

sha256(abi.encodePacked(param1, ...., paramN);
 

Where:

abi.encodePacked(param1, ..., paramN) = [bytes: 0x0102030405060701]

and

sha255(abi.encodePacked(param1, ..., paramN)) = [bytes32:
 0x245138c905599c8579ab186fbdbd6e62396aac35a98a6568f8803eed049d1251]

The main problem I'm having is that by using python sha256 on 0102030405060701 the result I'm getting is 5bc31e3decf480124c79c114744d111ec82b62e466a097c3ced6fe76cbace9a5.

What am I doing wrong?

1 Answer

0 votes
by (14.4k points)

You should not hash the string "0102030405060701". What you actually need to do is hash the bytes which are represented by that hexadecimal value in your code.

You may use the code which is mentioned below:

>>> import hashlib

>>> import binascii

>>> hashlib.sha256(b'0102030405060701').hexdigest()

'5bc31e3decf480124c79c114744d111ec82b62e466a097c3ced6fe76cbace9a5'

>>> hashlib.sha256(binascii.unhexlify('0102030405060701')).hexdigest()

'245138c905599c8579ab186fbdbd6e62396aac35a98a6568f8803eed049d1251'

You should note that hashing the string directly won't fetch what you are seeking. You should convert the string to binary using unhexlify in Solidity.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...