0

How can I concat two different datatypes and then hash it? for example: sha3(int8 + address)

Or it is also good for me hash them separately, and then hash together. like this: sha3(sha3(int8) + address)

How to do that most efficient way?

  • I have hash of my int8 sha3(num) And I can produce the same thing on front-end: Web3.utils.sha3(Web3.utils.toHex(40)) Now I am wondering what is the most efficient and hash secure way to hash them both (my hashed int8 and hashed or not hashed address). I can see I can concat both bytes32 two one string using this: ethereum.stackexchange.com/questions/1081/… and then hash it to produce my bytes32. Can I do this somehow more efficient? – gkucmierz Nov 25 '17 at 15:27
  • Or I think, I can do this easier like xoring both data, and then hash them. But I am afraid that this can be not secure enough. something like: sha3(sha3(int8) ^ address) What do you think? – gkucmierz Nov 25 '17 at 15:35
1

To concatenate the values, cast them into bytes, create an output bytes variable and then assign each byte

int8 _num;
address _addr;
bytes b0 = bytes(_num);
bytes b1 = bytes(_addr);
string s0 = new string(b0.length + b1.length);
bytes b2 = bytes(s0);
uint k = 0;
for (uint i = 0; i < b0.length(); i++) { b2[k++] = b0[i] }
for (i = 0; i < b1.length(); i++) {b2[k++] = b1[i] }

Then you can just hash the concatenated byte array. If you hashed these separately and then concatenated the two hashes it would probably be much less efficient (computationally) since you'd still have to write some code for concatenating them and they are much longer than the two variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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