this post was submitted on 24 Feb 2025
12 points (92.9% liked)

commandline

1891 readers
1 users here now

founded 2 years ago
MODERATORS
12
submitted 1 week ago* (last edited 1 week ago) by [email protected] to c/commandline
 

http://xkcd.com/936/

Not worth creating a project for, and it might be interesting to see what changes people would make.

Non-standard dependencies:

  • words, for the dictionary
  • zsh (this will probably work just fine with bash, though, too)
#!/usr/bin/zsh
# Author: @[email protected]
# 2025-02-23

final=(xargs echo)
count=6
while getopts d opt; do
	case $opt in
		d)
			final=(tr 'A-Z' 'a-z')
			;;
		*)
			printf "Password generator based on the correcthorse algorithm from http://xkcd.com/936//n/n"
			printf "USAGE: %s [-d] [#]\n" "$0"
			printf " -d  make the result all lower case; otherwise, each word will be capitalized.\n"
			printf " #   the number of words to include. Defaults to 6."
			exit 1
			;;
	esac
done
shift $(($OPTIND - 1))
[[ $# -gt 0 ]] && count=$*

shuf -n $((count * 2)) /usr/share/dict/american-english | \
	sed 's/'"'"'.*//; s/^\(\w\)/\U\1/' | \
	sort | uniq | shuf -n $count | xargs echo | \
	tr -d ' ' | $final

What's going on here:

Nearly 30% of the American dictionary (34,242) are words with apostrophes. They could be left in to help satisfy password requirements that demand "special characters," but correcthorse isn't an algorithm that handles idiot "password best practices" well anyway. So, since every word with an apostrophe has a pair word without one, we pull 2·N words to make sure we have enough. Then we strip out the plural/possessives and capitalize every word. Then we remove duplicates and select our N words from the result. Finally, we compact that into a space-less string of words, and if the user passed the -d option, we downcase the entire thing.

Without the user options, this really could be a 1-liner; that's how it started:

alias pony="shuf -n 12 /usr/share/dict/american-english | sed 's/'\"'\"'.*//; s/^\(\w\)/\U\1/' | sort | uniq | shuf -n 6 | xargs echo | tr -d ' '"
you are viewing a single comment's thread
view the rest of the comments
[–] her01n 3 points 5 days ago (1 children)

diceware is an online as well as command line implementation.

[–] [email protected] 1 points 5 days ago

That's the one that made me make this script. I thought, surely this is a one-liner? And indeed, except that I kept adding features. I think I'm going to change my shell function back to the one-liner though. All of that extra complexity is unnecessary.