diff --git a/src/main.rs b/src/main.rs index 5d37230..4d9f1bc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -95,8 +95,8 @@ fn init() -> io::Result<()> { } fn add(id: &String) -> io::Result<()> { - // TODO: get login passphrase - let mut st = Storage::new(String::from(""))?; + let passphrase = login()?; + let mut st = Storage::from_db(passphrase)?; if st.contains(id) { // TODO: ask to edit existing in outer function which invoked this one return Err(io::Error::new( @@ -113,8 +113,8 @@ fn add(id: &String) -> io::Result<()> { } fn list() -> io:: Result<()> { - // TODO: ask login - let st = Storage::new(String::from(""))?; + let passphrase = login()?; + let st = Storage::from_db(passphrase)?; let mut vec: Vec<_> = st.items.iter().collect(); vec.sort(); for item in &vec { diff --git a/src/storage.rs b/src/storage.rs index cbfc581..a50c61f 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -16,6 +16,7 @@ static STORAGE_FOLDER: Lazy = Lazy::new(|| "storage".to_string() ); static STORAGE_PATH: Lazy = Lazy::new(|| { format!("{}/db.mps", &*STORAGE_FOLDER) }); +static PASSWORD_TEST_SALT: &str = "MyPasswordStorage1234567890"; pub struct Item { @@ -90,6 +91,10 @@ impl Encoder { Err(e) => Err(io::Error::new(io::ErrorKind::InvalidData, e)) } } + pub fn get_encoded_test_passphrase(&self) -> String { + // TODO: encode SALT const with passphrase + self.passphrase.clone() + } } pub struct Storage { @@ -98,15 +103,27 @@ pub struct Storage { } impl Storage { - // TODO: make path as String too - pub fn new(password: String) -> io::Result { - let encoder = Encoder::from(password); + pub fn new(passphrase: String) -> Storage { + Storage { + items: HashSet::::new(), + encoder: Encoder::from(passphrase) + } + } + pub fn from_db(passphrase: String) -> io::Result { + let encoder = Encoder::from(passphrase); // TODO: throw error is password is incorrect let file = fs::File::open(&*STORAGE_PATH)?; let reader = io::BufReader::new(file); let mut items = HashSet::::new(); let mut id: Option = None; - for line in reader.lines() { + let mut lines = reader.lines(); + // TODO: uncomment when innit saving implemented + let passtest = match lines.next() { + Some(line) => line?, + None => return Err(io::Error::new(io::ErrorKind::InvalidData, "Bad storage db format: no passphrase in the beginnning")), + }; + println!("pass: {}", passtest); + for line in lines { match line { Ok(line) => { if id.is_none() { @@ -128,14 +145,16 @@ impl Storage { }) } - pub fn init(_passphrase: String) -> io::Result<()> { - // TODO: use pasphrase + pub fn init(passphrase: String) -> io::Result<()> { fs::create_dir(&*STORAGE_FOLDER)?; println!("Storage folder created"); //let mut db = DB::init(&*STORAGE_PATH, pass)?; fs::File::create(&*STORAGE_PATH)?; println!("Storage db created."); + let st = Storage::new(passphrase); + st.dump()?; + println!("Initialization complete."); println!(""); println!("Now it's required to add folder `{}` under git manually.", &*STORAGE_FOLDER); @@ -165,13 +184,13 @@ impl Storage { .write(true) .append(false) .open(&*STORAGE_PATH)?; + writeln!(file, "{}", self.encoder.get_encoded_test_passphrase())?; for item in self.items.iter() { writeln!(file, "{}", item.id)?; let content = self.encoder.encode(item.content.clone()); writeln!(file, "{}", content)?; } - Ok(()) } }