diff options
Diffstat (limited to 'lockchain-crypto/src')
-rw-r--r-- | lockchain-crypto/src/keys.rs | 37 | ||||
-rw-r--r-- | lockchain-crypto/src/lib.rs | 3 | ||||
-rw-r--r-- | lockchain-crypto/src/utils.rs | 88 |
3 files changed, 0 insertions, 128 deletions
diff --git a/lockchain-crypto/src/keys.rs b/lockchain-crypto/src/keys.rs deleted file mode 100644 index bde3e69..0000000 --- a/lockchain-crypto/src/keys.rs +++ /dev/null @@ -1,37 +0,0 @@ -//! A module that handles key generation and key loading - -use super::utils::{hashing, random}; -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-crypto/src/lib.rs b/lockchain-crypto/src/lib.rs index 5daba02..e4c5366 100644 --- a/lockchain-crypto/src/lib.rs +++ b/lockchain-crypto/src/lib.rs @@ -8,9 +8,6 @@ extern crate serde_derive; extern crate serde; extern crate miscreant; -extern crate base64; -extern crate blake2; -extern crate rand; extern crate lockchain_core as lcc; diff --git a/lockchain-crypto/src/utils.rs b/lockchain-crypto/src/utils.rs deleted file mode 100644 index 36e611c..0000000 --- a/lockchain-crypto/src/utils.rs +++ /dev/null @@ -1,88 +0,0 @@ -//! 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; - } -} |