Skip to main content

wae_authentication/password/
config.rs

1//! 密码哈希配置
2
3/// 密码哈希算法类型
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum PasswordHashAlgorithm {
6    /// bcrypt 算法
7    Bcrypt,
8}
9
10/// 密码哈希配置
11#[derive(Debug, Clone)]
12pub struct PasswordHashConfig {
13    /// 使用的哈希算法
14    pub algorithm: PasswordHashAlgorithm,
15    /// bcrypt 成本因子(4-31,推荐 12)
16    pub bcrypt_cost: u32,
17}
18
19impl Default for PasswordHashConfig {
20    fn default() -> Self {
21        Self { algorithm: PasswordHashAlgorithm::Bcrypt, bcrypt_cost: 12 }
22    }
23}
24
25impl PasswordHashConfig {
26    /// 创建新的密码哈希配置
27    ///
28    /// # Arguments
29    /// * `algorithm` - 使用的哈希算法
30    pub fn new(algorithm: PasswordHashAlgorithm) -> Self {
31        Self { algorithm, ..Default::default() }
32    }
33
34    /// 设置 bcrypt 成本因子
35    ///
36    /// # Arguments
37    /// * `cost` - 成本因子(4-31)
38    pub fn with_bcrypt_cost(mut self, cost: u32) -> Self {
39        self.bcrypt_cost = cost.clamp(4, 31);
40        self
41    }
42}