Skip to content

Commit

Permalink
Improve nushell syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
NonlinearFruit committed Jan 2, 2024
1 parent dd58445 commit 372eec8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 32 deletions.
19 changes: 11 additions & 8 deletions scripts/ipsum
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
# - http://stackoverflow.com/a/14203146/4769802
# - http://www.unix.com/shell-programming-and-scripting/156551-check-whether-string-begin-uppercase-lowercase-digit.html

# Outputs random (cryptographically secure) words from ~/scripts/words.txt
def main [
--with-entropy # Include the entropy of the generated words
--as-json # Print as json
number_of_words?: int # Number of words to generate (default: 1)
--with-entropy # Include the entropy of the generated words
--as-json # Print as json
number_of_words?: int = 1 # Number of words to generate
] {
let count = $number_of_words | default 1
let max_int = 16777216
let all_non_random_words = open "~/scripts/words.txt" | lines
let total_options = ($all_non_random_words | length) # apprx # of options

let words = 1..$count
let words = 1..$number_of_words
| each {|x|
od -N3 -An -i /dev/urandom
| into int
Expand All @@ -28,20 +28,23 @@ def main [
}

if $with_entropy {
let entropy = $total_options | math log 2 | $in * $count | math round --precision 3
let entropy = $total_options | math log 2 | $in * $number_of_words | math round --precision 3
if $as_json {
{
entropy: $entropy
words: $words
}
| to json
} else {
echo $"($count) word\(s) out of ($total_options) produces ($entropy) bits of entropy."
echo $"($number_of_words) word\(s) out of ($total_options) produces ($entropy) bits of entropy."
$words | str join (char newline)
}
} else {
if $as_json {
$words | to json
{
words: $words
}
| to json
} else {
$words | str join (char newline)
}
Expand Down
42 changes: 18 additions & 24 deletions scripts/passphrase
Original file line number Diff line number Diff line change
@@ -1,42 +1,36 @@
#!/usr/bin/env nu

# Generates a cryptographically secure passphrase
def main [
--with-entropy # Print the entropy of the passphrase
--number-of-numbers: int # Number of single digit numbers in passphrase
number_of_words? # Number of words in passphrase
--with-entropy # Print the entropy of the passphrase
--number-of-numbers: int = 0 # Number of single digit numbers in passphrase
number_of_words?: int = 1 # Number of words in passphrase
] {
let ipsum_structure = ipsum --with-entropy=$with_entropy --as-json $number_of_words
| from json
let numbers = 1..$number_of_numbers | each { random-number } | str join ''
let passphrase = $ipsum_structure
| get words
| append $numbers
| str join '#'
if $with_entropy {
let ipsum_structure = ipsum --with-entropy --as-json ($number_of_words | default 1)
| from json
let numbers = 1..($number_of_numbers | default 0) | each { random-number } | str join ''
let number_entropy = (10 | math log 2 | $in * ($number_of_numbers | default 0))
let passphrase = $ipsum_structure.words
| append $numbers
| str join '#'

let number_entropy = (10 | math log 2 | $in * $number_of_numbers)
print ({
passphrase: $passphrase
entropy: ($ipsum_structure.entropy + ($number_of_numbers | default 0))
entropy: ($ipsum_structure.entropy + $number_of_numbers)
})
echo $passphrase | clip copy
print "Copied!"
} else {
let numbers = 1..($number_of_numbers | default 0) | each { random-number } | str join ''
let passphrase = ipsum --as-json ($number_of_words | default 1)
| from json
| append $numbers
| str join '#'

print $passphrase
echo $passphrase | clip copy
print "Copied!"
}
echo $passphrase | clip copy
print "Copied!"
}

def random-number [] {
def random-number [max: int = 10] {
let max_int = 16777216
od -N3 -An -i /dev/urandom
| into int
| $in * 10 / $max_int
| $in * $max / $max_int
| math round
}

0 comments on commit 372eec8

Please sign in to comment.