sasl/
secret.rs

1#[cfg(feature = "scram")]
2use crate::common::scram::DeriveError;
3use alloc::borrow::ToOwned;
4use alloc::string::String;
5use alloc::vec::Vec;
6
7pub trait Secret {}
8
9pub trait Pbkdf2Secret {
10    fn salt(&self) -> &[u8];
11    fn iterations(&self) -> u32;
12    fn digest(&self) -> &[u8];
13}
14
15#[derive(Clone, Debug, PartialEq, Eq)]
16pub struct Plain(pub String);
17
18impl Secret for Plain {}
19
20#[derive(Clone, Debug, PartialEq, Eq)]
21pub struct Pbkdf2Sha1 {
22    pub salt: Vec<u8>,
23    pub iterations: u32,
24    pub digest: Vec<u8>,
25}
26
27impl Pbkdf2Sha1 {
28    #[cfg(feature = "scram")]
29    pub fn derive(password: &str, salt: &[u8], iterations: u32) -> Result<Pbkdf2Sha1, DeriveError> {
30        use crate::common::scram::{ScramProvider, Sha1};
31        use crate::common::Password;
32        let digest = Sha1::derive(&Password::Plain(password.to_owned()), salt, iterations)?;
33        Ok(Pbkdf2Sha1 {
34            salt: salt.to_vec(),
35            iterations,
36            digest,
37        })
38    }
39}
40
41impl Secret for Pbkdf2Sha1 {}
42
43impl Pbkdf2Secret for Pbkdf2Sha1 {
44    fn salt(&self) -> &[u8] {
45        &self.salt
46    }
47    fn iterations(&self) -> u32 {
48        self.iterations
49    }
50    fn digest(&self) -> &[u8] {
51        &self.digest
52    }
53}
54
55#[derive(Clone, Debug, PartialEq, Eq)]
56pub struct Pbkdf2Sha256 {
57    pub salt: Vec<u8>,
58    pub iterations: u32,
59    pub digest: Vec<u8>,
60}
61
62impl Pbkdf2Sha256 {
63    #[cfg(feature = "scram")]
64    pub fn derive(
65        password: &str,
66        salt: &[u8],
67        iterations: u32,
68    ) -> Result<Pbkdf2Sha256, DeriveError> {
69        use crate::common::scram::{ScramProvider, Sha256};
70        use crate::common::Password;
71        let digest = Sha256::derive(&Password::Plain(password.to_owned()), salt, iterations)?;
72        Ok(Pbkdf2Sha256 {
73            salt: salt.to_vec(),
74            iterations,
75            digest,
76        })
77    }
78}
79
80impl Secret for Pbkdf2Sha256 {}
81
82impl Pbkdf2Secret for Pbkdf2Sha256 {
83    fn salt(&self) -> &[u8] {
84        &self.salt
85    }
86    fn iterations(&self) -> u32 {
87        self.iterations
88    }
89    fn digest(&self) -> &[u8] {
90        &self.digest
91    }
92}