I am trying to create a hash (sha256) of an array but I don't understand how it works.
for example :
pragma solidity 0.4.11;
contract test { bytes32 public hash; bytes32 public hash2; bytes32 public hash3; bytes32 public hash4;
function test () {
address[2] memory _add;
_add[0] = 0xca35b7d915458ef540ade6068dfe2f44e8fa733c;
_add[1] = 0xca35b7d915458ef540ade6068dfe2f44e8fa733c;
hash = sha256(_add[0],_add[1]);
hash2 = sha256(_add);
_add[0] = 0;
_add[1] = 0;
hash3 = sha256(_add[0],_add[1]);
hash4 = sha256(_add);
}
hash is different from hash2 and hash3 is different from hash4... why?
The "tightly packed" data should result in the same hash ... no?
Best,
"Tightly packed" no longer applies when dealing with an array. You must zerofill to 32 bytes.
Unfortunately, I couldn't find the docs to validate that, so all I can do is give an example to show it works.
Side Note: Are you sure you want
sha256instead ofkeccak256, which is used most other places in Ethereum?Demo
I'll demonstrate with
sha3(the same askeccak256), but the concept is the same.Baseline
In Remix, you'll find that:
gives the result:
0x77e5189111eb6557e8a637b27ef8fbb15bc61d61c2f00cc48878f3a296e5e0caRebuild, with Web3.py
For what it's worth, you can also get the result with: