From c1b8a310255a84ba19f293f22dabe66aab8dfa7d Mon Sep 17 00:00:00 2001 From: Anna Liao Date: Mon, 27 Nov 2017 10:56:09 -0700 Subject: [PATCH] create random passwords from a set of allowed characters --- src/basics.md | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/intro.md | 4 ++++ 2 files changed, 54 insertions(+) diff --git a/src/basics.md b/src/basics.md index 83f9ce84..d99e167c 100644 --- a/src/basics.md +++ b/src/basics.md @@ -8,6 +8,8 @@ | [Generate random numbers within a range][ex-rand-range] | [![rand-badge]][rand] | [![cat-science-badge]][cat-science] | | [Generate random numbers with given distribution][ex-rand-dist] | [![rand-badge]][rand] | [![cat-science-badge]][cat-science] | | [Generate random values of a custom type][ex-rand-custom] | [![rand-badge]][rand] | [![cat-science-badge]][cat-science] | +| [Create random passwords from a set of alphanumeric characters][ex-rand-passwd] | [![rand-badge]][rand] | [![cat-os-badge]][cat-os] | +| [Create random passwords from a set of user-defined characters][ex-rand-choose] | [![rand-badge]][rand] | [![cat-os-badge]][cat-os] | | [Run an external command and process stdout][ex-parse-subprocess-output] | [![regex-badge]][regex] | [![cat-os-badge]][cat-os] [![cat-text-processing-badge]][cat-text-processing] | | [Run an external command passing it stdin and check for an error code][ex-parse-subprocess-input] | [![regex-badge]][regex] | [![cat-os-badge]][cat-os] [![cat-text-processing-badge]][cat-text-processing] | | [Run piped external commands][ex-run-piped-external-commands] | [![std-badge]][std] | [![cat-os-badge]][cat-os] | @@ -277,6 +279,52 @@ fn main() { } ``` +[ex-rand-passwd]: #ex-rand-passwd + +## Create random passwords from a set of alphanumeric characters + +[![rand-badge]][rand] [![cat-os-badge]][cat-os] + +Randomly generates a string of given length ASCII characters in the range `A-Z, a-z, 0-9`, with [`gen_ascii_chars`]. + +```rust +extern crate rand; + +use rand::{thread_rng, Rng}; + +fn main() { + let rand_string: String = thread_rng().gen_ascii_chars().take(30).collect(); + println!("{}", rand_string); +} +``` + +[ex-rand-choose]: #ex-rand-choose + +## Create random passwords from a set of user-defined characters + +[![rand-badge]][rand] [![cat-os-badge]][cat-os] + +Randomly generates a string of given length ASCII characters with custom user-defined bytestring, with [`choose`]. + +```rust +extern crate rand; + +use rand::{thread_rng, Rng}; + +fn main() { + const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\ + abcdefghijklmnopqrstuvwxyz\ + 0123456789)(*&^%$#@!~"; + + let mut rng = thread_rng(); + let password: Option = (0..30) + .map(|_| Some(*rng.choose(CHARSET)? as char)) + .collect(); + + println!("{:?}", password); +} +``` + [ex-parse-subprocess-output]: #ex-parse-subprocess-output ## Run an external command and process stdout @@ -1615,6 +1663,7 @@ fn main() { [`BufRead`]: https://doc.rust-lang.org/std/io/trait.BufRead.html [`BufReader`]: https://doc.rust-lang.org/std/io/struct.BufReader.html [`chain_err`]: https://docs.rs/error-chain/*/error_chain/index.html#chaining-errors +[`choose`]: https://docs.rs/rand/*/rand/trait.Rng.html#method.choose [`chrono::format::strftime`]: https://docs.rs/chrono/*/chrono/format/strftime/index.html [`Command`]: https://doc.rust-lang.org/std/process/struct.Command.html [`Datelike`]: https://docs.rs/chrono/*/chrono/trait.Datelike.html @@ -1639,6 +1688,7 @@ fn main() { [`File::try_clone`]: https://doc.rust-lang.org/std/fs/struct.File.html#method.try_clone [`File`]: https://doc.rust-lang.org/std/fs/struct.File.html [`foreign_links`]: https://docs.rs/error-chain/*/error_chain/#foreign-links +[`gen_ascii_chars`]: https://docs.rs/rand/*/rand/trait.Rng.html#method.gen_ascii_chars [`HashMap`]: https://doc.rust-lang.org/std/collections/struct.HashMap.html [`hmac::Signature`]: https://docs.rs/ring/*/ring/hmac/struct.Signature.html [`IndependentSample::ind_sample`]: https://doc.rust-lang.org/rand/rand/distributions/trait.IndependentSample.html#tymethod.ind_sample diff --git a/src/intro.md b/src/intro.md index f8c18901..45602b3f 100644 --- a/src/intro.md +++ b/src/intro.md @@ -26,6 +26,8 @@ community. It needs and welcomes help. For details see | [Generate random numbers within a range][ex-rand-range] | [![rand-badge]][rand] | [![cat-science-badge]][cat-science] | | [Generate random numbers with given distribution][ex-rand-dist] | [![rand-badge]][rand] | [![cat-science-badge]][cat-science] | | [Generate random values of a custom type][ex-rand-custom] | [![rand-badge]][rand] | [![cat-science-badge]][cat-science] | +| [Create random passwords from a set of alphanumeric characters][ex-rand-passwd] | [![rand-badge]][rand] | [![cat-os-badge]][cat-os] | +| [Create random passwords from a set of user-defined characters][ex-rand-choose] | [![rand-badge]][rand] | [![cat-os-badge]][cat-os] | | [Run an external command and process stdout][ex-parse-subprocess-output] | [![regex-badge]][regex] | [![cat-os-badge]][cat-os] [![cat-text-processing-badge]][cat-text-processing] | | [Run an external command passing it stdin and check for an error code][ex-parse-subprocess-input] | [![std-badge]][std] | [![cat-os-badge]][cat-os] | | [Run piped external commands][ex-run-piped-external-commands] | [![std-badge]][std] | [![cat-os-badge]][cat-os] | @@ -218,6 +220,8 @@ community. It needs and welcomes help. For details see [ex-progress-with-range]: net.html#ex-progress-with-range [ex-rand-custom]: basics.html#ex-rand-custom [ex-rand-dist]: basics.html#ex-rand-dist +[ex-rand-choose]: basics.html#ex-rand-choose +[ex-rand-passwd]: basics.html#ex-rand-passwd [ex-rand-range]: basics.html#ex-rand-range [ex-rand]: basics.html#ex-rand [ex-random-file-access]: basics.html#ex-random-file-access