xmpp-rs-mirror/sasl/src/secret.rs

90 lines
2 KiB
Rust
Raw Normal View History

#[cfg(feature = "scram")]
use crate::common::scram::DeriveError;
2017-03-25 22:45:30 +00:00
pub trait Secret {}
2017-03-25 22:45:30 +00:00
pub trait Pbkdf2Secret {
fn salt(&self) -> &[u8];
2020-06-21 23:19:24 +00:00
fn iterations(&self) -> u32;
fn digest(&self) -> &[u8];
}
2017-03-25 22:45:30 +00:00
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Plain(pub String);
2017-03-25 22:45:30 +00:00
impl Secret for Plain {}
2017-03-25 22:45:30 +00:00
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Pbkdf2Sha1 {
pub salt: Vec<u8>,
2020-06-21 23:19:24 +00:00
pub iterations: u32,
pub digest: Vec<u8>,
}
2017-04-04 16:04:23 +00:00
impl Pbkdf2Sha1 {
#[cfg(feature = "scram")]
2020-06-21 23:19:24 +00:00
pub fn derive(password: &str, salt: &[u8], iterations: u32) -> Result<Pbkdf2Sha1, DeriveError> {
2019-01-17 21:54:32 +00:00
use crate::common::scram::{ScramProvider, Sha1};
use crate::common::Password;
2017-04-04 16:04:23 +00:00
let digest = Sha1::derive(&Password::Plain(password.to_owned()), salt, iterations)?;
Ok(Pbkdf2Sha1 {
salt: salt.to_vec(),
iterations: iterations,
digest: digest,
})
}
}
2017-03-25 22:45:30 +00:00
impl Secret for Pbkdf2Sha1 {}
2017-03-25 22:45:30 +00:00
impl Pbkdf2Secret for Pbkdf2Sha1 {
fn salt(&self) -> &[u8] {
&self.salt
}
2020-06-21 23:19:24 +00:00
fn iterations(&self) -> u32 {
self.iterations
}
fn digest(&self) -> &[u8] {
&self.digest
}
}
2017-03-25 22:45:30 +00:00
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Pbkdf2Sha256 {
pub salt: Vec<u8>,
2020-06-21 23:19:24 +00:00
pub iterations: u32,
pub digest: Vec<u8>,
}
2017-04-04 16:04:23 +00:00
impl Pbkdf2Sha256 {
#[cfg(feature = "scram")]
pub fn derive(
password: &str,
salt: &[u8],
2020-06-21 23:19:24 +00:00
iterations: u32,
) -> Result<Pbkdf2Sha256, DeriveError> {
2019-01-17 21:54:32 +00:00
use crate::common::scram::{ScramProvider, Sha256};
use crate::common::Password;
2017-04-04 16:04:23 +00:00
let digest = Sha256::derive(&Password::Plain(password.to_owned()), salt, iterations)?;
Ok(Pbkdf2Sha256 {
salt: salt.to_vec(),
iterations: iterations,
digest: digest,
})
}
}
2017-03-25 22:45:30 +00:00
impl Secret for Pbkdf2Sha256 {}
2017-03-25 22:45:30 +00:00
impl Pbkdf2Secret for Pbkdf2Sha256 {
fn salt(&self) -> &[u8] {
&self.salt
}
2020-06-21 23:19:24 +00:00
fn iterations(&self) -> u32 {
self.iterations
}
fn digest(&self) -> &[u8] {
&self.digest
}
}