1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//! Algorithm parameters.

use crate::errors;
use core::default::Default;

pub const ROUNDS_DEFAULT: usize = 5_000;
pub const ROUNDS_MIN: usize = 1_000;
pub const ROUNDS_MAX: usize = 999_999_999;

#[derive(Debug, Clone)]
pub struct Sha512Params {
    pub(crate) rounds: usize,
}

impl Default for Sha512Params {
    fn default() -> Self {
        Sha512Params {
            rounds: ROUNDS_DEFAULT,
        }
    }
}

impl Sha512Params {
    pub fn new(rounds: usize) -> Result<Sha512Params, errors::CryptError> {
        if rounds < ROUNDS_MIN || rounds > ROUNDS_MAX {
            return Err(errors::CryptError::RoundsError);
        }
        Ok(Sha512Params { rounds })
    }
}