Skip to content

Instantly share code, notes, and snippets.

@ginkgomzd
Last active February 5, 2021 02:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ginkgomzd/6ba5377f2bdaea9b20dbcf72547f3546 to your computer and use it in GitHub Desktop.
Save ginkgomzd/6ba5377f2bdaea9b20dbcf72547f3546 to your computer and use it in GitHub Desktop.
generate a strong password without string delimiters in it
#!/usr/bin/env bash
# generate a strong password without shell special characters
# defaults to 32 characters, overridden to a number passed as first arg
# uses xclip to copy to clipboard
# uses zenity to alert to desktop (from gnome-run-prompt)
gen_pass_length=32
if [ $# -eq 1 ]; then
gen_pass_length="$1"
fi
# tinfoil hat:
# prevent use of a cached random seed
rm -f ~/.rnd
# 999 - generate plenty of chars
# 2>/dev/null - suppress 'could not write to ~/.rnd'
# tr - whitelist provided octal character list.
# -c, -C, --complement
# use the complement of SET1
# -d, --delete
# delete characters in SET1, do not translate
# -s, --squeeze-repeats
# replace each input sequence of a repeated character that is listed
# in SET1 with a single occurrence of that character
# head -c - num of chars
# tee /dev/tty - output to terminal as well as copy to clipboard (xclip)
#
# Generate custom blacklist using:
# http://www.unit-conversion.info/texttools/octal/
charset='\041-\047\050-\057\060-\067\070-\077\100-\107\110-\117\120-\127\130-\137\140-\147\150-\157\160-\167\170-\176'
# Exclude: "'`$&|:@;<>#
blacklist='\042\047\140\044\046\174\072\100\073\074\076\043'
# sample enough chars from the end of our random string
# just for fun
sample_size=$(( $gen_pass_length * 3 ))
openssl rand 999 2>/dev/null | \
tr -dC "$charset" | tr -d "$blacklist" | tr -s "$charset" | tail -c"$sample_size" | head -c"$gen_pass_length" | \
tee /dev/tty | xclip -selection c
gen_pass_msg='copied to clipboard'
# Disabled check for terminal because approach doesn't work for xfce run-prompt.
# if [ -t 1 ]; then
# # shell
# echo $'\n'$gen_pass_msg
# else
# desktop
zenity --info --title='Password Generator' --text="$gen_pass_msg" --width=200 2>/dev/null
# fi
# tinfoil hat:
rm -f ~/.rnd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment