How to Hash Data in Rust (SHA-256, Blake3, etc.)

Hash data in Rust using the sha2 crate for SHA-256 or blake3 crate for Blake3 by initializing a hasher, updating it with data, and finalizing the result.

Use the sha2 crate for SHA-256 or the blake3 crate for Blake3 by adding them to your dependencies and calling their digest methods.

use sha2::{Sha256, Digest};

fn main() {
    let mut hasher = Sha256::new();
    hasher.update(b"hello world");
    let result = hasher.finalize();
    println!("{:x}", result);
}

Add sha2 = "0.10" to your Cargo.toml dependencies.