diff options
-rw-r--r-- | lockchain-core/src/record.rs | 2 | ||||
-rw-r--r-- | lockchain-core/src/traits.rs | 25 | ||||
-rw-r--r-- | lockchain-core/src/users.rs | 3 | ||||
-rw-r--r-- | lockchain-crypto/src/databody.rs | 27 | ||||
-rw-r--r-- | lockchain-crypto/src/engine.rs | 19 | ||||
-rw-r--r-- | lockchain-crypto/src/lib.rs | 37 |
6 files changed, 70 insertions, 43 deletions
diff --git a/lockchain-core/src/record.rs b/lockchain-core/src/record.rs index 97f5fd2..d103b74 100644 --- a/lockchain-core/src/record.rs +++ b/lockchain-core/src/record.rs @@ -96,7 +96,7 @@ impl<T: Body> Record<T> { #[derive(Serialize, Deserialize)] pub struct EncryptedBody { - pub cipher: String, + pub ciph: String, } impl Body for EncryptedBody { diff --git a/lockchain-core/src/traits.rs b/lockchain-core/src/traits.rs index 01ae484..8b97745 100644 --- a/lockchain-core/src/traits.rs +++ b/lockchain-core/src/traits.rs @@ -11,7 +11,7 @@ //! compilation work without external crates but not calling //! functions at runtime. -use record::{Header, Payload, EncryptedBody}; +use record::{EncryptedBody, Header, Payload}; use serde::{de::DeserializeOwned, Serialize}; use users::User; @@ -48,16 +48,21 @@ pub trait UserLogin { fn login(name: &str, password: &str, salt: &str) -> Option<User>; } -/// This is a trait which needs to be implemented by any -/// backend which hopes to do encryption on data. -pub trait Encryption: Body + AutoEncoder { - fn encrypt(&mut self) -> EncryptedBody { - unimplemented!() - } +/// A set of utility function that need to be implemented in order +/// for a type to be encryptable or decryptable. +pub trait Encryptable: AutoEncoder {} - fn decrypt(_enc: EncryptedBody) -> Box<Self> { - unimplemented!() - } +/// A base trait that describes the basic functionality of +/// an encryption backend which handles encrypted files. +/// +/// Encryption is never done directly on the bodies, only via +/// this scheduler type with the help of the [[Encryptable]] trait. +pub trait EncryptionHandler<T> +where + T: Encryptable + AutoEncoder + Body, +{ + fn encrypt(&mut self, item: T) -> EncryptedBody; + fn decrypt(&mut self, item: EncryptedBody) -> T; } /// A trait that abstracts file or record loading for diff --git a/lockchain-core/src/users.rs b/lockchain-core/src/users.rs index 9fd4476..fb83283 100644 --- a/lockchain-core/src/users.rs +++ b/lockchain-core/src/users.rs @@ -3,6 +3,7 @@ use bcrypt::{self, DEFAULT_COST}; /// Simple user authentication abstraction +#[allow(dead_code)] pub struct User { name: String, pw_hash: String, @@ -12,7 +13,7 @@ pub struct User { impl User { /// - fn register(name: &str, password: &str, salt: &str) -> Option<User> { + pub fn register(name: &str, password: &str, salt: &str) -> Option<User> { Some(User { name: name.to_owned(), pw_hash: bcrypt::hash(&format!("{}{}", password, salt), DEFAULT_COST).ok()?, diff --git a/lockchain-crypto/src/databody.rs b/lockchain-crypto/src/databody.rs new file mode 100644 index 0000000..1387f9d --- /dev/null +++ b/lockchain-crypto/src/databody.rs @@ -0,0 +1,27 @@ +//! + +use lcc::traits::{AutoEncoder, Body}; +use lcc::Payload; +use std::collections::BTreeMap; + +#[derive(Serialize, Deserialize)] +pub struct DataBody { + tree: BTreeMap<String, Payload>, +} + +impl AutoEncoder for DataBody {} + +impl Body for DataBody { + fn get_field(&self, key: &str) -> Option<&Payload> { + self.tree.get(key) + } + + fn set_field(&mut self, key: &str, value: Payload) -> Option<()> { + self.tree.insert(key.to_owned(), value)?; + Some(()) + } + + fn flatten(&mut self) -> Option<()> { + None + } +} diff --git a/lockchain-crypto/src/engine.rs b/lockchain-crypto/src/engine.rs new file mode 100644 index 0000000..1030bf2 --- /dev/null +++ b/lockchain-crypto/src/engine.rs @@ -0,0 +1,19 @@ +//! + +use databody::DataBody; +use lcc::{traits::{Encryptable, EncryptionHandler}, + EncryptedBody}; + +impl Encryptable for DataBody {} + +pub struct AesEngine {} + +impl EncryptionHandler<DataBody> for AesEngine { + fn encrypt(&mut self, item: DataBody) -> EncryptedBody { + unimplemented!() + } + + fn decrypt(&mut self, item: EncryptedBody) -> DataBody { + unimplemented!() + } +} diff --git a/lockchain-crypto/src/lib.rs b/lockchain-crypto/src/lib.rs index 5648106..0799c96 100644 --- a/lockchain-crypto/src/lib.rs +++ b/lockchain-crypto/src/lib.rs @@ -6,37 +6,12 @@ extern crate serde; extern crate lockchain_core as lcc; -use lcc::{Payload, EncryptedBody}; -use lcc::traits::{Body, AutoEncoder, Base64AutoEncoder, Encryption}; - +use lcc::{traits::{AutoEncoder, Body}, + Payload}; use std::collections::BTreeMap; -#[derive(Serialize, Deserialize)] -pub struct DataBody { - tree: BTreeMap<String, Payload>, -} - -impl AutoEncoder for DataBody {} - -impl Body for DataBody { - fn get_field(&self, key: &str) -> Option<&Payload> { - self.tree.get(key).as_ref()? - } - - fn set_field(&mut self, key: &str, value: Payload) -> Option<()> { - self.tree.insert(key, value); - Some(()) - } - - fn flatten(&mut self) -> Option<()> { - None - } -} +mod databody; +mod engine; -impl Encryption for DataBody { - fn encrypt(&mut self) -> EncryptedBody { - EncryptedBody { - cipher: self.encode() - } - } -}
\ No newline at end of file +pub use databody::*; +pub use engine::*; |