UnableToDecodeBase64Message,\r
UnableToDecrypt,\r
UnableToDecodeMessageAsUTF8String,\r
UnableToDecodeBase64Message,\r
UnableToDecrypt,\r
UnableToDecodeMessageAsUTF8String,\r
\r
fn decode_key(key: &str) -> Result<Vec<u8>, KeyError> {\r
match base64::decode(key) {\r
\r
fn decode_key(key: &str) -> Result<Vec<u8>, KeyError> {\r
match base64::decode(key) {\r
/// IV: 16 bytes randomized.\r
/// Mode : CBC.\r
pub fn encrypt(key: &str, plain_text: &str) -> Result<String, EncryptError> {\r
/// IV: 16 bytes randomized.\r
/// Mode : CBC.\r
pub fn encrypt(key: &str, plain_text: &str) -> Result<String, EncryptError> {\r
- match symm::encrypt(symm::Cipher::aes_128_cbc(), &key_as_bytes, Some(&iv), text_as_bytes) {\r
- Ok(t) => t,\r
- Err(_e) => return Err(EncryptError::UnableToEncrypt)\r
- };\r
+ symm::encrypt(symm::Cipher::aes_128_cbc(), &key_as_bytes, Some(&iv), text_as_bytes)\r
+ .map_err(|_e| EncryptError::UnableToEncrypt)?;\r
/// Decrypt the given text with the given key. The key length must be 128 bits encoded in base64.\r
/// Input format: "1" + base_64(<IV> + <hash(message)> + <aes(message)>)\r
pub fn decrypt(key: &str, cipher_text: &str) -> Result<String, DecryptError> {\r
/// Decrypt the given text with the given key. The key length must be 128 bits encoded in base64.\r
/// Input format: "1" + base_64(<IV> + <hash(message)> + <aes(message)>)\r
pub fn decrypt(key: &str, cipher_text: &str) -> Result<String, DecryptError> {\r
\r
// Can't decrypt a message with the wrong version.\r
if !cipher_text.starts_with(consts::CURRENT_MESSAGE_VERSION) { return Err(DecryptError::WrongMessageVersion) }\r
\r
// Can't decrypt a message with the wrong version.\r
if !cipher_text.starts_with(consts::CURRENT_MESSAGE_VERSION) { return Err(DecryptError::WrongMessageVersion) }\r
base64::decode(&cipher_text.as_bytes()[consts::CURRENT_MESSAGE_VERSION.as_bytes().len()..])\r
.map_err(|_e| DecryptError::UnableToDecodeBase64Message)?;\r
\r
base64::decode(&cipher_text.as_bytes()[consts::CURRENT_MESSAGE_VERSION.as_bytes().len()..])\r
.map_err(|_e| DecryptError::UnableToDecodeBase64Message)?;\r
\r
let iv = &cipher_text_bytes[0..16];\r
let hash = &cipher_text_bytes[16..48];\r
let encrypted_message = &cipher_text_bytes[48..];\r
let iv = &cipher_text_bytes[0..16];\r
let hash = &cipher_text_bytes[16..48];\r
let encrypted_message = &cipher_text_bytes[48..];\r