You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.0 KiB
40 lines
1.0 KiB
3 months ago
|
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()
|
||
|
}
|
||
|
|
||
|
}
|