From e761a908268a02ace46a19bd43b247c1f474db47 Mon Sep 17 00:00:00 2001 From: ChristianVisintin Date: Tue, 22 Dec 2020 17:34:52 +0100 Subject: [PATCH] Crypto as utility module --- src/system/bookmarks_client.rs | 9 ++--- src/utils/crypto.rs | 62 ++++++++++++++++++++++++++++++++++ src/utils/mod.rs | 1 + 3 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 src/utils/crypto.rs diff --git a/src/system/bookmarks_client.rs b/src/system/bookmarks_client.rs index 00803f0..4119ac4 100644 --- a/src/system/bookmarks_client.rs +++ b/src/system/bookmarks_client.rs @@ -24,16 +24,15 @@ */ // Deps -extern crate magic_crypt; extern crate rand; // Local use crate::bookmarks::serializer::BookmarkSerializer; use crate::bookmarks::{Bookmark, SerializerError, SerializerErrorKind, UserHosts}; use crate::filetransfer::FileTransferProtocol; +use crate::utils::crypto; use crate::utils::fmt::fmt_time; // Ext -use magic_crypt::MagicCryptTrait; use rand::{distributions::Alphanumeric, Rng}; use std::fs::{OpenOptions, Permissions}; use std::io::{Read, Write}; @@ -373,16 +372,14 @@ impl BookmarksClient { /// /// Encrypt provided string using AES-128. Encrypted buffer is then converted to BASE64 fn encrypt_str(&self, txt: &str) -> String { - let crypter = new_magic_crypt!(self.key.clone(), 128); - crypter.encrypt_str_to_base64(txt.to_string()) + crypto::aes128_b64_crypt(self.key.as_str(), txt) } /// ### decrypt_str /// /// Decrypt provided string using AES-128 fn decrypt_str(&self, secret: &str) -> Result { - let crypter = new_magic_crypt!(self.key.clone(), 128); - match crypter.decrypt_base64_to_string(secret.to_string()) { + match crypto::aes128_b64_decrypt(self.key.as_str(), secret) { Ok(txt) => Ok(txt), Err(err) => Err(SerializerError::new_ex( SerializerErrorKind::SyntaxError, diff --git a/src/utils/crypto.rs b/src/utils/crypto.rs new file mode 100644 index 0000000..defb1de --- /dev/null +++ b/src/utils/crypto.rs @@ -0,0 +1,62 @@ +//! ## Crypto +//! +//! `crypto` is the module which provides utilities for crypting + +/* +* +* Copyright (C) 2020 Christian Visintin - christian.visintin1997@gmail.com +* +* This file is part of "TermSCP" +* +* TermSCP is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* TermSCP is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with TermSCP. If not, see . +* +*/ + +// Deps +extern crate magic_crypt; + +// Ext +use magic_crypt::MagicCryptTrait; + +/// ### aes128_b64_crypt +/// +/// Crypt a string using AES128; output is returned as a BASE64 string +pub fn aes128_b64_crypt(key: &str, input: &str) -> String { + let crypter = new_magic_crypt!(key.to_string(), 128); + crypter.encrypt_str_to_base64(input.to_string()) +} + +/// ### aes128_b64_decrypt +/// +/// Decrypt a string using AES128 +pub fn aes128_b64_decrypt(key: &str, secret: &str) -> Result { + let crypter = new_magic_crypt!(key.to_string(), 128); + crypter.decrypt_base64_to_string(secret.to_string()) +} + +#[cfg(test)] +mod tests { + + use super::*; + + #[test] + fn test_utils_crypto_aes128() { + let key: &str = "MYSUPERSECRETKEY"; + let input: &str = "Hello world!"; + let secret: String = aes128_b64_crypt(&key, input); + assert_eq!(secret.as_str(), "z4Z6LpcpYqBW4+bkIok+5A=="); + assert_eq!(aes128_b64_decrypt(key, secret.as_str()).ok().unwrap().as_str(), input); + } + +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 5a3dd3f..1162e25 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -24,5 +24,6 @@ */ // modules +pub mod crypto; pub mod fmt; pub mod parser;