xkcd-passgen 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/bin/bash
  2. #
  3. # Generate random xkcd-style passwords using the built-in UNIX words file!
  4. #
  5. # Or... generate passwords that are closer to the Diceware method.
  6. #
  7. # If you don't know what a xkcd-style password is, refer to the comic below:
  8. #
  9. # https://xkcd.com/936/
  10. #
  11. # USAGE: xkcd-passgen [-d] [-l LENGTH in words]
  12. #
  13. # Copyright 2016 Klaus Zimmermann - https://quitter.se/kzimmermann
  14. #
  15. # This program is free software: you can redistribute it and/or modify
  16. # it under the terms of the GNU General Public License as published by
  17. # the Free Software Foundation, either version 3 of the License, or
  18. # (at your option) any later version.
  19. #
  20. # This program is distributed in the hope that it will be useful,
  21. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. # GNU General Public License for more details.
  24. #
  25. # You should have received a copy of the GNU General Public License
  26. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. #
  28. # How many words will your password be? (We don't recommend lower than 4)
  29. LENGTH=4
  30. # Do you want to do Diceware instead (short words, not longer than 6 chars)?
  31. # Default is no, override with -d
  32. DICEWARE=false
  33. # This is the standard words file in Debian, might be different in your
  34. # distribution. Change this accordingly.
  35. WORDS=/etc/dictionaries-common/words
  36. if [ ! -f "$WORDS" ]
  37. then
  38. echo "No suitable word file found at $WORDS."
  39. echo "Please use another file if your distribution has one."
  40. exit 1
  41. fi
  42. helper() {
  43. cat <<EOF
  44. USAGE: $(basename $0) [-d] [-l LENGTH]
  45. Where:
  46. -d: enables Diceware method to be used instead
  47. -l: generates a passphrase LENGTH words long
  48. This script is Free Software licensed under the GPL v3
  49. EOF
  50. }
  51. while [ -n "$1" ]
  52. do
  53. if [[ "$1" == "-l" ]]
  54. then
  55. LENGTH="$2"
  56. shift
  57. elif [[ "$1" == "-h" ]]
  58. then
  59. helper
  60. exit 0
  61. elif [[ "$1" == "-d" ]]
  62. then
  63. DICEWARE=true
  64. else
  65. echo "Unrecognized option '$1'"
  66. exit 1
  67. fi
  68. shift
  69. done
  70. if [[ "$DICEWARE" == true ]]
  71. then
  72. echo "Diceware mode enabled."
  73. if [[ "$LENGTH" -le 4 ]]
  74. then
  75. LENGTH=6 # minimum since words are shorter
  76. fi
  77. cat "$WORDS" |
  78. grep -e "^......$" | # only words up to 6 chars allowed.
  79. shuf |
  80. tail -"$LENGTH" |
  81. tr "\n" " " | # now you have words all in one line
  82. tr [A-Z] [a-z] | # put everything lowercase
  83. sed "s/'s//g" # remove 's that occasionally appear in the file.
  84. else
  85. # "normal", unfiltered xkcd-style passgen
  86. cat "$WORDS" |
  87. shuf |
  88. tail -"$LENGTH" |
  89. tr "\n" " " | # now you have words all in one line
  90. tr [A-Z] [a-z] | # put everything lowercase
  91. sed "s/'s//g" # remove 's that occasionally appear in the file.
  92. fi
  93. echo ""