use traits::AutoEncoder;
use super::utils::{hashing, random};
pub const KEY_LENGTH: usize = 64;
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Clone)]
pub struct Key {
pub data: Vec<u8>,
}
impl AutoEncoder for Key {}
impl Key {
pub fn generate() -> Key {
let data = random::bytes(KEY_LENGTH);
Key { data: data }
}
pub fn from_password(password: &str, salt: &str) -> Key {
let hashed = hashing::blake2(password, salt);
let mut vec: Vec<u8> = Vec::new();
for b in &hashed {
vec.push(b.clone());
}
Key { data: vec }
}
pub fn to_vec(&self) -> Vec<u8> {
self.data.clone()
}
pub fn to_slice(&self) -> [u8; KEY_LENGTH] {
let mut slice: [u8; KEY_LENGTH] = [0; KEY_LENGTH];
slice.clone_from_slice(&self.data);
slice
}
}