keccak256 in Solidity
The fingerprint of anything, via abi.encodePacked.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
// keccak256 over abi.encodePacked: the fingerprint of anything.
contract SeedVault {
mapping(bytes32 => bool) public deposited;
function depositProof(string calldata variety, uint256 lot)
external returns (bytes32 id)
{
id = keccak256(abi.encodePacked(variety, lot));
deposited[id] = true;
}
function verify(string calldata variety, uint256 lot)
external view returns (bool)
{
return deposited[keccak256(abi.encodePacked(variety, lot))];
}
}
How it works
abi.encodePackedserializes the arguments tightly.keccak256hashes the bytes into abytes32id.- Recomputing the hash verifies without storing the inputs.
Keywords and builtins used here
boolbytes32contractexternalfunctionkeccak256mappingpublicreturnreturnsstringtrueuint256
The run, in numbers
- Lines
- 20
- Characters to type
- 502
- Tokens
- 93
- Three-star pace
- 55 tpm
At the three-star pace of 55 tokens a minute, this run takes about 101 seconds.
Step 2 of 3 in The environment, step 22 of 28 in Language basics.