Browse Source

encoder

fix-typo
Coin de Gamma 3 months ago
parent
commit
22a25a3565
  1. 39
      src/encoder.rs
  2. 1
      src/main.rs
  3. 40
      src/storage.rs

39
src/encoder.rs

@ -0,0 +1,39 @@
use base64::prelude::*; // TODO: use hex instead
use std::io;
pub struct Encoder {
passphrase: String
}
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
BASE64_STANDARD.encode(line)
}
// TODO: review error type
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) {
Ok(s) => Ok(s),
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()
}
}

1
src/main.rs

@ -1,3 +1,4 @@
mod encoder;
mod storage;
mod editor;

40
src/storage.rs

@ -1,4 +1,7 @@
use base64::prelude::*;
use crate::encoder;
use encoder::Encoder;
use once_cell::sync::Lazy;
@ -68,41 +71,6 @@ impl Ord for Item {
}
}
struct Encoder {
passphrase: String
}
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
BASE64_STANDARD.encode(line)
}
// TODO: review error type
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) {
Ok(s) => Ok(s),
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 {
items: HashSet::<Item>,
encoder: Encoder

Loading…
Cancel
Save