sha256 - How an array is "tightly packed" in Solidity -
i trying create hash (sha256) of array don't understand how 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 different hash2 , hash3 different hash4... why?
the "tightly packed" data should result in same hash ... no?
best,
"tightly packed" no longer applies when dealing array. must zerofill 32 bytes.
unfortunately, couldn't find docs validate that, can give example show works.
side note: sure want sha256
instead of keccak256
, used other places in ethereum?
demo
i'll demonstrate sha3
(the same keccak256
), concept same.
baseline
in remix, you'll find that:
sha3([0xca35b7d915458ef540ade6068dfe2f44e8fa733c, 0xca35b7d915458ef540ade6068dfe2f44e8fa733c]);
gives result: 0x77e5189111eb6557e8a637b27ef8fbb15bc61d61c2f00cc48878f3a296e5e0ca
rebuild, web3.py
from web3 import web3, ipcprovider web3 = web3(ipcprovider()) addr = 'ca35b7d915458ef540ade6068dfe2f44e8fa733c' zerofilled = addr.zfill(64) # 64 hex chars == 32 bytes two_packed = zerofilled * 2 sha = web3.sha3('0x' + two_packed, encoding='hex') assert sha == '0x77e5189111eb6557e8a637b27ef8fbb15bc61d61c2f00cc48878f3a296e5e0ca'
for it's worth, can result with:
web3.soliditysha3( ['address[2]'], [[ '0xca35b7d915458ef540ade6068dfe2f44e8fa733c', '0xca35b7d915458ef540ade6068dfe2f44e8fa733c', ]], )
Comments
Post a Comment