From 1d9d6e95d07d7c31ee05066257296682ed5c8f5c Mon Sep 17 00:00:00 2001 From: Unbewohnte Date: Tue, 10 Aug 2021 11:03:13 +0300 Subject: [PATCH] Added a psgen --- psgen/.gitignore | 1 + psgen/Cargo.lock | 85 +++++++++++++++++++++++++++++++++++++++++++++++ psgen/Cargo.toml | 10 ++++++ psgen/LICENSE | 9 +++++ psgen/src/main.rs | 51 ++++++++++++++++++++++++++++ 5 files changed, 156 insertions(+) create mode 100644 psgen/.gitignore create mode 100644 psgen/Cargo.lock create mode 100644 psgen/Cargo.toml create mode 100644 psgen/LICENSE create mode 100644 psgen/src/main.rs diff --git a/psgen/.gitignore b/psgen/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/psgen/.gitignore @@ -0,0 +1 @@ +/target diff --git a/psgen/Cargo.lock b/psgen/Cargo.lock new file mode 100644 index 0000000..31e7028 --- /dev/null +++ b/psgen/Cargo.lock @@ -0,0 +1,85 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "libc" +version = "0.2.98" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "320cfe77175da3a483efed4bc0adc1968ca050b098ce4f2f1c13a56626128790" + +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + +[[package]] +name = "psgen" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "rand" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7" +dependencies = [ + "rand_core", +] + +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" diff --git a/psgen/Cargo.toml b/psgen/Cargo.toml new file mode 100644 index 0000000..af33b3e --- /dev/null +++ b/psgen/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "psgen" +version = "0.1.0" +edition = "2018" +authors = ["Unbewohnte | Nikolay Kasyanov"] + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = ">=0.8.4" \ No newline at end of file diff --git a/psgen/LICENSE b/psgen/LICENSE new file mode 100644 index 0000000..a08fc5c --- /dev/null +++ b/psgen/LICENSE @@ -0,0 +1,9 @@ +The MIT License (MIT) + +Copyright © 2021 Unbewohne | Nikolay Kasyanov + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/psgen/src/main.rs b/psgen/src/main.rs new file mode 100644 index 0000000..f3c473b --- /dev/null +++ b/psgen/src/main.rs @@ -0,0 +1,51 @@ +use rand::Rng; + +// returns pseudo-randomly generated password from chars +fn generate_password(chars: String, length: u32) -> String { + let mut rng = rand::thread_rng(); + + let mut password: String = String::with_capacity(length as usize); + for _ in 0..length { + // wtf + let random_index:usize = rng.gen_range(0..chars.len() - 1); + + let random_char:char = chars.chars().nth(random_index).unwrap(); + + password += random_char.to_string().as_str(); + } + + return password; +} + +fn main() { + // get command line arguments + let args: Vec = std::env::args().collect(); + + let mut pass_length: u8 = 8; // default password length + + // check for given arguments + if args.len() > 1 { + // has some arguments -> get the [1] one and check for an error + let pass_length_res = args[1].parse::(); + + match pass_length_res { + Ok(n) => { + // check for 0 + if n != 0 { + // not 0 -> safely switch default pass_length value with a new one + pass_length = n; + } + }, + Err(e) => println!("Please, specify an u8 (1..255) integer as the first argument next time: {}",e), + } + } + + // characters, from which we will construct a password + let chars: String = String::from("ABCDEFGHIJKLMNOPQRSUVWXYZabcdefghijklmnopqrstuvwxyz1234567890{}[]()!@#$%^&*~"); + + // generate password + let generated_password: String = generate_password(chars, pass_length as u32); + + // and print it + println!("{}",generated_password); +}