From 714ee28006799a0952d649085f7352652546e8ef Mon Sep 17 00:00:00 2001 From: lumi Date: Tue, 4 Apr 2017 18:04:23 +0200 Subject: [PATCH] add derive methods to Pbkdf2 secrets --- sasl/src/secret.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/sasl/src/secret.rs b/sasl/src/secret.rs index f2d9853..dc6c112 100644 --- a/sasl/src/secret.rs +++ b/sasl/src/secret.rs @@ -18,6 +18,20 @@ pub struct Pbkdf2Sha1 { pub digest: Vec, } +impl Pbkdf2Sha1 { + #[cfg(feature = "openssl")] + pub fn derive(password: &str, salt: &[u8], iterations: usize) -> Result { + use common::scram::{ScramProvider, Sha1}; + use common::Password; + let digest = Sha1::derive(&Password::Plain(password.to_owned()), salt, iterations)?; + Ok(Pbkdf2Sha1 { + salt: salt.to_vec(), + iterations: iterations, + digest: digest, + }) + } +} + impl Secret for Pbkdf2Sha1 {} impl Pbkdf2Secret for Pbkdf2Sha1 { @@ -33,6 +47,20 @@ pub struct Pbkdf2Sha256 { pub digest: Vec, } +impl Pbkdf2Sha256 { + #[cfg(feature = "openssl")] + pub fn derive(password: &str, salt: &[u8], iterations: usize) -> Result { + use common::scram::{ScramProvider, Sha256}; + use common::Password; + let digest = Sha256::derive(&Password::Plain(password.to_owned()), salt, iterations)?; + Ok(Pbkdf2Sha256 { + salt: salt.to_vec(), + iterations: iterations, + digest: digest, + }) + } +} + impl Secret for Pbkdf2Sha256 {} impl Pbkdf2Secret for Pbkdf2Sha256 {