4

Here is a basic hash test. I am trying to reproduce the output with: http://emn178.github.io/online-tools/sha3_256.html

Is sha3-256 correct?

The output for '1' as the value input is b10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6 but I cannot reproduce this with the calculator.

contract HashTest {

bytes32 public hashOutput;
uint public valueOut;

function hash(uint _values) {
    var value = _values; 
    valueOut = value;
    hashOutput = sha3(value);
}

function refund() {
   msg.sender.send(this.balance);
}
}

Also I am using "var" because that is how it was provided in the blind auction example and once I can get the hash to match I will create a more complex function using var if possible.

5

In Solidity the sha3() method will use the variable type (as opposed to variable content) to determine the size of it.

uint maps to uint256 and that means in your example not the number 0x01 will be hashed, but 0x0000000000000000000000000000000000000000000000000000000000000001 is used as input. Use uint8 for an 8 bit variable (0x01).

Also as a reminder, Ethereum uses Keccak and not the final SHA3 (Why aren't Solidity sha3 hashes not matching what other sha3 libraries produce?)

Edit: sample code using ethereumjs-abi:

var ABI = require('ethereumjs-abi');
var abi = new ABI();
var BN = require('bn.js')

console.log(abi.soliditySHA3(
    [ 'uint' ],
    [ new BN('01', 16) ]
).toString('hex'));
  • That is helpful. Now keccak-256 with input: 0000000000000000000000000000000000000000000000000000000000000001 Produces: 20ec45d096f1fa2aeff1e3da8a84697d90109524958ed4be9f6d69e37a9140a4 – JTicker12 Mar 11 '16 at 18:28
  • Added a sample code, which produces correct results. – axic Mar 11 '16 at 18:33
  • Seems I should be able to put the 63 0's and '1' into the linked calculator under with sha3-256 or at least keccak-256 and come up with the above hash Oxb10e2.... no? – JTicker12 Mar 11 '16 at 18:50
  • @JTicker12 most online calculators won't handle hex input, and treat all input as strings. If you give 01 to online Keccak-256 calculator, the matching result in Solidity would be to pass the string "01" as in sha3("01"). – eth Mar 11 '16 at 19:35
  • @eth yes this is what I needed to know. Then for testing purposes I will use strings for everything. keccak-256 btw! – JTicker12 Mar 11 '16 at 19:51

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.