1

In Solidity, I run the following code:

function hashTest() public returns(bytes32,bytes32,bytes32) {
bytes32 _solution = 0xbc756c25d68ea2f260ea5f15e1e1c734c019cbc014270dd386eacca4699f60f6;
bytes32 v = keccak256(abi.encodePacked(_solution));
bytes32 x = sha256(abi.encodePacked(_solution));
bytes32 z = ripemd160(abi.encodePacked(_solution));
Print(v,x,z);
return(v,x,z);

I want to replicate in python, but so far I can only get the first (the one supported by Web3), does anyone know how to get the others? Here's my current try:

def testHash():
    _solutions = '0xbc756c25d68ea2f260ea5f15e1e1c734c019cbc014270dd386eacca4699f60f6';
    v = Web3.toHex(Web3.sha3(hexstr=_solutions))
    x = "0x" + hashlib.new('sha256',str.encode(_solutions)).hexdigest()
    z= hashlib.new('ripemd160',str.encode(_solutions)).hexdigest()
    print("v",v)
    print("x",x);
    print("z",z);

So like I said, the top matches, but the other two don't

1 Answer 1

1

The issue here is that you are treating the hex string as a string. You should convert the string to bytes using bytes.fromhex(). Note that you should remove the 0x from the string. Like this:

_solutions = '0xbc756c25d68ea2f260ea5f15e1e1c734c019cbc014270dd386eacca4699f60f6';
v = Web3.toHex(Web3.sha3(hexstr=_solutions))
x = "0x" + hashlib.new('sha256',bytes.fromhex(_solutions[2:])).hexdigest()
z= hashlib.new('ripemd160',bytes.fromhex(_solutions[2:])).hexdigest()

Hope this helps.

1
  • You're a valiant shining knight. Thanks for helping out, this works great
    – thefett
    Nov 28, 2018 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.