From 0923bf5247be623dad59ce60e20f07e0882bb69f Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Wed, 22 May 2024 23:57:47 +0200 Subject: [PATCH 1/4] feat(rand): add rand option to FormatOptions The user can now specify how many rand values to add to the list. The Cli needed some minor changes. Refs: #21 #19 --- Cargo.toml | 1 + src/format.rs | 16 ++++++++++++++++ src/main.rs | 22 +++++++++++++++++++--- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d206a7c..109a5a4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,4 +19,5 @@ clap = { version = "4.5.4", features = ["derive"] } fast32 = "1.0.2" libpt = { version = "0.5.1", features = ["bintols"], default-features = false } num = "0.4.3" +rand = "0.8.5" diff --git a/src/format.rs b/src/format.rs index ebc59d1..4396ba0 100644 --- a/src/format.rs +++ b/src/format.rs @@ -86,6 +86,11 @@ pub struct FormatOptions { #[arg(short = 's', long)] /// format to base64 base64: bool, + #[arg(short = 'r', long, default_value_t = 0)] + /// output random numbers + /// + /// Add a user defined amount of cryptographically pseudorandom numbers to the number list. + rand: usize, #[arg(short = 'z', long)] /// format to base32 base32: bool, @@ -183,6 +188,16 @@ impl FormatOptions { pub fn push_number(&mut self, value: NumberType) { self.numbers.push(value) } + + /// get rand + pub fn rand(&self) -> usize { + self.rand + } + + /// set amount of extra random numbers manually + pub fn set_rand(&mut self, rand: usize) { + self.rand = rand; + } } impl Default for FormatOptions { @@ -197,6 +212,7 @@ impl Default for FormatOptions { base64: false, dec: false, numbers: vec![], + rand: 0 } } } diff --git a/src/main.rs b/src/main.rs index ca7e5ab..52473a9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,9 +10,9 @@ use clap::{CommandFactory, Parser}; mod format; use format::*; -use numf::format::numf_parser; +use numf::format::{numf_parser, NumberType}; -fn main() { +fn main() -> anyhow::Result<()> { // try to read from stdin first, appending the numbers we read to the FormatOptions let mut options = FormatOptions::parse(); let mut stdin_nums = Vec::new(); @@ -23,6 +23,7 @@ fn main() { let whole: String = match String::from_utf8(stdin_nums) { Ok(r) => r, Err(e) => { + eprintln!("{}", FormatOptions::command().render_usage()); eprintln!("stdin for this program only accepts text: {e:#?}"); exit(1); } @@ -32,6 +33,7 @@ fn main() { let number = match numf_parser(s) { Ok(n) => n, Err(e) => { + eprintln!("{}", FormatOptions::command().render_usage()); eprintln!("could not parse number from stdin: {e:#?}"); exit(2); } @@ -40,14 +42,27 @@ fn main() { } } Err(e) => { + eprintln!("{}", FormatOptions::command().render_usage()); eprintln!("could not read from stdin: {e:#?}"); exit(2); } }; } + if options.rand() > 0 { + use rand::prelude::*; + let mut rand = rand::rngs::OsRng; + for _i in 0..options.rand() { + let mut by: [u8; 16] = [0; 16]; + rand.fill_bytes(&mut by); + let n: NumberType = libpt::bintols::join::array_to_unsigned(&by)?; + options.push_number(n); + } + } + if options.numbers().is_empty() { - format!("{}", FormatOptions::command().render_usage()); + eprintln!("{}", FormatOptions::command().render_usage()); + eprintln!("no numbers have been provided"); exit(1); } @@ -59,4 +74,5 @@ fn main() { for o in out { println!("{o}") } + Ok(()) } From 950c26a35caf4ff1ab99fd03663b73f19d2d0cad Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Wed, 22 May 2024 22:00:38 +0000 Subject: [PATCH 2/4] automatic cargo CI changes --- src/format.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/format.rs b/src/format.rs index 4396ba0..72837cc 100644 --- a/src/format.rs +++ b/src/format.rs @@ -212,7 +212,7 @@ impl Default for FormatOptions { base64: false, dec: false, numbers: vec![], - rand: 0 + rand: 0, } } } From 8fbc612f5ae334d8ea82a43dc990a6e10f4c232d Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Thu, 23 May 2024 00:20:07 +0200 Subject: [PATCH 3/4] feat(rand): #19 #21 add max rand and use number parser --- src/format.rs | 26 +++++++++++++++++++++----- src/main.rs | 7 ++----- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/format.rs b/src/format.rs index 4396ba0..184a236 100644 --- a/src/format.rs +++ b/src/format.rs @@ -86,11 +86,16 @@ pub struct FormatOptions { #[arg(short = 's', long)] /// format to base64 base64: bool, - #[arg(short = 'r', long, default_value_t = 0)] + #[arg(short = 'r', long, default_value_t = 0, value_parser=numf_parser::)] /// output random numbers /// /// Add a user defined amount of cryptographically pseudorandom numbers to the number list. - rand: usize, + rand: NumberType, + #[arg(long, default_value_t = NumberType::MAX, value_parser=numf_parser::)] + /// max for the random numbers + /// + /// Generated numbers will not be lower than this. Only has an effect with --rand set. + rand_max: NumberType, #[arg(short = 'z', long)] /// format to base32 base32: bool, @@ -190,14 +195,24 @@ impl FormatOptions { } /// get rand - pub fn rand(&self) -> usize { + pub fn rand(&self) -> NumberType { self.rand } /// set amount of extra random numbers manually - pub fn set_rand(&mut self, rand: usize) { + pub fn set_rand(&mut self, rand: NumberType) { self.rand = rand; } + + /// get highes allowed random value + pub fn rand_max(&self) -> NumberType { + self.rand_max + } + + /// set highes allowed random value + pub fn set_rand_max(&mut self, rand_max: NumberType) { + self.rand_max = rand_max; + } } impl Default for FormatOptions { @@ -212,7 +227,8 @@ impl Default for FormatOptions { base64: false, dec: false, numbers: vec![], - rand: 0 + rand: 0, + rand_max: NumberType::MAX } } } diff --git a/src/main.rs b/src/main.rs index 52473a9..a8eabaa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,7 @@ use clap::{CommandFactory, Parser}; mod format; use format::*; -use numf::format::{numf_parser, NumberType}; +use numf::format::numf_parser; fn main() -> anyhow::Result<()> { // try to read from stdin first, appending the numbers we read to the FormatOptions @@ -53,10 +53,7 @@ fn main() -> anyhow::Result<()> { use rand::prelude::*; let mut rand = rand::rngs::OsRng; for _i in 0..options.rand() { - let mut by: [u8; 16] = [0; 16]; - rand.fill_bytes(&mut by); - let n: NumberType = libpt::bintols::join::array_to_unsigned(&by)?; - options.push_number(n); + options.push_number(rand.gen_range(0..options.rand_max())); } } From 93a27564c6735c53fb0e67e9b7edd9387deedae7 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Wed, 22 May 2024 22:21:49 +0000 Subject: [PATCH 4/4] automatic cargo CI changes --- src/format.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/format.rs b/src/format.rs index 184a236..6bc0ae3 100644 --- a/src/format.rs +++ b/src/format.rs @@ -228,7 +228,7 @@ impl Default for FormatOptions { dec: false, numbers: vec![], rand: 0, - rand_max: NumberType::MAX + rand_max: NumberType::MAX, } } }