[Ethereum] SOLIDITY: How to get a bytes32 keccak256 hash of an address and 2 uint256 variables

hashkeccaksolidity

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

. . . . . . . . . . . . . .^———————————————————————– ^

Best Answer

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))
Related Topic