aboutsummaryrefslogtreecommitdiff
path: root/src/security/keys.rs
blob: 6b338895c081d723791c15e7ca957f4fc9b0ec3c (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
49
50
51
52
53
//! A module that handles key generation and key loading
//!



use std::fs::File;
use std::ffi::OsStr;
use std::io::prelude::*;

use super::random;
use super::encoding;
use super::hash;

pub const KEY_LENGTH: usize = 16;


/// A wrapper to represent a key for encryption
#[derive(Clone, Serialize, Deserialize)]
pub struct Key {
    pub data: [u8; KEY_LENGTH],
}


/// A helper function to easily load a key into memory
pub fn load_key(path: &OsStr) -> Key {

    let mut key = String::new();
    let mut key_file = File::open(path).unwrap();
    key_file.read_to_string(&mut key).expect(
        "Failed to load primary key file!",
    );

    let vec = encoding::base64_decode(&key);
    let mut k: [u8; 16] = [0; 16];
    k.clone_from_slice(&vec);

    return Key { data: k };
}


pub fn password_to_key(password: &str) -> Key {
    let hashed = hash::blake2_16(password, "");
    return Key { data: hashed };
}

pub fn generate_key() -> Key {
    let key = random::bytes(KEY_LENGTH);

    let mut k: [u8; KEY_LENGTH] = [0; KEY_LENGTH];
    k.clone_from_slice(&key);

    return Key { data: k };
}