pub fn hash(password: Password, rounds: Option<u32>) -> StringExpand description
Hash a password with a freshly generated, cryptographically secure random salt.
This is the recommended high-level API for storing new passwords.
§Arguments
password— Password to hash. Its buffer is zeroed before return.rounds— Optional iteration count:Noneuses the SHA-crypt default of 5000 rounds and omits therounds=segment from the output.Some(n)recordsrounds=n$in the output, withnclamped into[1000, 999_999_999]per the SHA-crypt specification. Note thatSome(5000)also omits therounds=segment, so its output is bytewise identical toNone.
§Returns
A complete hash string of the form $6$[rounds=N$]salt$hash.
§Panics
Panics (or aborts) if the active backend’s CSPRNG fails. All four backends treat CSPRNG failure as fatal rather than returning a predictable salt:
backend-aws-lc/backend-boring: the FFIRAND_bytesaborts the process when entropy is unavailable.backend-openssl: this crate asserts theRAND_bytesreturn code.backend-rust-crypto: this crateexpects thegetrandomcall.
§Security
- Salt is drawn from the active backend’s CSPRNG.
- Password and intermediate hash buffers are wiped via the backend’s non-elidable zeroing primitive before this function returns.
§Examples
use crypt_sha512::{hash, verify, Password};
let h = hash(Password::from("hunter2"), None);
assert_eq!(verify(Password::from("hunter2"), &h), Ok(true));
// Higher work factor for sensitive deployments
let h = hash(Password::from("hunter2"), Some(100_000));
assert_eq!(verify(Password::from("hunter2"), &h), Ok(true));