From 22a25a35659f803c7a11c06a3a7dd2cc9d652bcf Mon Sep 17 00:00:00 2001 From: Coin de Gamma Date: Thu, 19 Sep 2024 07:32:50 +0000 Subject: [PATCH] encoder --- src/encoder.rs | 39 +++++++++++++++++++++++++++++++++++++++ src/main.rs | 1 + src/storage.rs | 40 ++++------------------------------------ 3 files changed, 44 insertions(+), 36 deletions(-) create mode 100644 src/encoder.rs diff --git a/src/encoder.rs b/src/encoder.rs new file mode 100644 index 0000000..7192a02 --- /dev/null +++ b/src/encoder.rs @@ -0,0 +1,39 @@ +use base64::prelude::*; // TODO: use hex instead + +use std::io; + + +pub struct Encoder { + passphrase: String +} + +impl Encoder { + pub fn from(passphrase: String) -> Encoder { + Encoder { passphrase } + } + + // TODO: get by ref + pub fn encode(&self, line: String) -> String { + // TODO: use passphrasee to encode + BASE64_STANDARD.encode(line) + } + + // TODO: review error type + pub fn decode(&self, line: String) -> io::Result { + let content = BASE64_STANDARD.decode(line).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; + match String::from_utf8(content) { + Ok(s) => Ok(s), + Err(e) => Err(io::Error::new(io::ErrorKind::InvalidData, e)) + } + } + + pub fn test_encoded_passphrase(&self, test_passphrase_encoded: String) -> bool { + self.passphrase == test_passphrase_encoded + } + + pub fn get_encoded_test_passphrase(&self) -> String { + // TODO: encode SALT const with passphrase + self.passphrase.clone() + } + +} diff --git a/src/main.rs b/src/main.rs index 013d368..4017795 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +mod encoder; mod storage; mod editor; diff --git a/src/storage.rs b/src/storage.rs index d381fb5..b8cda7f 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -1,4 +1,7 @@ -use base64::prelude::*; +use crate::encoder; + +use encoder::Encoder; + use once_cell::sync::Lazy; @@ -68,41 +71,6 @@ impl Ord for Item { } } -struct Encoder { - passphrase: String -} - -impl Encoder { - pub fn from(passphrase: String) -> Encoder { - Encoder { passphrase } - } - - // TODO: get by ref - pub fn encode(&self, line: String) -> String { - // TODO: use passphrasee to encode - BASE64_STANDARD.encode(line) - } - - // TODO: review error type - pub fn decode(&self, line: String) -> io::Result { - let content = BASE64_STANDARD.decode(line).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; - match String::from_utf8(content) { - Ok(s) => Ok(s), - Err(e) => Err(io::Error::new(io::ErrorKind::InvalidData, e)) - } - } - - pub fn test_encoded_passphrase(&self, test_passphrase_encoded: String) -> bool { - self.passphrase == test_passphrase_encoded - } - - pub fn get_encoded_test_passphrase(&self) -> String { - // TODO: encode SALT const with passphrase - self.passphrase.clone() - } - -} - pub struct Storage { items: HashSet::, encoder: Encoder