Browse Source

encrypt pass

fix-typo
Coin de Gamma 3 months ago
parent
commit
de6a028e8b
  1. 17
      src/encoder.rs
  2. 6
      src/storage.rs

17
src/encoder.rs

@ -31,8 +31,7 @@ impl Encoder {
} }
// TODO: error type // TODO: error type
// TODO: rename to encrypt pub fn encrypt(&self, plain_text: &String) -> String {
pub fn encode(&self, plain_text: &String) -> String {
let key = Key::<Aes256Gcm>::from_slice(self.passphrase.as_bytes()); let key = Key::<Aes256Gcm>::from_slice(self.passphrase.as_bytes());
let nonce = Aes256Gcm::generate_nonce(&mut OsRng); let nonce = Aes256Gcm::generate_nonce(&mut OsRng);
@ -52,8 +51,7 @@ impl Encoder {
} }
// TODO: review error type // TODO: review error type
// TODO: rename to decrypt pub fn decrypt(&self, encrypted_data: String) -> io::Result<String> {
pub fn decode(&self, encrypted_data: String) -> io::Result<String> {
let encrypted_data = hex::decode(encrypted_data) let encrypted_data = hex::decode(encrypted_data)
.expect("failed to decode hex string into vec"); .expect("failed to decode hex string into vec");
@ -78,16 +76,13 @@ impl Encoder {
Ok(result) Ok(result)
} }
pub fn test_encoded_passphrase(&self, piassphrase_encoded: String) -> bool { pub fn test_encoded_passphrase(&self, passphrase_encrypted: String) -> io::Result<bool> {
// TODO: implement let decrypted = self.decrypt(passphrase_encrypted)?;
//self.passphrase == passphrase_encoded Ok(PASSWORD_TEST == decrypted)
// Encoder::get_encoded_test_passphrase();
return true;
} }
pub fn get_encoded_test_passphrase(&self) -> String { pub fn get_encoded_test_passphrase(&self) -> String {
// TODO: use this self.encrypt(&PASSWORD_TEST.to_string())
self.passphrase.clone()
} }
} }

6
src/storage.rs

@ -104,7 +104,7 @@ impl Storage {
"Bad storage db format: no passphrase in the beginnning" "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")); return Err(io::Error::new(io::ErrorKind::InvalidData, "Wrong passphrase"));
} }
for line in lines { for line in lines {
@ -113,7 +113,7 @@ impl Storage {
if id.is_none() { if id.is_none() {
id = Some(line); id = Some(line);
} else { } else {
let content = encoder.decode(line)?; let content = encoder.decrypt(line)?;
items.insert(Item::from(id.unwrap(), content)); items.insert(Item::from(id.unwrap(), content));
id = None; id = None;
} }
@ -198,7 +198,7 @@ impl Storage {
for item in self.items.iter() { for item in self.items.iter() {
writeln!(file, "{}", item.id)?; writeln!(file, "{}", item.id)?;
let content = self.encoder.encode(&item.content); let content = self.encoder.encrypt(&item.content);
writeln!(file, "{}", content)?; writeln!(file, "{}", content)?;
} }
Ok(()) Ok(())

Loading…
Cancel
Save