From ce924bb294a22e011110266d57a20f2d12adec8b Mon Sep 17 00:00:00 2001 From: ChristianVisintin Date: Thu, 24 Dec 2020 11:23:31 +0100 Subject: [PATCH] random utils --- src/system/bookmarks_client.rs | 12 ++------ src/utils/mod.rs | 1 + src/utils/random.rs | 52 ++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 src/utils/random.rs diff --git a/src/system/bookmarks_client.rs b/src/system/bookmarks_client.rs index 191877c..95c9829 100644 --- a/src/system/bookmarks_client.rs +++ b/src/system/bookmarks_client.rs @@ -23,17 +23,14 @@ * */ -// Deps -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; +use crate::utils::random::random_alphanumeric_with_len; // Ext -use rand::{distributions::Alphanumeric, thread_rng, Rng}; use std::fs::{OpenOptions, Permissions}; use std::io::{Read, Write}; use std::path::{Path, PathBuf}; @@ -282,12 +279,7 @@ impl BookmarksClient { /// Generate a new AES key and write it to key file fn generate_key(key_file: &Path) -> Result { // Generate 256 bytes (2048 bits) key - let mut rng = thread_rng(); - let key: String = std::iter::repeat(()) - .map(|()| rng.sample(Alphanumeric)) - .map(char::from) - .take(256) - .collect(); + let key: String = random_alphanumeric_with_len(256); // Write file match OpenOptions::new() .create(true) diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 1162e25..5d20a9a 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -27,3 +27,4 @@ pub mod crypto; pub mod fmt; pub mod parser; +pub mod random; diff --git a/src/utils/random.rs b/src/utils/random.rs new file mode 100644 index 0000000..0c17c11 --- /dev/null +++ b/src/utils/random.rs @@ -0,0 +1,52 @@ +//! ## Random +//! +//! `random` is the module which provides utilities for rand + +/* +* +* 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 rand; +// Ext +use rand::{distributions::Alphanumeric, thread_rng, Rng}; + +/// ## random_alphanumeric_with_len +/// +/// Generate a random alphanumeric string with provided length +pub fn random_alphanumeric_with_len(len: usize) -> String { + let mut rng = thread_rng(); + std::iter::repeat(()) + .map(|()| rng.sample(Alphanumeric)) + .map(char::from) + .take(len) + .collect() +} + +#[cfg(test)] +mod tests { + + use super::*; + + #[test] + fn test_utils_random_alphanumeric_with_len() { + assert_eq!(random_alphanumeric_with_len(256).len(), 256); + } +}