Browse Source

encoder++

fix-typo
Coin de Gamma 3 months ago
parent
commit
9d7a82d75c
  1. 1
      Cargo.toml
  2. 47
      src/encoder.rs
  3. 2
      src/main.rs
  4. 9
      src/storage.rs

1
Cargo.toml

@ -8,6 +8,7 @@ edition = "2021"
[dependencies] [dependencies]
base64 = "0.22.1" base64 = "0.22.1"
clap = { version = "4.5.16", features = ["derive"] } clap = { version = "4.5.16", features = ["derive"] }
hex = "0.4.3"
once_cell = "1.19.0" once_cell = "1.19.0"
rpassword = "7.3.1" rpassword = "7.3.1"
tempfile = "3.12.0" tempfile = "3.12.0"

47
src/encoder.rs

@ -1,38 +1,55 @@
use base64::prelude::*; // TODO: use hex instead
use std::io; use std::io;
static PASSWORD_TEST: &str = "MyPasswordStorage"; // will be added with nonce for storing each time
pub struct Encoder { pub struct Encoder {
// will be stored with padding 32 bytes
passphrase: String passphrase: String
} }
impl Encoder { impl Encoder {
pub fn from(passphrase: String) -> Encoder { pub fn from(passphrase: &String) -> Encoder {
Encoder { passphrase } // TODO: throw error if password longer that 32 bytes
let padded_passphrase = Encoder::get_passhrase_with_padding(passphrase);
Encoder { passphrase: padded_passphrase }
}
fn get_passhrase_with_padding(passphrase: &String) -> String {
let mut result = passphrase.clone();
while result.len() < 32 {
// use '-' for padding, can be anything else
result.push('-');
}
result
} }
// 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 return line.clone();
BASE64_STANDARD.encode(line)
} }
// TODO: review error type // TODO: review error type
pub fn decode(&self, line: String) -> io::Result<String> { pub fn decode(&self, line: String) -> io::Result<String> {
let content = BASE64_STANDARD.decode(line).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
match String::from_utf8(content) { //.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
Ok(s) => Ok(s), //match String::from_utf8(content) {
Err(e) => Err(io::Error::new(io::ErrorKind::InvalidData, e)) // Ok(s) => Ok(s),
} // Err(e) => Err(io::Error::new(io::ErrorKind::InvalidData, e))
//}
return Ok(line.clone());
} }
pub fn test_encoded_passphrase(&self, test_passphrase_encoded: String) -> bool { pub fn test_encoded_passphrase(&self, piassphrase_encoded: String) -> bool {
self.passphrase == test_passphrase_encoded // TODO: implement
//self.passphrase == passphrase_encoded
// Encoder::get_encoded_test_passphrase();
return true;
} }
pub fn get_encoded_test_passphrase(&self) -> String { pub fn get_encoded_test_passphrase(&self) -> String {
// TODO: encode SALT const with passphrase // TODO: use this
self.passphrase.clone() self.passphrase.clone()
} }

2
src/main.rs

@ -140,7 +140,7 @@ impl MPS {
fn edit(&mut self, id: &String) -> io::Result<()> { fn edit(&mut self, id: &String) -> io::Result<()> {
self.login()?; self.login()?;
let mut st = self.storage.as_mut().unwrap(); let st = self.storage.as_mut().unwrap();
if !st.contains(id) { if !st.contains(id) {
let question = format!("Item [{}] exist. Do you want to add it instead?", id); let question = format!("Item [{}] exist. Do you want to add it instead?", id);
match get_prompt(&question)? { match get_prompt(&question)? {

9
src/storage.rs

@ -2,7 +2,6 @@ use crate::encoder;
use encoder::Encoder; use encoder::Encoder;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use std::collections::HashSet; use std::collections::HashSet;
@ -19,7 +18,7 @@ static STORAGE_FOLDER: Lazy<String> = Lazy::new(|| "storage".to_string() );
static STORAGE_PATH: Lazy<String> = Lazy::new(|| { static STORAGE_PATH: Lazy<String> = Lazy::new(|| {
format!("{}/db.mps", &*STORAGE_FOLDER) format!("{}/db.mps", &*STORAGE_FOLDER)
}); });
static PASSWORD_TEST_SALT: &str = "MyPasswordStorage1234567890";
#[derive(Clone)] #[derive(Clone)]
pub struct Item { pub struct Item {
@ -80,7 +79,7 @@ impl Storage {
pub fn new(passphrase: String) -> Storage { pub fn new(passphrase: String) -> Storage {
Storage { Storage {
items: HashSet::<Item>::new(), items: HashSet::<Item>::new(),
encoder: Encoder::from(passphrase) encoder: Encoder::from(&passphrase)
} }
} }
pub fn from_db(passphrase: String) -> io::Result<Storage> { pub fn from_db(passphrase: String) -> io::Result<Storage> {
@ -90,7 +89,7 @@ impl Storage {
"Storage is not initialized" "Storage is not initialized"
)); ));
} }
let encoder = Encoder::from(passphrase); let encoder = Encoder::from(&passphrase);
// TODO: throw error is password is incorrect // TODO: throw error is password is incorrect
let file = fs::File::open(&*STORAGE_PATH)?; let file = fs::File::open(&*STORAGE_PATH)?;
let reader = io::BufReader::new(file); let reader = io::BufReader::new(file);
@ -199,7 +198,7 @@ impl Storage {
for item in self.items.iter() { for item in self.items.iter() {
writeln!(file, "{}", item.id)?; writeln!(file, "{}", item.id)?;
let content = self.encoder.encode(item.content.clone()); let content = self.encoder.encode(&item.content);
writeln!(file, "{}", content)?; writeln!(file, "{}", content)?;
} }
Ok(()) Ok(())

Loading…
Cancel
Save