quickpass 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/bin/bash
  2. #
  3. # Quick way to generate a random password for online systems
  4. #
  5. # Usage: quickpass -l [LENGTH], where:
  6. # length - the desired length of the password
  7. #
  8. # FIXME:
  9. # because of the way `printf' is implemented (at least in bash), there's a
  10. # pitfall when sampling the backslash '\' character: we cannot ignore it.
  11. # Either we interpret it and risk breaking the line when copying to xsel or we
  12. # forcefully escape it together with every other shell character, thus
  13. # lengthening the passwords unpredictably. I chose the former.
  14. #
  15. # License information:
  16. #
  17. # Copyright (C) 2016 - kzimmermann <https://quitter.se/kzimmermann>
  18. #
  19. # This program is free software: you can redistribute it and/or modify
  20. # it under the terms of the GNU General Public License as published by
  21. # the Free Software Foundation, either version 3 of the License, or
  22. # (at your option) any later version.
  23. #
  24. # This program is distributed in the hope that it will be useful,
  25. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. # GNU General Public License for more details.
  28. #
  29. # You should have received a copy of the GNU General Public License
  30. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  31. #
  32. # Some online services put a cap on how long a password can be. We default it
  33. # to 32 characters, but you can change it accordingly. If you estipulate a
  34. # number as an argument to this program, this value is overriden
  35. # It's not recommended to use a password that's shorter than 14 characters!
  36. LENGTH=32
  37. # Do we want to use quickpass as part of a script? If so, print the password to
  38. # stdout, where it can be piped to variable or file. Pass -p to enable it.
  39. FORCE_PRINT=false
  40. # Get command-line options:
  41. while [[ -n "$1" ]]
  42. do
  43. case "$1" in
  44. "-h" | "--help" )
  45. cat <<EOF
  46. Quick way to generate a random throwaway password for online systems
  47. Usage: $(basename $0) -l LENGTH -p, where:
  48. -l: make a password of the desired LENGTH (default: $LENGTH)
  49. -p: print final password to stdout (useful in shell scripts)
  50. EOF
  51. exit 0
  52. ;;
  53. "-l" | "--length" )
  54. shift
  55. LENGTH="$1"
  56. ;;
  57. "-p" | "--print" )
  58. FORCE_PRINT=true
  59. ;;
  60. * )
  61. echo "Error: unrecognized option '$1'"
  62. exit 1
  63. ;;
  64. esac
  65. # call next round
  66. shift
  67. done
  68. seed="$(dd if=/dev/urandom bs=512 count=1 2> /dev/null | tr -dc [:graph:])"
  69. index=${RANDOM:2:2}
  70. # I need to do this to avoid things like 09, 04, etc
  71. [[ ${index:0:1} == "0" ]] && index=${index:1:1}
  72. password=${seed:$index:$LENGTH}
  73. if [[ $FORCE_PRINT == true ]]
  74. then
  75. echo $password
  76. exit 0
  77. fi
  78. if [[ -f /usr/bin/xsel ]]; then
  79. printf %b ${password} | xsel -ib
  80. printf %b ${password} | xsel -i
  81. echo "Password copied to the clipboard."
  82. else
  83. echo $password
  84. fi