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.
Hashing turns any amount of data into a fixed-size string of characters that acts like a unique fingerprint. You use this to verify that files haven't been tampered with or to store passwords securely without saving the actual text. Think of it like a digital seal that breaks if the contents change.