typestar

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

  1. abi.encodePacked serializes the arguments tightly.
  2. keccak256 hashes the bytes into a bytes32 id.
  3. Recomputing the hash verifies without storing the inputs.

Keywords and builtins used here

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.

Type this snippet

Step 2 of 3 in The environment, step 22 of 28 in Language basics.

← Previous Next →