5

I would like to know how to get a bytes32 keccak256 hash of an address and 2 uint256 variables.

I want something that will look like this. But from the remix IDE errors, I need to make the the 3 variables into bytes32 and then hash that?

/* here are the variable names and types (Apart from msg.sender which is address */

uint256 _unixTimestamp;
uint256 _timeExpired;

/* This is what I have got so far, what do I need to do to fix the error? */
bytes32 output = keccak256(msg.sender, _unixTimestamp, _timeExpired);

Here is the warning message from the remix IDE

Warning: This function only accepts a single "bytes" argument. Please use "abi.encodePacked(...)" or a similar function to encode the data.

bytes32 output = keccak256(msg.sender, _unixTimestamp, _timeExpired);

. . . . . . . . . . . . . .^----------------------------------------------------------------------- ^

3
  • you have uint265 instead of uint256 other than that it should work
    – qbsp
    Jun 6, 2018 at 7:06
  • I fixed that, and now just got warning... "Warning: This function only accepts a single "bytes" argument. Please use "abi.encodePacked(...)" or a similar function to encode the data. bytes32 output = keccak256(msg.sender, _unixTimestamp, _timeExpired);" Jun 6, 2018 at 7:08
  • 4
    just use the function as proposed by the warning: keccak256(abi.encodePacked(msg.sender, _unixTimestamp, _timeExpired))
    – qbsp
    Jun 6, 2018 at 7:11

1 Answer 1

2

As reported in this thread: https://github.com/ethereum/solidity/issues/3955

The new abi.encode*() methods offer a replacement to perform packing, which then can be passed to all these hashing functions.

Bottom line: As @mirg commented, just use abi.encodePacked() inside keccak256(). That is:

keccak256(abi.encodePacked(msg.sender, _unixTimestamp, _timeExpired))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.