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: rename to encrypt
pub fn encode(&self, plain_text: &String) -> String {
pub fn encrypt(&self, plain_text: &String) -> String {
let key = Key::<Aes256Gcm>::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<String> {
pub fn decrypt(&self, encrypted_data: String) -> io::Result<String> {
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<bool> {
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())
}
}

6
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(())

Loading…
Cancel
Save