genpass.plugin.zsh 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. # EG, 9-mien-flood-Patti-buxom-dozes-ickier-pay-ailed-Foster
  60. # Can take a numerical argument for generating extra passwords
  61. local -i i num
  62. [[ $1 =~ '^[0-9]+$' ]] && num=$1 || num=1
  63. # Get all alphabetic words of at most 6 characters in length
  64. local dict=$(LC_ALL=C grep -E '^[a-zA-Z]{,6}$' /usr/share/dict/words)
  65. # Calculate the base-2 entropy of each word in $dict
  66. # Entropy is e = L * log2(C), where L is the length of the password (here,
  67. # in words) and C the size of the character set (here, words in $dict).
  68. # Solve for e = 128 bits of entropy. Recall: log2(n) = log(n)/log(2).
  69. local -i n=$((int(ceil(128*log(2)/log(${(w)#dict})))))
  70. for i in {1..$num}; do
  71. printf "$n-"
  72. printf "$dict" | shuf -n "$n" | paste -sd '-'
  73. done
  74. }