Skip to main content

Module crypto

Module crypto 

Source
Available on crate feature crypto only.
Expand description

Password hashing and secure token generation.

Enabled by the crypto Cargo feature:

rust-web-server = { version = "17", features = ["crypto"] }

§Password hashing

Uses Argon2id (OWASP recommended) with a random 128-bit salt. The output is a self-describing PHC string that includes the algorithm, parameters, salt, and hash — store it as-is in your database.

use rust_web_server::crypto::{hash_password, verify_password};

let hash = hash_password("hunter2").unwrap();
assert!(verify_password("hunter2", &hash).unwrap());
assert!(!verify_password("wrong", &hash).unwrap());

§Secure token generation

use rust_web_server::crypto::generate_token;

let token = generate_token(32); // 32 random bytes → 64-char lowercase hex string

Structs§

CryptoError
Error returned by crypto operations.

Functions§

generate_token
Generate n_bytes of cryptographically secure random bytes and return them as a lowercase hex string of length n_bytes * 2.
hash_password
Hash password using Argon2id with a random salt.
verify_password
Verify password against a PHC hash string produced by hash_password.