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);

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

BlockchainBoy
  • 345
  • 1
  • 4
  • 10
  • you have uint265 instead of uint256 other than that it should work – qbsp Jun 06 '18 at 07: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);" – BlockchainBoy Jun 06 '18 at 07:08
  • 4
    just use the function as proposed by the warning: keccak256(abi.encodePacked(msg.sender, _unixTimestamp, _timeExpired)) – qbsp Jun 06 '18 at 07:11

1 Answers1

3

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))
Paul Razvan Berg
  • 17,902
  • 6
  • 73
  • 143