diff options
author | Jesse Luehrs <doy@tozt.net> | 2020-04-05 02:17:25 -0400 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2020-04-05 02:17:25 -0400 |
commit | cd894c27e0b0d5746b95b9c2933da3ba6e9a3f5b (patch) | |
tree | 94de4da0e8ac1cea7a855f8fb1d16d6f320e7e72 /src/identity.rs | |
parent | 070315ce5f80e82fcb5f39c15cd7bbf1682fdf8b (diff) | |
download | rbw-cd894c27e0b0d5746b95b9c2933da3ba6e9a3f5b.tar.gz rbw-cd894c27e0b0d5746b95b9c2933da3ba6e9a3f5b.zip |
basic implementation of the cryptographic stuff
Diffstat (limited to 'src/identity.rs')
-rw-r--r-- | src/identity.rs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/identity.rs b/src/identity.rs new file mode 100644 index 0000000..85b4853 --- /dev/null +++ b/src/identity.rs @@ -0,0 +1,44 @@ +use crate::prelude::*; + +pub struct Identity { + pub email: String, + pub enc_key: Vec<u8>, + pub mac_key: Vec<u8>, + pub master_password_hash: Vec<u8>, +} + +impl Identity { + pub fn new(email: &str, password: &str, iterations: u32) -> Result<Self> { + let mut key = vec![0u8; 32]; + pbkdf2::pbkdf2::<hmac::Hmac<sha2::Sha256>>( + password.as_bytes(), + email.as_bytes(), + iterations as usize, + &mut key, + ); + + let mut hash = vec![0u8; 32]; + pbkdf2::pbkdf2::<hmac::Hmac<sha2::Sha256>>( + &key, + password.as_bytes(), + 1, + &mut hash, + ); + + let hkdf = hkdf::Hkdf::<sha2::Sha256>::from_prk(&key) + .map_err(|_| Error::HkdfFromPrk)?; + hkdf.expand(b"enc", &mut key) + .map_err(|_| Error::HkdfExpand)?; + + let mut mac_key = vec![0u8; 32]; + hkdf.expand(b"mac", &mut mac_key) + .map_err(|_| Error::HkdfExpand)?; + + Ok(Self { + email: email.to_string(), + enc_key: key, + mac_key, + master_password_hash: hash, + }) + } +} |