Skip to main content

Hashing

A cryptographic hash function is a widely used cryptographic primitive that maps an arbitrary length input to a fixed length output, the hash value. The hash function is designed to be a one-way function, which means that it is infeasible to invert the function to find the input data from a given hash value, and to be collision resistant, which means that it is infeasible to find two different inputs that map to the same hash value.

The IOTA Move API supports the following cryptographic hash functions:

  • SHA2-256 as std::hash::sha2_256
  • SHA3-256 as std::hash::sha3_256
  • Keccak256 as iota::hash::keccak256
  • Blake2b-256 as iota::hash::blake2b256

Usage

The SHA2-256 and SHA3-256 hash functions are available in the Move Standard Library in the std::hash module. The following example shows how to use the SHA2-256 hash function in a smart contract:

module example::hashing_std {
use std::hash;

/// Object that holds the output hash value.
public struct Output has key, store {
id: UID,
value: vector<u8>
}

public fun hash_data(data: vector<u8>, recipient: address, ctx: &mut TxContext) {
let hashed = Output {
id: object::new(ctx),
value: hash::sha2_256(data),
};
// Transfer an output data object holding the hashed data to the recipient.
transfer::public_transfer(hashed, recipient)
}
}

The Keccak256 and Blake2b-256 hash functions are available through the iota::hash module in the IOTA Move Library. An example of how to use the Keccak256 hash function in a smart contract is shown below. Notice that here, the input to the hash function is given as a reference. This is the case for both Keccak256 and Blake2b-256.

module example::hashing_iota {
use iota::hash;

/// Object that holds the output hash value.
public struct Output has key, store {
id: UID,
value: vector<u8>
}

public fun hash_data(data: vector<u8>, recipient: address, ctx: &mut TxContext) {
let hashed = Output {
id: object::new(ctx),
value: hash::keccak256(&data),
};
// Transfer an output data object holding the hashed data to the recipient.
transfer::public_transfer(hashed, recipient)
}
}

Question 1/2

Which cryptographic hash functions are supported by the IOTA Move API?