#!/bin/bash ## Poor man's password generator. # # Requires: # xclip (optional): copies password to clipboard # qrencode (optional): generate QR code of the password # # TODO: # Override clipboard after a configurable amount of time (https://git.zx2c4.com/password-store/tree/src/password-store.sh#n173) NC='\033[0m' RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[0;33m' msg_info() { echo -e "${GREEN}$1${NC}" } msg_warn() { echo -e "${YELLOW}$1${NC}" } msg_error() { echo -e "${RED}$1${NC}" } print_help() { echo echo "genpass - a poor man's password generator" echo echo "USAGE:" echo " genpass [OPTION ...]" echo echo "where OPTIONs are:" echo " -c | --clipboard:" echo " copy the generated password to clipboard (needs xclip)" echo " -f | --filter :" echo " specify a custom filter for the password (see specification" echo " for SETs in 'tr' man page). Defaults to 'A-Za-z0-9_'." echo " -h | -h | --help:" echo " print this help message" echo " -l | --length :" echo " provide password length (defaults to 32)" echo " -q | --qrcode:" echo " generate a qrcode of the password (needs qrencode) and" echo " print it to stdout (UTF8 format)" echo } CLIPBOARD=0 QRCODE=0 LENGTH=32 FILTER=A-Za-z0-9_ while [ $# != 0 ] do arg=$1 case "$arg" in -c|--clipboard) CLIPBOARD=1 ;; -f|--filter) if [[ $# -ge 2 ]]; then FILTER=$2 shift else msg_error "No value found for flag $arg!" print_help exit 1 fi ;; -h|--help) print_help exit 0 ;; -l|--length) if [[ $# -ge 2 ]]; then LENGTH=$2 shift else msg_error "No value found for flag $arg!" print_help exit 2 fi ;; -q|--qrcode) QRCODE=1 ;; *) msg_error "$1: invalid option" print_help exit 3 ;; esac shift done PASSWD="$(tr -dc $FILTER < /dev/urandom | head -c $LENGTH)" if [ $CLIPBOARD -eq 1 ]; then echo -nE "$PASSWD" | xclip -in -selection clipboard elif [ $QRCODE -eq 1 ]; then echo -nE "$PASSWD" | qrencode --size=5 --type=UTF8 -o - else echo -E "$PASSWD" fi exit 0