blob: 62118515cb15c68825401f9a6b6ec5f40795daa6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
//! A module that handles key generation and key loading
use traits::AutoEncoder;
use super::utils::{hashing, random};
/// A shared key length parameter for all cryptographic operations
///
/// This is *not* ideal and should be replaced with something better
/// at some point in the future
pub const KEY_LENGTH: usize = 64;
/// A wrapper to represent a key for encryption
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Clone)]
pub struct Key {
pub data: Vec<u8>,
}
impl AutoEncoder for Key {}
impl Key {
/// Create a new key from scratch
pub fn generate() -> Key {
let data = random::bytes(KEY_LENGTH);
Key { data: data }
}
/// Use a password as a key
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()
}
/// Used to get the raw data from this key, as a slice copy
pub fn to_slice(&self) -> [u8; KEY_LENGTH] {
let mut slice: [u8; KEY_LENGTH] = [0; KEY_LENGTH];
slice.clone_from_slice(&self.data);
slice
}
}
|