I simply want to create a 32 byte hash of a string and compare equality to another one, but this is proving almost impossible for some reason. E.g:

hash("a") == hash("b")

My compiler throws errors if I try to use keccak256: TypeError: Invalid type for argument in function call. Invalid implicit conversion from string memory to bytes memory requested. This function requires a single bytes argument as this only seems to be able to take bytes memory. Hashes created by abi.encode are not comparable with == and are also not of a fixed length it seems so this also doesn't seem to be a solution.

I am new to programming in Solidity, so any help would be great; I am using solidity 0.5.0!

1 Answer 1


Try this:

pragma solidity 0.5.1;

contract Hasher {

    function compare(string memory a, string memory b) public pure returns(bool) {
        return keccak256(abi.encodePacked(a)) == keccak256(abi.encodePacked(b));

Hope it helps.

  • Can we do directly keccak256(a) == keccak256(b) ? @Rob Hitchens B9lab
    – alper
    Commented Feb 21, 2019 at 9:34

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.