diff --git a/src/main.rs b/src/main.rs index e251079..013d368 100644 --- a/src/main.rs +++ b/src/main.rs @@ -100,13 +100,10 @@ impl MPS { } print!("Enter passphrase for storage: "); io::stdout().flush()?; - // TODO: check in db let passphrase = rpassword::read_password()?; print!("\x1B[1A\x1B[2K"); // Move cursor up one line and clear that line io::stdout().flush()?; - if passphrase != "pass" { - return Err(io::Error::new(io::ErrorKind::InvalidData, "Wrong passphrase")); - } + // storage will return error if passphrase is incorrect self.storage = Some(Storage::from_db(passphrase)?); Ok(()) } diff --git a/src/storage.rs b/src/storage.rs index e92fb63..d381fb5 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -76,7 +76,7 @@ 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 @@ -91,10 +91,16 @@ impl Encoder { 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 { @@ -131,6 +137,9 @@ impl Storage { "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 { match line { Ok(line) => { @@ -148,8 +157,8 @@ impl Storage { } } Ok(Storage { - items: items, - encoder: encoder + items, + encoder }) }