use hmac::Hmac;
use pbkdf2::pbkdf2;
use rand_core::{OsRng, RngCore};
use sha2::Sha256;
pub struct EncryptionKey {
pub pubk: [u8; 32],
pub salt: [u8; 16],
}
impl EncryptionKey {
pub fn new(password: &[u8], rounds: u32) -> Self {
let mut salt = [0; 16];
OsRng.fill_bytes(&mut salt);
let mut pubk = [0; 32];
if let Err(_) = pbkdf2::<Hmac<Sha256>>(password, &salt, rounds, &mut pubk) {
panic!("Key derivation failed")
}
Self { pubk, salt }
}
pub fn with_salt(password: &[u8], salt: [u8; 16], rounds: u32) -> Self {
let mut pubk = [0; 32];
if let Err(_) = pbkdf2::<Hmac<Sha256>>(password, &salt, rounds, &mut pubk) {
panic!("Key derivation failed")
}
Self { pubk, salt }
}
}