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: