nu_scripts/sourced/misc/password_generator
Antoine Stevan ed9165fda1
fix removed commands (#645)
related to
- https://github.com/nushell/nushell/pull/10567
- https://github.com/nushell/nushell/pull/10668
- https://github.com/nushell/nushell/pull/10568

this PR removes mentions to removed commands from
https://github.com/nushell/nushell/pull/10567,
https://github.com/nushell/nushell/pull/10668 and
https://github.com/nushell/nushell/pull/10568.

the main change has been introduced with
```nushell
sd 'random integer' 'random int' **/*.nu
```

running `rg "$nothing|random integer|to xml .* --pretty"` gives
- before
```
modules/random-list/random-list.nu
85:# Generate a random integer list.
95:        random integer $range

modules/fun/wordle.nu
11:  let word = ($words | get (random integer 0..($words | length)) | get column1)

benchmarks/random-bytes.nu
5:        | each { random integer }

sourced/misc/password_generator/ReadMe.md
84:Obviously you can just use the `random chars` or `random integers` commands but I like to have words I can read in my passwords, and I think those generated by this script have sufficient entropy.

sourced/misc/password_generator/nupass.nu
43:  let random_numbers = (1..$words | par-each { |i| (random integer 0..99) } --threads $threads)
71:    return (0..($words - 1) | each { |it| (random integer 0..99 | into string) + ($random_words | get $it) } | reduce { |it
, acc| $acc + $it })
92:    | get (random integer 1..($numlines))
99:        let rint = (random integer 1..4)
119:    | get (random integer 0..($symbolcharslen - 1))
```
- after
```
modules/random-list/random-list.nu
85:# Generate a random integer list.

sourced/misc/password_generator/ReadMe.md
84:Obviously you can just use the `random chars` or `random integers` commands but I like to have words I can read in my passwords, and I think those generated by this script have sufficient entropy.
```
2023-10-19 19:35:23 +02:00
..
genpass-dict-jp refactor: (#418) 2023-04-25 17:56:25 -05:00
nupass.nu fix removed commands (#645) 2023-10-19 19:35:23 +02:00
ReadMe.md Nupass par each threads 20230504 (#477) 2023-05-04 08:00:38 -05:00

Nushell Password Generator "nupass"

This nushell command randomly retrieves a specified number of words from a dictionary file (English with Japanese words added by @rickcogley) less than or equal to a given parameter's length, formats the words randomly with capitalization, then separates the words with some random symbols and numbers to return a password.

To use:

  1. Get the dictionary file to your system using nushell's http:
http get https://raw.githubusercontent.com/RickCogley/jpassgen/master/genpass-dict-jp.txt | save genpass-dict-jp

...which has also been included in this folder for convenience.

  1. Confirm your $env.NU_LIB_DIRS location, and copy the below script 2. nupass.nu there as nupass.nu.
  2. Set the script as executable like chmod +x nupass.nu
  3. Specify the dictionary file's location in the script:
let dictfile = $"/path/to/my/genpass-dict-jp"
  1. In the main function's flags section, confirm and edit the default symbols list, diceware delimiter and threads for par-each (double the number of your CPU cores seems to be a good sweet spot):
--symbols (-s): string = "!@#$%^&()_-+[]{}" # Symbols to use in password
--delimiter (-m): string = "-" # Delimiter for diceware
--threads (-t): int = 16  # Number of threads to use in par-each
  1. Load the script with use in your config.nu, something like:
use nupass.nu

(you can specify the path as use /path/to/nupass.nu if you're not taking advantage of $env.NU_LIB_DIRS)

Reload nu, then run it to test:

nupass -h
nupass 5
nupass 6 --debug
nupass 8 -v diceware
nupass 8 -v diceware -m _
nupass 4 -v mixnmatch
nupass 6 -v alphanum
nupass 5 -l 8 

Testing

If you're making changes to the script while testing, you can just re-source the script by doing:

use nupass.nu

... which will reload the latest you have saved.

From nu version 0.79.1, you can use the standard library, and use its bench command to do a benchmark. Load the standard library by adding use std in your env.nu, reload, then assuming nupass.nu is in your path, you can benchmark like so:

std bench --rounds 10 --verbose {nupass 10}
std bench --rounds 10 --verbose {nupass 100 -v diceware}
std bench --rounds 10 --verbose {nupass 1000 -v mixnmatch}

If you change the par-each to each in the main list builders for instance, you'll see a significant performance hit. When I benchmarked nupass 100, using just each took 7 sec per round, whereas changing to par-each dropped that to about 1 sec per round.

You can tweak it a little further by setting the threads for par-each.

std bench --rounds 10 --verbose {nupass 100 -v diceware -t 8}
std bench --rounds 10 --verbose {nupass 100 -v diceware -t 16}
std bench --rounds 10 --verbose {nupass 100 -v diceware -t 32}
image

Caveats

I've been scripting for quite a long time, but not in nu. Input appreciated to make this more nu-esque! The script is in a decent place as of the 20230501 version.

Obviously you can just use the random chars or random integers commands but I like to have words I can read in my passwords, and I think those generated by this script have sufficient entropy.

This command doesn't let you specify a precise length.

Acknowledgements

Thanks everyone on Discord for putting up with and answering my nubie questions @amtoine, @fdncred, @jelle, @sygmei, @kubouch and for the feedback after try number 1.

image