Browse Source

password continue

fix-typo
Coin de Gamma 4 months ago
parent
commit
1e6fcb56c0
  1. 8
      src/main.rs
  2. 33
      src/storage.rs

8
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 {

33
src/storage.rs

@ -16,6 +16,7 @@ static STORAGE_FOLDER: Lazy<String> = Lazy::new(|| "storage".to_string() );
static STORAGE_PATH: Lazy<String> = 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<Storage> {
let encoder = Encoder::from(password);
pub fn new(passphrase: String) -> Storage {
Storage {
items: HashSet::<Item>::new(),
encoder: Encoder::from(passphrase)
}
}
pub fn from_db(passphrase: String) -> io::Result<Storage> {
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::<Item>::new();
let mut id: Option<String> = 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(())
}
}

Loading…
Cancel
Save