|
|
|
@ -1,38 +1,55 @@
|
|
|
|
|
use base64::prelude::*; // TODO: use hex instead
|
|
|
|
|
|
|
|
|
|
use std::io; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static PASSWORD_TEST: &str = "MyPasswordStorage"; // will be added with nonce for storing each time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub struct Encoder { |
|
|
|
|
// will be stored with padding 32 bytes
|
|
|
|
|
passphrase: String |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl Encoder { |
|
|
|
|
pub fn from(passphrase: String) -> Encoder { |
|
|
|
|
Encoder { passphrase } |
|
|
|
|
pub fn from(passphrase: &String) -> Encoder { |
|
|
|
|
// 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
|
|
|
|
|
pub fn encode(&self, line: String) -> String { |
|
|
|
|
// TODO: use passphrasee to encode
|
|
|
|
|
BASE64_STANDARD.encode(line) |
|
|
|
|
pub fn encode(&self, line: &String) -> String { |
|
|
|
|
return line.clone(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 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)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//.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))
|
|
|
|
|
//}
|
|
|
|
|
return Ok(line.clone()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn test_encoded_passphrase(&self, test_passphrase_encoded: String) -> bool { |
|
|
|
|
self.passphrase == test_passphrase_encoded |
|
|
|
|
pub fn test_encoded_passphrase(&self, piassphrase_encoded: String) -> bool { |
|
|
|
|
// TODO: implement
|
|
|
|
|
//self.passphrase == passphrase_encoded
|
|
|
|
|
// Encoder::get_encoded_test_passphrase();
|
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn get_encoded_test_passphrase(&self) -> String { |
|
|
|
|
// TODO: encode SALT const with passphrase
|
|
|
|
|
// TODO: use this
|
|
|
|
|
self.passphrase.clone() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|