From de6a028e8b8d560047eaa3e59527cec7edf729a7 Mon Sep 17 00:00:00 2001 From: Coin de Gamma Date: Thu, 19 Sep 2024 15:43:08 +0000 Subject: [PATCH] encrypt pass --- src/encoder.rs | 17 ++++++----------- src/storage.rs | 6 +++--- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/src/encoder.rs b/src/encoder.rs index 97cd69c..f96cc96 100644 --- a/src/encoder.rs +++ b/src/encoder.rs @@ -31,8 +31,7 @@ impl Encoder { } // TODO: error type - // TODO: rename to encrypt - pub fn encode(&self, plain_text: &String) -> String { + pub fn encrypt(&self, plain_text: &String) -> String { let key = Key::::from_slice(self.passphrase.as_bytes()); let nonce = Aes256Gcm::generate_nonce(&mut OsRng); @@ -52,8 +51,7 @@ impl Encoder { } // TODO: review error type - // TODO: rename to decrypt - pub fn decode(&self, encrypted_data: String) -> io::Result { + pub fn decrypt(&self, encrypted_data: String) -> io::Result { let encrypted_data = hex::decode(encrypted_data) .expect("failed to decode hex string into vec"); @@ -78,16 +76,13 @@ impl Encoder { Ok(result) } - pub fn test_encoded_passphrase(&self, piassphrase_encoded: String) -> bool { - // TODO: implement - //self.passphrase == passphrase_encoded - // Encoder::get_encoded_test_passphrase(); - return true; + pub fn test_encoded_passphrase(&self, passphrase_encrypted: String) -> io::Result { + let decrypted = self.decrypt(passphrase_encrypted)?; + Ok(PASSWORD_TEST == decrypted) } pub fn get_encoded_test_passphrase(&self) -> String { - // TODO: use this - self.passphrase.clone() + self.encrypt(&PASSWORD_TEST.to_string()) } } diff --git a/src/storage.rs b/src/storage.rs index d0d37d6..60d7729 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -104,7 +104,7 @@ impl Storage { "Bad storage db format: no passphrase in the beginnning" )), }; - if !encoder.test_encoded_passphrase(passtest) { + if !encoder.test_encoded_passphrase(passtest)? { return Err(io::Error::new(io::ErrorKind::InvalidData, "Wrong passphrase")); } for line in lines { @@ -113,7 +113,7 @@ impl Storage { if id.is_none() { id = Some(line); } else { - let content = encoder.decode(line)?; + let content = encoder.decrypt(line)?; items.insert(Item::from(id.unwrap(), content)); id = None; } @@ -198,7 +198,7 @@ impl Storage { for item in self.items.iter() { writeln!(file, "{}", item.id)?; - let content = self.encoder.encode(&item.content); + let content = self.encoder.encrypt(&item.content); writeln!(file, "{}", content)?; } Ok(())