4

I try to use sha3/keccak functions inside a Solidity contract, but I get a weird error:

Untitled:5:13: Error: Undeclared identifier.
bytes32 d=keccak256(0x616263);

The contract :

pragma solidity ^0.4.0;

contract C {

    bytes32 d=keccak256(0x616263);

    bytes32 a=sha3(uint256(1));
}
  • What version of the Solidity compiler are you using? – eth Oct 24 '16 at 18:52
  • 0.4.3 nightly (in solidity browser) – Sig Touri Oct 24 '16 at 19:31
3

the keccak256() is an alias for the sha3 function to avoid any confusion with the sha-3 standard (The opcode is still called SHA3.) this alias was adopted this month so the old compiler don't recognize it. so use sha3 instead.

The available hashing functions are sha3, sha256, ripemd160:

pragma solidity ^0.4.0;

contract C { 

    function hashingsha3 (string s)   returns  (bytes32 hash){
        return sha3(s);//<=========
    }

    function hashingsha256 (string s)   returns  (bytes32 hash){
        return sha256(s); //<=========
    }

    function ripemd160 (string s)   returns  (bytes20 hash){
        return ripemd160(s); //<=========
    }
}
3

For browser-solidity, keccak256 is fixed in version 0.4.4 and later.

keccak256 is recommended to use, it's identical to sha3, and is less confusing because Ethereum does not use the final SHA-3 standard, but the original winning algorithm named Keccak. More contracts using keccak256 will prevent confusion such as Why aren't Solidity sha3 hashes not matching what other sha3 libraries produce?

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.