14

In Solidity, I find that the sha256 function can be used like this a = sha256(uint b, string c, bool d, ......) (the arguments can be a combination of different types, and the other types can also be included.)

What does sha256 do with the arguments of different types?

1 Answer 1

11

Solidity docs say that sha256 (and sha3, ripemd160) arguments are tightly packed:

“tightly packed” means that the arguments are concatenated without padding. This means that the following are all identical:

sha3("ab", "c")

sha3("abc")

sha3(0x616263)

sha3(6382179)

sha3(97, 98, 99)

Using the question's example of a = sha256(uint b, string c, bool d),

  • if we assign some values like b is 31415, c is "abc", and d is false,

  • then a would equal sha256(31415, 6382179, 0), which is same as sha256(0x7ab761626300).


For completeness, the docs mention that:

If padding is needed, explicit type conversions can be used: sha3(“x00x12”) is the same as sha3(uint16(0x12)).

4
  • What would the following line return : uint var = sha3("abc","pqr",121); Nov 15, 2016 at 4:59
  • @RahulSharma browser-solidity tells me 0xec7bee56c53a0d4df8f2fb29d262d59c29a5896470ad3788c7a89cd70eac82ad
    – eth
    Nov 15, 2016 at 8:38
  • Is it the output of sha3 function? Nov 15, 2016 at 8:40
  • Yes gist.github.com/anonymous/9e144d1bd0def71a1f80053404fe4896 (I forgot to use constant but browser-solidity is still able to show the result.)
    – eth
    Nov 15, 2016 at 8:43

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.