diff options
author | Katharina Fey <kookie@spacekookie.de> | 2018-05-08 14:38:02 +0200 |
---|---|---|
committer | Katharina Fey <kookie@spacekookie.de> | 2018-05-08 14:38:02 +0200 |
commit | d18641e8374d05c663a1b001329a17d32660abf7 (patch) | |
tree | a7b8ae81ac54234a57fab16e33fc1c5f0823b0d1 /lockchain-core/src/crypto | |
parent | 73ff5fdf4d94ba244abeb3796ef16fe3f69157d7 (diff) |
Changing the way that vault traits work
Diffstat (limited to 'lockchain-core/src/crypto')
-rw-r--r-- | lockchain-core/src/crypto/keys.rs | 41 | ||||
-rw-r--r-- | lockchain-core/src/crypto/utils.rs | 88 |
2 files changed, 129 insertions, 0 deletions
diff --git a/lockchain-core/src/crypto/keys.rs b/lockchain-core/src/crypto/keys.rs new file mode 100644 index 0000000..f132075 --- /dev/null +++ b/lockchain-core/src/crypto/keys.rs @@ -0,0 +1,41 @@ +//! A module that handles key generation and key loading + +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 Key { + + /// Create a new key from scratch + pub fn generate() -> Key { + let data = random::bytes(KEY_LENGTH); + return 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()); + } + return Key { data: vec }; + } + + /// 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); + return slice; + } +} diff --git a/lockchain-core/src/crypto/utils.rs b/lockchain-core/src/crypto/utils.rs new file mode 100644 index 0000000..36e611c --- /dev/null +++ b/lockchain-core/src/crypto/utils.rs @@ -0,0 +1,88 @@ +//! A collection of utility submodules + +/// Encoding module +pub mod encoding { + use base64; + use std::fmt::Write; + + /// Encode a piece of arbitary data into a bse64 string + pub fn base64_encode(data: &Vec<u8>) -> String { + return base64::encode(data); + } + + /// Decode a base64 string into arbitrary data + pub fn base64_decode(data: &String) -> Vec<u8> { + return base64::decode(data).unwrap(); + } + + /// Simply encode a byte-string as hexadecimal symbols + pub fn encode_hex(data: &str) -> String { + let mut s = String::new(); + for &byte in data.as_bytes() { + write!(&mut s, "{:X}", byte).expect("Unable to HEX encode!"); + } + + return s; + } +} + +/// A hashing utility module +pub mod hashing { + use blake2::Blake2s; + use blake2::digest::{Input, VariableOutput}; + + const BLAKE_16_LENGTH: usize = 16; + + /// Hash a value with blake2 + pub fn blake2(data: &str, salt: &str) -> [u8; BLAKE_16_LENGTH] { + let mut hasher = match Blake2s::new(BLAKE_16_LENGTH) { + Ok(res) => res, + Err(some) => panic!(some), + }; + + let to_hash = format!("{}{}", data, salt); + hasher.process(to_hash.as_bytes()); + + let mut buffer = [0u8; BLAKE_16_LENGTH]; + match hasher.variable_result(&mut buffer) { + Ok(res) => res, + Err(e) => panic!(e), + }; + + return buffer; + } +} + +/// Random number utility module for lockchain +/// +/// Provides stateless secure random number and byte generation +pub mod random { + use rand::{thread_rng, Rng}; + + /// Generate a random number with an upper bound + pub fn number(bound: u64) -> u64 { + return thread_rng().next_u64() % bound; + } + + /// Generate a sequence of random bytes that are returned + /// as a vector. + /// + /// Can at most allocate 2048 bytes at a time + /// FIXME: That shouldn't have a limit! + pub fn bytes(length: usize) -> Vec<u8> { + let mut vec: Vec<u8> = Vec::new(); + + if length > 2048 { + return vec; + } + + let mut random_data = [0u8; 2048]; + thread_rng().fill_bytes(&mut random_data); + + for i in 0..length { + vec.push(random_data[i]); + } + + return vec; + } +} |