0

I have tried using many hashing algorithms but there output in solidity doesn't match with the one given in online converters.

Here is my program:-
pragma solidity ^0.4.18;  
contract C {  
bytes32 public temp;  
function hashingsha3 (uint s)   {  
temp= sha3(s);  
}

    function hashingsha256 (uint s) {
        temp= sha256(s); 
    }
     function kec (uint s)   returns  (bytes32 hash){
        temp= keccak256(s); 
    }

}

this is the online site to convert

I have read solidity doesn't use the current keccak256 approved by NIST.
But then what does it use that matches any online output?
Any help would be useful.

List of outputs :
Solidity
sha3(12)
0xdf6966c971051c3d54ec59162606531493a51404a002842f56009d7e5cf4a8c7

sha256(12)
0xa82872b96246dac512ddf0515f5da862a92ecebebcb92537b6e3e73199694c45

keccak256(12) 0xdf6966c971051c3d54ec59162606531493a51404a002842f56009d7e5cf4a8c7

online converter
SHA256(12) 6b51d431df5d7f141cbececcf79edf3dd861c3b4069f0b11661a3eefacbba918

SHA512/256 (12) d0d0b58b8cad7a27781cb78b3212d8abce1a4eb4192882e703f7f2e1ea5158da

SHA3-256(12) 1a9a118cb653759c3fcb3bd5060e6f9910c8c27008dd11fe4315f4635c9caa98

SHA3-512 (12) f235c129089233ce3c9c85f1d1554b9cb21952b27e0765bcbcf75d550dd4d2874e546889da5c44db9c066e05e268f4742d672889ff62fb9cb18a3d1b57f00658

Keccak-256 (12) 7f8b6b088b6d74c2852fc86c796dca07b44eed6fb3daf5e6b59f7c364db14528

Thankyou

EDITED:

pragma solidity ^0.4.18;  
contract C {  
bytes32 public temp;  
function hashingsha3 (bytes s)   {  
temp= sha3(s);  
}  
function hashingsha256 (bytes s) {  
temp= sha256(s);  
}  
function kec (bytes s)  {  
temp= keccak256(s);  
}  
}

I changed the code : but still the outputs now for this are: Passed as a string in solidity: sha256(12) 0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855

sha3(12) 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470

keccak256(12) 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470

enter image description here

enter image description here

  • It's unclear what you are asking. – Vignesh Karthikeyan Jun 11 '18 at 19:53
  • I am asking why are the hashed outputs of solidity and online converter different.What should I use if I wish to confirm it with online converters. – Neha Jain Jun 12 '18 at 4:31
2

The issue you're having is that in your Solidity code, you're hashing numbers, while the online converter you're using is hashing strings.

If you do keccak256("12") instead of keccak256(12), you'll get a hash matching the one on the website.

  • Thanks for your help. But it is still not working. – Neha Jain Jun 12 '18 at 4:35
  • What do you mean "not working?" What's your current code, what result are you getting, and what are you getting from the website you found? – smarx Jun 12 '18 at 4:48
  • I edited the question in which I posted the current code and the results. – Neha Jain Jun 12 '18 at 4:54
  • It's unclear how you're calling the functions, but my guess is you're still passing numbers instead of strings. – smarx Jun 12 '18 at 4:55
  • Make sure you have quotes around 12, like this: "12". – smarx Jun 12 '18 at 4:56
2

Completely agree with Mr. Marx. I'm just adding another answer as a commentary for consideration.

Your question turns attention to a hidden assumption. That is, that it's currently possible and convenient to find a suitable library for any given client, and always will be.

In my opinion, it would be a good idea to expose read-only function to assist clients and to provide a guarantee that they will always be able to compute the hash by the same method as today's compiled contract. Something simple ...

function hashHelper(uint value) public pure returns(bytes32 hash) {
  return keccak256(value);
}

The idea would be to use the same function internally, thus avoiding possible future divergence between the compiled bytecode and clients of all shapes and sizes.

Hope it helps.

  • I din't really understand what do you mean.Are you saying I should just have returned instead of declaring temp public ? – Neha Jain Jun 12 '18 at 4:33
  • It might be more clear if/when you write the client-side to this, that is, work out the library and syntax to use to successfully compute a hash the same way the contract does. The method suggested will greatly simplify that as well as help future-proof it. – Rob Hitchens - B9lab Jun 12 '18 at 14:34
  • amazing answer, thanks, Mr. Hitchens! – Arri May 29 at 21:00

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.