mirror of
https://github.com/veeso/termscp.git
synced 2025-12-07 09:36:00 -08:00
Crypto as utility module
This commit is contained in:
@@ -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<String, SerializerError> {
|
||||
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,
|
||||
|
||||
62
src/utils/crypto.rs
Normal file
62
src/utils/crypto.rs
Normal file
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
// 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<String, magic_crypt::MagicCryptError> {
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -24,5 +24,6 @@
|
||||
*/
|
||||
|
||||
// modules
|
||||
pub mod crypto;
|
||||
pub mod fmt;
|
||||
pub mod parser;
|
||||
|
||||
Reference in New Issue
Block a user