From 853183330ef822caa997fb1a6e73aab4e3e6acc9 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Sun, 11 Jan 2026 17:57:50 -0700 Subject: [PATCH] sha-crypt: add `ShaCrypt::SHA256` and `ShaCrypt::SHA512` Associated constants which are each configured using the default recommended `Parameters` but the respective SHA-family algorithm --- sha-crypt/src/mcf.rs | 8 +++++++- sha-crypt/src/params.rs | 13 ++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/sha-crypt/src/mcf.rs b/sha-crypt/src/mcf.rs index 889499ec..13fcfdd6 100644 --- a/sha-crypt/src/mcf.rs +++ b/sha-crypt/src/mcf.rs @@ -23,8 +23,14 @@ pub struct ShaCrypt { } impl ShaCrypt { + /// SHA-crypt configured with SHA-256 as the default. + pub const SHA256: Self = Self::new(Algorithm::Sha256Crypt, Params::RECOMMENDED); + + /// SHA-crypt configured with SHA-512 as the default. + pub const SHA512: Self = Self::new(Algorithm::Sha512Crypt, Params::RECOMMENDED); + /// Create a new password hasher with customized algorithm and params. - pub fn new(algorithm: Algorithm, params: Params) -> Self { + pub const fn new(algorithm: Algorithm, params: Params) -> Self { Self { algorithm, params } } } diff --git a/sha-crypt/src/params.rs b/sha-crypt/src/params.rs index 5aab6483..57d4d3f0 100644 --- a/sha-crypt/src/params.rs +++ b/sha-crypt/src/params.rs @@ -18,8 +18,13 @@ pub struct Params { } impl Params { - /// Default number of rounds. - pub const ROUNDS_DEFAULT: u32 = 5_000; + /// Recommended parameters. + pub const RECOMMENDED: Self = Self { + rounds: Self::RECOMMENDED_ROUNDS, + }; + + /// Recommended number of rounds. + pub const RECOMMENDED_ROUNDS: u32 = 5_000; /// Minimum number of rounds allowed. pub const ROUNDS_MIN: u32 = 1_000; @@ -38,9 +43,7 @@ impl Params { impl Default for Params { fn default() -> Self { - Params { - rounds: Self::ROUNDS_DEFAULT, - } + Params::RECOMMENDED } }