sha_crypt/
params.rs

1//! Algorithm parameters.
2
3use crate::errors;
4use core::default::Default;
5
6/// Default number of rounds.
7pub const ROUNDS_DEFAULT: usize = 5_000;
8
9/// Minimum number of rounds allowed.
10pub const ROUNDS_MIN: usize = 1_000;
11
12/// Maximum number of rounds allowed.
13pub const ROUNDS_MAX: usize = 999_999_999;
14
15/// Algorithm parameters.
16#[derive(Debug, Clone)]
17pub struct Sha512Params {
18    pub(crate) rounds: usize,
19}
20
21impl Default for Sha512Params {
22    fn default() -> Self {
23        Sha512Params {
24            rounds: ROUNDS_DEFAULT,
25        }
26    }
27}
28
29impl Sha512Params {
30    /// Create new algorithm parameters.
31    pub fn new(rounds: usize) -> Result<Sha512Params, errors::CryptError> {
32        if (ROUNDS_MIN..=ROUNDS_MAX).contains(&rounds) {
33            Ok(Sha512Params { rounds })
34        } else {
35            Err(errors::CryptError::RoundsError)
36        }
37    }
38}
39
40/// Algorithm parameters.
41#[derive(Debug, Clone)]
42pub struct Sha256Params {
43    pub(crate) rounds: usize,
44}
45
46impl Default for Sha256Params {
47    fn default() -> Self {
48        Sha256Params {
49            rounds: ROUNDS_DEFAULT,
50        }
51    }
52}
53
54impl Sha256Params {
55    /// Create new algorithm parameters.
56    pub fn new(rounds: usize) -> Result<Sha256Params, errors::CryptError> {
57        if (ROUNDS_MIN..=ROUNDS_MAX).contains(&rounds) {
58            Ok(Sha256Params { rounds })
59        } else {
60            Err(errors::CryptError::RoundsError)
61        }
62    }
63}