DevToolsForYou

Hash Generator in Rust — Code Examples

Hash Generator in RustUse the online tool →

Cryptographic hash functions convert input data into a fixed-size digest. SHA-256 is the current standard for security-sensitive use cases; MD5 is broken for security but still used for checksums.

Rust uses the sha2 crate for SHA-2, the md5 crate for MD5, and hmac for HMAC. The hex crate converts raw bytes to a hex string.

Rust
// Cargo.toml:
// sha2 = "0.10"
// md5  = "0.10"
// hmac = "0.12"
// hex  = "0.4"
use sha2::{Sha256, Sha512, Digest};

fn main() {
    let input = b"Hello, world!";

    // SHA-256
    let sha256 = hex::encode(Sha256::digest(input));
    println!("{}", sha256);
    // 315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3

    // SHA-512
    let sha512 = hex::encode(Sha512::digest(input));
    println!("{}", sha512);

    // MD5 (not for security use)
    let md5_hash = md5::compute(input);
    println!("{:x}", md5_hash); // 6cd3556deb0da54bca060b4c39479839

    // HMAC-SHA256
    use hmac::{Hmac, Mac};
    type HmacSha256 = Hmac<Sha256>;
    let mut mac = HmacSha256::new_from_slice(b"secret-key").unwrap();
    mac.update(input);
    let result = mac.finalize().into_bytes();
    println!("{}", hex::encode(result));
}
Notes & gotchas
  • Sha256::digest is a one-shot method; use the update/finalize pattern for streaming large data.
  • Never use MD5 for passwords — use argon2 or bcrypt crates instead.
  • The RustCrypto project (sha2, hmac, etc.) uses a common Digest trait, making it easy to swap algorithms.
Try it in your browser

Need to hash md5/sha without writing code? The Hash Generator runs entirely in your browser — paste your input and get the result instantly. No signup, no install, no data sent to a server.

Open Hash MD5/SHA
Hash Generator in other languages