mirror of
https://github.com/veeso/termscp.git
synced 2026-09-29 23:41:19 -07:00
Crypto as utility module
This commit is contained in:
@@ -24,16 +24,15 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// Deps
|
// Deps
|
||||||
extern crate magic_crypt;
|
|
||||||
extern crate rand;
|
extern crate rand;
|
||||||
|
|
||||||
// Local
|
// Local
|
||||||
use crate::bookmarks::serializer::BookmarkSerializer;
|
use crate::bookmarks::serializer::BookmarkSerializer;
|
||||||
use crate::bookmarks::{Bookmark, SerializerError, SerializerErrorKind, UserHosts};
|
use crate::bookmarks::{Bookmark, SerializerError, SerializerErrorKind, UserHosts};
|
||||||
use crate::filetransfer::FileTransferProtocol;
|
use crate::filetransfer::FileTransferProtocol;
|
||||||
|
use crate::utils::crypto;
|
||||||
use crate::utils::fmt::fmt_time;
|
use crate::utils::fmt::fmt_time;
|
||||||
// Ext
|
// Ext
|
||||||
use magic_crypt::MagicCryptTrait;
|
|
||||||
use rand::{distributions::Alphanumeric, Rng};
|
use rand::{distributions::Alphanumeric, Rng};
|
||||||
use std::fs::{OpenOptions, Permissions};
|
use std::fs::{OpenOptions, Permissions};
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
@@ -373,16 +372,14 @@ impl BookmarksClient {
|
|||||||
///
|
///
|
||||||
/// Encrypt provided string using AES-128. Encrypted buffer is then converted to BASE64
|
/// Encrypt provided string using AES-128. Encrypted buffer is then converted to BASE64
|
||||||
fn encrypt_str(&self, txt: &str) -> String {
|
fn encrypt_str(&self, txt: &str) -> String {
|
||||||
let crypter = new_magic_crypt!(self.key.clone(), 128);
|
crypto::aes128_b64_crypt(self.key.as_str(), txt)
|
||||||
crypter.encrypt_str_to_base64(txt.to_string())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// ### decrypt_str
|
/// ### decrypt_str
|
||||||
///
|
///
|
||||||
/// Decrypt provided string using AES-128
|
/// Decrypt provided string using AES-128
|
||||||
fn decrypt_str(&self, secret: &str) -> Result<String, SerializerError> {
|
fn decrypt_str(&self, secret: &str) -> Result<String, SerializerError> {
|
||||||
let crypter = new_magic_crypt!(self.key.clone(), 128);
|
match crypto::aes128_b64_decrypt(self.key.as_str(), secret) {
|
||||||
match crypter.decrypt_base64_to_string(secret.to_string()) {
|
|
||||||
Ok(txt) => Ok(txt),
|
Ok(txt) => Ok(txt),
|
||||||
Err(err) => Err(SerializerError::new_ex(
|
Err(err) => Err(SerializerError::new_ex(
|
||||||
SerializerErrorKind::SyntaxError,
|
SerializerErrorKind::SyntaxError,
|
||||||
|
|||||||
@@ -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
|
// modules
|
||||||
|
pub mod crypto;
|
||||||
pub mod fmt;
|
pub mod fmt;
|
||||||
pub mod parser;
|
pub mod parser;
|
||||||
|
|||||||
Reference in New Issue
Block a user