X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=src%2Fcrypto.rs;h=a7aa5b98f49faec4b47fd63b112f41eff0d1613e;hb=c1a5fe1d8e733d744a94acf278903133e2a72115;hp=7e707d02a218c64fc99034c8f1ac9205ccac0635;hpb=51aa7c917b3ecfb80d06d5a06cc9f553b8bc91c6;p=rup.git diff --git a/src/crypto.rs b/src/crypto.rs index 7e707d0..a7aa5b9 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -33,8 +33,7 @@ fn decode_key(key: &str) -> Result, KeyError> { } /// Encrypt the given text with the given key. The key length must be 128 bits encoded in base64. -/// Ouput format: -/// Format "1" + base_64( + + ) +/// Ouput format: "1" + base_64( + + ) /// IV: 16 bytes randomized. /// Mode : CBC. pub fn encrypt(key: &str, plain_text: &str) -> Result { @@ -44,10 +43,8 @@ pub fn encrypt(key: &str, plain_text: &str) -> Result { let iv = rand::thread_rng().gen::<[u8; 16]>(); let cipher_text = - match symm::encrypt(symm::Cipher::aes_128_cbc(), &key_as_bytes, Some(&iv), text_as_bytes) { - Ok(t) => t, - Err(_e) => return Err(EncryptError::UnableToEncrypt) - }; + symm::encrypt(symm::Cipher::aes_128_cbc(), &key_as_bytes, Some(&iv), text_as_bytes) + .map_err(|_e| EncryptError::UnableToEncrypt)?; let hash_text = sha256(&text_as_bytes); @@ -59,7 +56,8 @@ pub fn encrypt(key: &str, plain_text: &str) -> Result { Ok(String::from("1") + &base64::encode(&result)) } -/// TODO: return a Result +/// Decrypt the given text with the given key. The key length must be 128 bits encoded in base64. +/// Input format: "1" + base_64( + + ) pub fn decrypt(key: &str, cipher_text: &str) -> Result { let key_as_bytes = decode_key(key).map_err(|e| DecryptError::KeyError(e))?;