xmpp-rs/sasl/src/secret.rs

71 lines
1.9 KiB
Rust
Raw Normal View History

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];
fn iterations(&self) -> usize;
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>,
pub iterations: usize,
pub digest: Vec<u8>,
}
2017-04-04 16:04:23 +00:00
impl Pbkdf2Sha1 {
#[cfg(feature = "scram")]
2017-04-04 16:04:23 +00:00
pub fn derive(password: &str, salt: &[u8], iterations: usize) -> Result<Pbkdf2Sha1, String> {
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 }
fn iterations(&self) -> usize { 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>,
pub iterations: usize,
pub digest: Vec<u8>,
}
2017-04-04 16:04:23 +00:00
impl Pbkdf2Sha256 {
#[cfg(feature = "scram")]
2017-04-04 16:04:23 +00:00
pub fn derive(password: &str, salt: &[u8], iterations: usize) -> Result<Pbkdf2Sha256, String> {
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 }
fn iterations(&self) -> usize { self.iterations }
fn digest(&self) -> &[u8] { &self.digest }
}