Back to glossary

Hash Function

Table of Contents

What is a hash function?

A hash function is a mathematical algorithm that takes input data of any size, like a file or a block of transactions, and transforms it into a fixed-size output data called a hash

Blockchains use hash functions to secure transactions and validate blocks. The result of applying a hash function to a piece of data serves as a fingerprint for that data, providing an easy way to detect whether the data has been altered. 

How do hash functions work?

A hash function operates through a series of operations that transform an input message into a hash value. 

  1. Input processing: The hash function accepts an input, referred to as a message, which can be of any size or format.
  2. Data segmentation: The function segments this input into smaller parts.
  3. Mathematical operations: The function applies a series of algorithmic transformations to each segment.
  4. Output: After performing these operations on each segment, the results are combined to produce a fixed-length hash value.

Illustration of the process a hash function executes.

For example, when a hash function processes "hello world," it runs an algorithm on the letters and spaces and outputs a fixed-length string. Demo it here!

Hash functions show the avalanche effect, the phenomenon where even a slight change in the input data results in a different output. This ensures that small alterations to the data are visible, which significantly enhances the security of the resulting hash.

Since hash functions are deterministic (the same input will always generate the same hash), they are ideal for tracking and verifying data.

Types of hash functions

  • Keccak-256 is a cryptographic hash function that produces a fixed-length 256-bit output that is integral to blockchain consensus mechanisms. For instance, when validators validate a block in Ethereum, they use Keccak-256 to generate a hash of the block header, ensuring the entire block’s integrity. For example, this function computes the Keccak-256 hash of a given string, number, and address, and returns the resulting 256-bit hash:
function hash(string memory _text, uint256 _num, address _addr)
        public
        pure
        returns (bytes32)
    {
        return keccak256(abi.encodePacked(_text, _num, _addr));
    }

    • The SHA (Secure Hash Algorithm) is a family of cryptographic hash functions. SHA outputs are collision-resistant, secure, and irreversible. They are commonly used in blockchain and security protocols. The most famous use of SHA-256 is Bitcoin's proof-of-work mechanism. Here is an example of a simple SHA-256 hash function that computes the SHA-256 hash of a given string input and returns the resulting 256-bit hash. NOTE: Ethereum’s Keccak-256 is often mistakenly referred to as SHA-3, but they are different algorithms.
    function computeSHA256(string memory input) public pure returns (bytes32) {
         return sha256(abi.encodePacked(input));
    }

    • Checksum functions: A checksum is a simpler form of hash function used to verify data integrity during transmission. They are not as secure as Keccak-256 or SHA-256 and are generally used in non-cryptographic settings, like file downloads. They ensure the data received matches the data sent, making them effective for quick verification in non-blockchain environments. Here is a simple example of a Rust code snippet computes the CRC32 checksum of the byte string “foo bar baz” using the crc32fast crate..
    use crc32fast::Hasher;
    
    let mut hasher = Hasher::new();
    hasher.update(b"foo bar baz");
    let checksum = hasher.finalize();

    What are hash functions used for in blockchains?

    Hash functions play a crucial role in blockchain technology, providing several key functionalities that ensure the integrity, security, and efficiency of the system. Primary uses include:

    1. Data integrity: Hash functions ensure that data stored on the blockchain has not been altered. Each block contains a hash of the previous block to create a chain of blocks that is tamper-evident. Any change in data would result in a completely different hash, making alterations easy to detect.
    2. Transaction verification: Hash functions are used to verify blockchain transactions. 
    3. Mining: Hash functions are integral to the mining process in proof-of-work blockchains like Bitcoin. Miners compete to solve complex mathematical puzzles, which involve finding a hash that meets certain criteria.
    4. Merkle Trees: Hash functions are used to construct Merkle trees, data structures that efficiently and securely verify the integrity of large sets of data.
    5. Account creation: In Ethereum a public key is derived from a private key using an algorithm called ECDSA. This public key is then hashed using Keccak-256 with the last 20 bytes of the hash forming the Ethereum address.

    Tools for hash functions

    Several tools make it easier to work with hash functions, allowing users and developers to generate, verify, and explore hash values across different contexts.

    1. Online hash generators: Online tools can generate hashes from any input. These tools support different algorithms like Keccak-256, SHA-256, and MD5. 
    2. Command line tools:some text
      1. Cast is a Foundry CLI tool used by security researchers and blockchain developers for interacting with Ethereum RPC endpoints, debugging, and analyzing smart contracts. Cast offers commands for many purposes, including hashing, encoding, and generating wallet addresses. some text
        1. For example: cast keccak “Foundry is amazing!” computes the Keccak-256 hash of the given input.
        2. Developers can see a full list of commands by running cast -h or cast --help in the terminal.
      2. OpenSSL is a cryptographic library that generates hashes right from the terminal. For instance, you can use openssl dgst -sha256 filename to hash a file using SHA-256.
    Image of the Etherscan displaying the output of a hash.
    1. Blockchain explorers: These tools allow users to view and verify smart contract activities on various blockchains. They decode blockchain transaction hashes to display information in easy to read format. 
    2. Decoding tools: Decoders can break down a cryptographic hash (like a transaction hash) to reveal details like sender, receiver, and gas fees. This "decoding" process doesn't reverse the hash but interprets the information linked to it. Here is an example using Ethereum Transaction Decoder:
      • Tx Hash: 0x70775e6cb2953edef96843fd12363971a47c54c2c52cca51e9e97b5264c6368d
    Image displaying the output of a hash from an Ethereum transaction decoder.

    The decoded transaction hash reveals:

    • the block hash (0xdb7ee8077c1fb3f5c2e977fdc47e8db56d1289b6e03fef3cf52c484d6e033c9d) and block number (0x13d9742) identify the block containing the transaction.
    • The from address (0x4838b106fce9647bdf1e7877bf73ce8b0bad5f97) and to address (0x388c818ca8b9251b393131c08a736a67ccb19297) indicate the sender and recipient.
    • The transaction hash (0x70775e6cb2953edef96843fd12363971a47c54c2c52cca51e9e97b5264c6368d) uniquely identifies this transaction.
    • The gas (0x565f), gas price (0x703a36c2c), max fee per gas (0x703a36c2c), and max priority fee per gas (0x0) provide details on the transaction fees.
    • The value (0x1d10cb66ffe9205) represents the amount of cryptocurrency transferred.
    • Additional details include the nonce (0xae743), transaction index (0xa6), chain ID (0x1), and cryptographic signatures (v, r, s, and yParity), which ensure the transaction’s authenticity and integrity.

    Related Terms

    No items found.