Browse Source

encoder++

fix-typo
Coin de Gamma 3 months ago
parent
commit
209e49abe8
  1. 41
      src/encoder.rs
  2. 11
      src/storage.rs

41
src/encoder.rs

@ -31,7 +31,7 @@ impl Encoder {
}
// TODO: error type
pub fn encrypt(&self, plain_text: &String) -> String {
pub fn encrypt(&self, plain_text: &String) -> io::Result<String> {
let key = Key::<Aes256Gcm>::from_slice(self.passphrase.as_bytes());
let nonce = Aes256Gcm::generate_nonce(&mut OsRng);
@ -39,49 +39,56 @@ impl Encoder {
// TODO: mar error inted of expect
let ciphered_data = cipher.encrypt(&nonce, plain_text.as_bytes())
.expect("failed to encrypt");
.map_err(|_| io::Error::new(
io::ErrorKind::Other,
"Failed to encrypt"
))?;
// combining nonce and encrypted data together
// for storage purpose
let mut encrypted_data: Vec<u8> = nonce.to_vec();
encrypted_data.extend_from_slice(&ciphered_data);
hex::encode(encrypted_data)
Ok(hex::encode(encrypted_data))
}
// TODO: review error type
pub fn decrypt(&self, encrypted_data: String) -> io::Result<String> {
let encrypted_data = hex::decode(encrypted_data)
.expect("failed to decode hex string into vec");
.map_err(|_| io::Error::new(
io::ErrorKind::Other,
"failed to decode hex string into vec"
))?;
let key = Key::<Aes256Gcm>::from_slice(self.passphrase.as_bytes());
let (nonce_arr, ciphered_data) = encrypted_data.split_at(12);
let nonce = Nonce::from_slice(nonce_arr);
let cipher = Aes256Gcm::new(key);
let plaintext = cipher.decrypt(nonce, ciphered_data)
.expect("failed to decrypt data");
.map_err(|_| io::Error::new(
io::ErrorKind::InvalidData,
"failed to decrypt data"
))?;
let result = String::from_utf8(plaintext)
.expect("failed to convert vector of bytes to string");
//.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
//match String::from_utf8(content) {
// Ok(s) => Ok(s),
// Err(e) => Err(io::Error::new(io::ErrorKind::InvalidData, e))
//}
.map_err(|_| io::Error::new(
io::ErrorKind::InvalidData,
"failed to convert vector of bytes to string"
))?;
Ok(result)
}
pub fn test_encoded_passphrase(&self, passphrase_encrypted: String) -> io::Result<bool> {
let decrypted = self.decrypt(passphrase_encrypted)?;
// TODO: better way to check error
let decrypted = match self.decrypt(passphrase_encrypted) {
Ok(decrypted) => decrypted,
Err(_) => return Ok(false)
};
Ok(PASSWORD_TEST == decrypted)
}
pub fn get_encoded_test_passphrase(&self) -> String {
pub fn get_encoded_test_passphrase(&self) -> io::Result<String> {
self.encrypt(&PASSWORD_TEST.to_string())
}

11
src/storage.rs

@ -90,13 +90,11 @@ impl 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;
let mut lines = reader.lines();
// TODO: uncomment when innit saving implemented
let passtest = match lines.next() {
Some(line) => line?,
None => return Err(
@ -132,13 +130,10 @@ impl Storage {
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!("Storage db created.");
println!("Initialization complete.");
println!("");
println!("Now it's required to add folder `{}` under git manually.", &*STORAGE_FOLDER);
@ -194,11 +189,11 @@ impl Storage {
.write(true)
.append(false)
.open(&*STORAGE_PATH)?;
writeln!(file, "{}", self.encoder.get_encoded_test_passphrase())?;
writeln!(file, "{}", self.encoder.get_encoded_test_passphrase()?)?;
for item in self.items.iter() {
writeln!(file, "{}", item.id)?;
let content = self.encoder.encrypt(&item.content);
let content = self.encoder.encrypt(&item.content)?;
writeln!(file, "{}", content)?;
}
Ok(())

Loading…
Cancel
Save