genpass.plugin.zsh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. autoload -U regexp-replace
  2. zmodload zsh/mathfunc
  3. genpass-apple() {
  4. # Generates a 128-bit password of 6 pseudowords of 6 characters each
  5. # EG, xudmec-4ambyj-tavric-mumpub-mydVop-bypjyp
  6. # Can take a numerical argument for generating extra passwords
  7. local -i i j num
  8. [[ $1 =~ '^[0-9]+$' ]] && num=$1 || num=1
  9. local consonants="$(LC_ALL=C tr -cd b-df-hj-np-tv-xz < /dev/urandom \
  10. | head -c $((24*$num)))"
  11. local vowels="$(LC_ALL=C tr -cd aeiouy < /dev/urandom | head -c $((12*$num)))"
  12. local digits="$(LC_ALL=C tr -cd 0-9 < /dev/urandom | head -c $num)"
  13. # The digit is placed on a pseudoword edge using $base36. IE, Dvccvc or cvccvD
  14. local position="$(LC_ALL=C tr -cd 056bchinotuz < /dev/urandom | head -c $num)"
  15. local -A base36=(0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 a 10 b 11 c 12 d 13 \
  16. e 14 f 15 g 16 h 17 i 18 j 19 k 20 l 21 m 22 n 23 o 24 p 25 q 26 r 27 s 28 \
  17. t 29 u 30 v 31 w 32 x 33 y 34 z 35)
  18. for i in {1..$num}; do
  19. local pseudo=""
  20. for j in {1..12}; do
  21. # Uniformly iterate through $consonants and $vowels for each $i and $j
  22. # Creates cvccvccvccvccvccvccvccvccvccvccvccvc for each $num
  23. pseudo="${pseudo}${consonants:$((24*$i+2*${j}-26)):1}"
  24. pseudo="${pseudo}${vowels:$((12*$i+${j}-13)):1}"
  25. pseudo="${pseudo}${consonants:$((24*$i+2*${j}-25)):1}"
  26. done
  27. local -i digit_pos=${base36[${position[$i]}]}
  28. local -i char_pos=$digit_pos
  29. # The digit and uppercase character must be in different locations
  30. while [[ $digit_pos == $char_pos ]]; do
  31. char_pos=$base36[$(LC_ALL=C tr -cd 0-9a-z < /dev/urandom | head -c 1)]
  32. done
  33. # Places the digit on a pseudoword edge
  34. regexp-replace pseudo "^(.{$digit_pos}).(.*)$" \
  35. '${match[1]}${digits[$i]}${match[2]}'
  36. # Uppercase a random character (that is not a digit)
  37. regexp-replace pseudo "^(.{$char_pos})(.)(.*)$" \
  38. '${match[1]}${(U)match[2]}${match[3]}'
  39. # Hyphenate each 6-character pseudoword
  40. regexp-replace pseudo '^(.{6})(.{6})(.{6})(.{6})(.{6})(.{6})$' \
  41. '${match[1]}-${match[2]}-${match[3]}-${match[4]}-${match[5]}-${match[6]}'
  42. printf "${pseudo}\n"
  43. done
  44. }
  45. genpass-monkey() {
  46. # Generates a 128-bit base32 password as if monkeys banged the keyboard
  47. # EG, nz5ej2kypkvcw0rn5cvhs6qxtm
  48. # Can take a numerical argument for generating extra passwords
  49. local -i i num
  50. [[ $1 =~ '^[0-9]+$' ]] && num=$1 || num=1
  51. local pass=$(LC_ALL=C tr -cd '0-9a-hjkmnp-tv-z' < /dev/urandom \
  52. | head -c $((26*$num)))
  53. for i in {1..$num}; do
  54. printf "${pass:$((26*($i-1))):26}\n"
  55. done
  56. }
  57. genpass-xkcd() {
  58. # Generates a 128-bit XKCD-style passphrase
  59. # e.g, 9-mien-flood-Patti-buxom-dozes-ickier-pay-ailed-Foster
  60. # Can take a numerical argument for generating extra passwords
  61. if (( ! $+commands[shuf] )); then
  62. echo >&2 "$0: \`shuf\` command not found. Install coreutils (\`brew install coreutils\` on macOS)."
  63. return 1
  64. fi
  65. if [[ ! -e /usr/share/dict/words ]]; then
  66. echo >&2 "$0: no wordlist found in \`/usr/share/dict/words\`. Install one first."
  67. return 1
  68. fi
  69. local -i i num
  70. [[ $1 =~ '^[0-9]+$' ]] && num=$1 || num=1
  71. # Get all alphabetic words of at most 6 characters in length
  72. local dict=$(LC_ALL=C grep -E '^[a-zA-Z]{1,6}$' /usr/share/dict/words)
  73. # Calculate the base-2 entropy of each word in $dict
  74. # Entropy is e = L * log2(C), where L is the length of the password (here,
  75. # in words) and C the size of the character set (here, words in $dict).
  76. # Solve for e = 128 bits of entropy. Recall: log2(n) = log(n)/log(2).
  77. local -i n=$((int(ceil(128*log(2)/log(${(w)#dict})))))
  78. for i in {1..$num}; do
  79. printf "$n-"
  80. printf "$dict" | shuf -n "$n" | paste -sd '-' -
  81. done
  82. }