Jan 11, 2024

How to hash in RUST?

Steps

  • Setup your Rust environment
  • Add sha3 to your Cargo.toml
  • Compute sha3
  • Complete code

I. Setup your RUST environment

cargo new hash_example
cd hash_example

II. Add crates: sha3 as a dependency

[dependencies]
sha3 = "0.10"

III. sha3_hash.rs


use sha3::Sha3_256;
use digest::Digest;

pub fn compute_sha3(input: &str) -> String {
let mut hasher = Sha3_256::new();
hasher.update(input.as_bytes());
let result = hasher.finalize();
format!("{:x}", result)
}

IV. main.rs

mod sha3_hash;

fn main() {
let input = "hello world";

let sha3_result = sha3_hash::compute_sha3(input);
println!("SHA3-256 hash of '{}': {}", input, sha3_result);

}

You can find complete code at:

https://github.com/Nepalichhoro/Hashing_Rust


Thanks for reading! If you want to see future content, you can follow me on Twitter or get connected over at LinkedIn.


Support My Content

If you find my content helpful, consider supporting a humanitarian cause (building homes for elderly people in rural Terai region of Nepal) that I am planning with your donation:

Ethereum (ETH)

0xb0b1B20062DA9Dd7BaA4D5b088DF49dbe4b46fF2

Thank you for your support!