\r
fn decode_key(key: &str) -> Result<Vec<u8>, KeyError> {\r
match base64::decode(key) {\r
- Ok(k) => if k.len() != 16 { return Err(KeyError::WrongKeyLength) } else { Ok(k) },\r
+ Ok(k) => if k.len() != 16 { Err(KeyError::WrongKeyLength) } else { Ok(k) },\r
Err(_e) => Err(KeyError::UnableToDecodeBase64Key)\r
}\r
}\r
/// IV: 16 bytes randomized.\r
/// Mode : CBC.\r
pub fn encrypt(key: &str, plain_text: &str) -> Result<String, EncryptError> {\r
- let key_as_bytes = decode_key(key).map_err(|e| EncryptError::KeyError(e))?;\r
+ let key_as_bytes = decode_key(key).map_err(EncryptError::KeyError)?;\r
\r
let text_as_bytes = plain_text.as_bytes();\r
let iv = rand::thread_rng().gen::<[u8; 16]>();\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
- let key_as_bytes = decode_key(key).map_err(|e| DecryptError::KeyError(e))?;\r
+ let key_as_bytes = decode_key(key).map_err(DecryptError::KeyError)?;\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
use listenfd::ListenFd;
use actix_files as fs;
-use actix_web::{web, middleware, App, HttpServer, HttpResponse, Responder, Result, web::Query};
+use actix_web::{web, middleware, App, HttpServer, HttpResponse, Result, web::Query};
use askama::Template;
use std::io::prelude::*;
fn read_key() -> String {
let mut key = String::new();
File::open(consts::FILE_KEY)
- .expect(&format!("Failed to open key file: {}", consts::FILE_KEY))
+ .unwrap_or_else(|_| panic!("Failed to open key file: {}", consts::FILE_KEY))
.read_to_string(&mut key)
- .expect(&format!("Failed to read key file: {}", consts::FILE_KEY));
+ .unwrap_or_else(|_| panic!("Failed to read key file: {}", consts::FILE_KEY));
String::from(
percent_encoding::percent_decode(key.replace('\n', "").as_bytes())
.decode_utf8()
- .expect(&format!("Failed to decode key file: {}", consts::FILE_KEY))
+ .unwrap_or_else(|_| panic!("Failed to decode key file: {}", consts::FILE_KEY))
)
}
println!("Starting RUP as web server...");
let config: Config = {
- let f = File::open(consts::FILE_CONF).expect(&format!("Failed to open configuration file {}", consts::FILE_CONF));
+ let f = File::open(consts::FILE_CONF).unwrap_or_else(|_| panic!("Failed to open configuration file {}", consts::FILE_CONF));
match from_reader(f) {
Ok(c) => c,
Err(e) => panic!("Failed to load config: {}", e)
server.run()
}
-fn process_args(key: &String) -> bool {
+fn process_args(key: &str) -> bool {
fn print_usage() {
println!("Usage:");
println!(" {} [--help] [--encrypt <plain-text>|--decrypt <cipher-text>]", get_exe_name());
print_usage();
return true
} else if let Some((position_arg_encrypt, _)) = args.iter().find_position(|arg| arg == &"--encrypt") {
- match args.iter().nth(position_arg_encrypt + 1) {
+ match args.get(position_arg_encrypt + 1) {
Some(mess_to_encrypt) => {
match crypto::encrypt(&key, mess_to_encrypt) {
Ok(encrypted_mess) => {
return true
} else if let Some((position_arg_decrypt, _)) = args.iter().find_position(|arg| arg == &"--decrypt") {
- match args.iter().nth(position_arg_decrypt + 1) {
+ match args.get(position_arg_decrypt + 1) {
Some(cipher_text) => {
let cipher_text_decoded = percent_encoding::percent_decode(cipher_text.as_bytes()).decode_utf8().expect("Unable to decode encoded cipher text");
match crypto::decrypt(&key, &cipher_text_decoded) {
return true
}
- return false
+ false
}