aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKatharina Fey <kookie@spacekookie.de>2018-04-02 13:53:30 +0200
committerKatharina Fey <kookie@spacekookie.de>2018-04-02 13:53:30 +0200
commitc789005d19660f9f56b6eab7090a58758b5c7a07 (patch)
treeb2d5b8290486350c432bbaee7f6b72f982bbaf72
parent5fe943efec2f7c0210d9b12767876ef3a251dfac (diff)
Refactoring utilities in security module. Definine basic library crate
Diffstat (limited to '')
-rw-r--r--Cargo.lock3
-rw-r--r--Cargo.toml14
-rw-r--r--src/lib.rs16
-rw-r--r--src/main.rs76
-rw-r--r--src/security/encoding.rs25
-rw-r--r--src/security/keys.rs8
-rw-r--r--src/security/mod.rs4
-rw-r--r--src/security/random.rs34
-rw-r--r--src/security/utils.rs90
9 files changed, 120 insertions, 150 deletions
diff --git a/Cargo.lock b/Cargo.lock
index d6eb8f2..b6a659e 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -166,13 +166,12 @@ version = "0.2.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
-name = "lockchain"
+name = "lockchain-core"
version = "0.1.0"
dependencies = [
"base64 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"blake2 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"chrono 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
- "generic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
"miscreant 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"ordermap 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"pwhash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
diff --git a/Cargo.toml b/Cargo.toml
index af3c30c..6c442a3 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,14 +1,13 @@
[package]
-name = "lockchain"
+name = "lockchain-core"
+description = "The core library that handles lockchain vaults and records"
+docs = "https://docs.rs/lockchain-core"
+readme = "README.md"
version = "0.1.0"
authors = ["Katharina Fey <kookie@spacekookie.de>"]
[dependencies]
-serde = "1.0"
-serde_json = "1.0"
-serde_derive = "1.0"
chrono = { version = "0.4", features = ["serde", "rustc-serialize"] }
-generic-array = "0.9.0"
ordermap = "0.3.2"
# Cryptography
@@ -17,3 +16,8 @@ base64 = "0.8.0"
blake2 = "0.7"
pwhash = "0.1"
rand = "0.3.0"
+
+# Serialisation
+serde = "1.0"
+serde_json = "1.0"
+serde_derive = "1.0" \ No newline at end of file
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..81ec433
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,16 @@
+//! Core lockchain
+
+extern crate chrono;
+extern crate serde;
+extern crate serde_json;
+#[macro_use]
+extern crate serde_derive;
+extern crate base64;
+extern crate blake2;
+extern crate miscreant;
+extern crate rand;
+
+pub mod record;
+mod security;
+mod test;
+pub mod vault;
diff --git a/src/main.rs b/src/main.rs
deleted file mode 100644
index 30f0f72..0000000
--- a/src/main.rs
+++ /dev/null
@@ -1,76 +0,0 @@
-//! This will become the lockchain library crate at some point
-//!
-//! For now it's a hybrid between a library and a Gtk+ UI
-
-extern crate chrono;
-extern crate serde;
-extern crate serde_json;
-#[macro_use]
-extern crate serde_derive;
-extern crate base64;
-extern crate blake2;
-extern crate miscreant;
-extern crate rand;
-
-pub mod record;
-mod security;
-mod test;
-pub mod vault;
-
-use security::{crypto::CryptoEngine, keys::Key};
-use record::Record;
-
-fn main() {
- let rec = Record::new("name", "category");
- println!("{:?}", rec);
-
- println!("====================================");
- let mut engine = CryptoEngine::new(Key::new());
- let cipher = engine.encrypt(&rec).unwrap();
- println!("Encrypted: {}", cipher);
-
- println!("====================================");
-
- let decrypted: Record = engine.decrypt(cipher).unwrap();
- println!("{:?}", decrypted);
-}
-
-// fn load() {
-// let vault = Vault::load(
-// "Personal",
-// "/home/spacekookie/Desktop",
-// "my password is cheese",
-// );
-// println!("{:?}", vault.records);
-// }
-
-// fn create_and_populate() {
-// /* Create a new vault at a path, name and primary password */
-// let mut vault = match Vault::new(
-// "Personal",
-// "/home/spacekookie/Desktop",
-// "my password is cheese",
-// ) {
-// Ok(s) => s,
-// Err(e) => panic!("Somehow failed to create the vault because {:?}", e),
-// };
-
-// /* Add a record with some tags */
-// vault.add_record("mastodon", "web", vec!["social", "network"]);
-
-// /* Add a few data fields to the body */
-// vault.add_data(
-// "mastodon",
-// "url",
-// Text(String::from("https://mastodon.social")),
-// );
-// vault.add_data("mastodon", "user", Text(String::from("spacekookie")));
-// vault.add_data(
-// "mastodon",
-// "password",
-// Text(String::from("My password is molten cheese")),
-// );
-
-// /* Sync the changes to disk */
-// vault.sync();
-// }
diff --git a/src/security/encoding.rs b/src/security/encoding.rs
deleted file mode 100644
index abfe052..0000000
--- a/src/security/encoding.rs
+++ /dev/null
@@ -1,25 +0,0 @@
-//! Simple encoding submodule
-//!
-
-use std::fmt::Write;
-use base64;
-
-/// 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;
-}
diff --git a/src/security/keys.rs b/src/security/keys.rs
index 3e41688..83898ba 100644
--- a/src/security/keys.rs
+++ b/src/security/keys.rs
@@ -6,9 +6,7 @@
use std::fs::File;
use std::io::prelude::*;
-use super::random;
-use super::hash;
-
+use super::utils::{Hashing, Encoding, Random};
pub const KEY_LENGTH: usize = 64;
@@ -21,13 +19,13 @@ pub struct Key {
impl Key {
/// Create a new key from scratch
pub fn new() -> Key {
- let data = random::bytes(KEY_LENGTH);
+ 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 = hash::blake2_16(password, salt);
+ let hashed = Hashing::blake2(password, salt);
let mut vec: Vec<u8> = Vec::new();
for b in &hashed {
vec.push(b.clone());
diff --git a/src/security/mod.rs b/src/security/mod.rs
index 249377f..f12bab6 100644
--- a/src/security/mod.rs
+++ b/src/security/mod.rs
@@ -4,8 +4,6 @@
//! and high-level encryption and decryption functions.
// Utility modules
-pub mod encoding;
-pub mod random;
+pub mod utils;
pub mod crypto;
-pub mod hash;
pub mod keys; \ No newline at end of file
diff --git a/src/security/random.rs b/src/security/random.rs
deleted file mode 100644
index eb88d99..0000000
--- a/src/security/random.rs
+++ /dev/null
@@ -1,34 +0,0 @@
-//! Random number utility module for lockchain
-//!
-//! Provides stateless secure random number and byte generation
-
-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;
-} \ No newline at end of file
diff --git a/src/security/utils.rs b/src/security/utils.rs
new file mode 100644
index 0000000..7b38674
--- /dev/null
+++ b/src/security/utils.rs
@@ -0,0 +1,90 @@
+//! 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;
+ }
+}