Browse Source

passphrasse test in db

fix-typo
Coin de Gamma 3 months ago
parent
commit
174244fcef
  1. 5
      src/main.rs
  2. 15
      src/storage.rs

5
src/main.rs

@ -100,13 +100,10 @@ impl MPS {
} }
print!("Enter passphrase for storage: "); print!("Enter passphrase for storage: ");
io::stdout().flush()?; io::stdout().flush()?;
// TODO: check in db
let passphrase = rpassword::read_password()?; let passphrase = rpassword::read_password()?;
print!("\x1B[1A\x1B[2K"); // Move cursor up one line and clear that line print!("\x1B[1A\x1B[2K"); // Move cursor up one line and clear that line
io::stdout().flush()?; io::stdout().flush()?;
if passphrase != "pass" { // storage will return error if passphrase is incorrect
return Err(io::Error::new(io::ErrorKind::InvalidData, "Wrong passphrase"));
}
self.storage = Some(Storage::from_db(passphrase)?); self.storage = Some(Storage::from_db(passphrase)?);
Ok(()) Ok(())
} }

15
src/storage.rs

@ -76,7 +76,7 @@ impl Encoder {
pub fn from(passphrase: String) -> Encoder { pub fn from(passphrase: String) -> Encoder {
Encoder { passphrase } Encoder { passphrase }
} }
// TODO: get by ref // TODO: get by ref
pub fn encode(&self, line: String) -> String { pub fn encode(&self, line: String) -> String {
// TODO: use passphrasee to encode // TODO: use passphrasee to encode
@ -91,10 +91,16 @@ impl Encoder {
Err(e) => Err(io::Error::new(io::ErrorKind::InvalidData, e)) 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 { pub fn get_encoded_test_passphrase(&self) -> String {
// TODO: encode SALT const with passphrase // TODO: encode SALT const with passphrase
self.passphrase.clone() self.passphrase.clone()
} }
} }
pub struct Storage { pub struct Storage {
@ -131,6 +137,9 @@ 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) {
return Err(io::Error::new(io::ErrorKind::InvalidData, "Wrong passphrase"));
}
for line in lines { for line in lines {
match line { match line {
Ok(line) => { Ok(line) => {
@ -148,8 +157,8 @@ impl Storage {
} }
} }
Ok(Storage { Ok(Storage {
items: items, items,
encoder: encoder encoder
}) })
} }

Loading…
Cancel
Save