0

If I calculate the following in Solidity 0.6.3:

bytes memory secretSalt = "abc";
hashedSecret = sha256(abi.encodePacked(secretSalt))

I get the result "0xba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" which matches what I get on two online SHA256 generators when I input "abc" (though, without the quotes, see here and here).

But if I try

uint secretNumber = 5;
hashedSecret = sha256(abi.encodePacked(secretNumber));

The result is "0x96de8fc8c256fa1e1556d41af431cace7dca68707c78dd88c3acab8b17164c47" which does not match those online SHA256 generators. I tried various hex representations of 5 on the online SHA256 generators and still cannot get "0x96de..."

Any thoughts on how Solidity is transforming the number "uint 5", relative to these common SHA256 implementations?

0

The type uint is 32 bytes in length, so uint(5) is stored as 0000000000000000000000000000000000000000000000000000000000000005 in hexadecimal.

In order to test this input you need a calculator that accepts hexadecimal input, many only accept text strings.

| improve this answer | |
  • It's strange that those two online Sha256 generators handle the "abc" input correctly while I can't get them to work for unit(5) under various translations. Meanwhile, I went to liavaag.org/English/SHA-Generator, and it works (it has a toggle for text or hex input). – Eric Falkenstein Apr 25 at 23:44
  • @EricFalkenstein It works because "abc" is declared as string, so abi.encode returns their ascii representation (in hexadecimal "616263"). If you use a type of fixed length like uintNNN or bytesNN abi.encode will fill missing with 0 bytes. – Ismael Apr 26 at 1:37

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.