How to hash in sway

how can I hash various inputs to get a unique output in B256 value, similar to keccak 256 in solidity

You can use the hash library in sway-lib

  use std::{
      hash::Hasher,
  };

        let mut latest_random_seed = input_random_seed;

        let mut hasher = Hasher::new();

        latest_random_seed.hash(hasher);
        latest_random_seed = decompose(hasher.keccak256()).0;
1 Like

Hey @div-lync, you can certainly use the sway-libs to hash and get value in B256 format,

use std::hash::keccak256;

contract;

impl MyContract {
    pub fn hash_string(input: str[32]) -> b256 {
        keccak256(input)
    }

    pub fn hash_bytes(input: [u8; 32]) -> b256 {
        keccak256(input)
    }

    pub fn hash_multiple_inputs(input1: str[32], input2: [u8; 32]) -> b256 {
        let concatenated = input1 + input2;
        keccak256(concatenated)
    }
}