mirror of
https://github.com/RGBCube/nu_scripts
synced 2025-08-01 22:57:46 +00:00
Add nupass.nu password generator (#450)
This commit is contained in:
parent
97fac19e33
commit
822005fca6
3 changed files with 11751 additions and 0 deletions
66
misc/password_generator/ReadMe.md
Normal file
66
misc/password_generator/ReadMe.md
Normal file
|
@ -0,0 +1,66 @@
|
|||
# Nushell Password Generator "nupass"
|
||||
|
||||
This nushell command randomly retrieves three 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.
|
||||
|
||||
2. Confirm your `$env.NU_LIB_DIRS` location, and copy the below script `2. nupass.nu` there as `nupass.nu`.
|
||||
3. Set the script as executable like `chmod +x nupass.nu`
|
||||
4. Specify the dictionary file's location in the script:
|
||||
|
||||
```
|
||||
let dictfile = $"/path/to/my/genpass-dict-jp"
|
||||
```
|
||||
|
||||
5. Confirm the symbols are what you want to use, and edit to taste:
|
||||
|
||||
```
|
||||
let symbolchars = "!@#$%^&*()_-+[]"
|
||||
```
|
||||
|
||||
6. 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 3 -d
|
||||
```
|
||||
|
||||
### Testing
|
||||
|
||||
If you're making changes to the script while testing, just re-source the script by doing:
|
||||
|
||||
`use nupass.nu`
|
||||
|
||||
... which will reload the latest save.
|
||||
|
||||
### Caveats
|
||||
|
||||
I've been scripting for quite a long time, but not in nu. Input appreciated to make this more nu-esque!
|
||||
|
||||
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, and for the feedback after try number 1.
|
||||
|
||||
<img width="1041" alt="image" src="https://user-images.githubusercontent.com/512328/231930563-4da63f8c-d9de-4620-901e-a39ed32b049f.png">
|
11593
misc/password_generator/genpass-dict-jp
Normal file
11593
misc/password_generator/genpass-dict-jp
Normal file
File diff suppressed because it is too large
Load diff
92
misc/password_generator/nupass.nu
Executable file
92
misc/password_generator/nupass.nu
Executable file
|
@ -0,0 +1,92 @@
|
|||
# Script to generate a password from a dictionary file
|
||||
# Author: @rickcogley
|
||||
# Thanks: @amtoine, @fdncred, @jelle
|
||||
# Updates: 20230415 - initial version
|
||||
# 20230416 - added @amtoine's slick probabilistic "random decimal" char capitalization
|
||||
# 20230417 - added script duration output in debug block
|
||||
|
||||
#======= NUPASS PASSWORD GENERATOR =======
|
||||
# Generate password of 3 dictionary file words, numbers and symbols
|
||||
export def main [
|
||||
word_length: int = 4 # Max length of 3 words in password
|
||||
--debug (-d) # Include debug info
|
||||
] {
|
||||
##### Main function #####
|
||||
# Get dictionary file:
|
||||
# http get https://raw.githubusercontent.com/RickCogley/jpassgen/master/genpass-dict-jp.txt | save genpass-dict-jp
|
||||
# Set the path:
|
||||
let dictfile = $"/usr/local/bin/genpass-dict-jp"
|
||||
let starttime = (date now)
|
||||
|
||||
# Find number of lines with strings less than or equal to the supplied length
|
||||
let num_lines = (open ($dictfile) | lines | wrap word | upsert len {|it| $it.word | split chars | length} | where len <= ($word_length) | length)
|
||||
|
||||
# Get random words from dictionary file
|
||||
let randword1 = ($dictfile | get-random-word $word_length $num_lines | random-format-word)
|
||||
let randword2 = ($dictfile | get-random-word $word_length $num_lines | random-format-word)
|
||||
let randword3 = ($dictfile | get-random-word $word_length $num_lines | random-format-word)
|
||||
|
||||
# Get some symbols to sprinkle like salt bae
|
||||
let symbol_chars = "!@#$%^&*()_-+[]"
|
||||
let symb1 = (get-random-symbol $symbol_chars)
|
||||
let symb2 = (get-random-symbol $symbol_chars)
|
||||
let symb3 = (get-random-symbol $symbol_chars)
|
||||
let symb4 = (get-random-symbol $symbol_chars)
|
||||
|
||||
# Print some vars if debug flag is set
|
||||
if $debug {
|
||||
print $"(ansi bg_red) ====== DEBUG INFO ====== (ansi reset)"
|
||||
print $"(ansi bg_blue) 🔔 Number of lines in dict with words under ($word_length) chars: (ansi reset)"
|
||||
print $num_lines
|
||||
print $"(ansi bg_blue) 🔔 Words from randomly selected lines: (ansi reset)"
|
||||
print $randword1 $randword2 $randword3
|
||||
print $"(ansi bg_blue) 🔔 Randomly selected symbols: (ansi reset)"
|
||||
print $symb1 $symb2 $symb3 $symb4
|
||||
let endtime = (date now)
|
||||
print $"(ansi bg_green) 🔔 Generated password in ($endtime - $starttime): (ansi reset)"
|
||||
}
|
||||
|
||||
# Return password
|
||||
return $"($symb1)(random integer 1..99)($randword1)($symb2)($randword2)($symb3)(random integer 1..99)($randword3)($symb4)"
|
||||
|
||||
}
|
||||
|
||||
##### Utility functions #####
|
||||
# Function to get random word from a dictionary file
|
||||
def get-random-word [
|
||||
wordlength: int
|
||||
numlines: int
|
||||
] {
|
||||
open
|
||||
| lines
|
||||
| wrap word
|
||||
| upsert len {|it| $it.word | str length}
|
||||
| where len <= ($wordlength)
|
||||
| get (random integer 1..($numlines))
|
||||
| get word
|
||||
}
|
||||
|
||||
# Function to format a word randomly
|
||||
def random-format-word [] {
|
||||
each {|it|
|
||||
let rint = (random integer 1..4)
|
||||
if $rint == 1 {
|
||||
($it | str capitalize)
|
||||
} else if $rint == 2 {
|
||||
($it | str upcase)
|
||||
} else if $rint == 3 {
|
||||
($it | split chars | each {|c| if (random decimal) < 0.2 { $c | str upcase } else { $c }} | str join "")
|
||||
} else {
|
||||
($it | str downcase)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Function to get random symbol from list of symbols
|
||||
def get-random-symbol [
|
||||
symbolchars: string
|
||||
] {
|
||||
$symbolchars
|
||||
| split chars
|
||||
| get (random integer 0..14)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue