From 4cdad2d2ff060374c5f0263783897e7900d00990 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sat, 20 Nov 2010 19:59:55 -0500 Subject: [PATCH] Add passwd-gen post and script. --- posts/passwd-gen.mdwn | 13 +++++++++ posts/passwd-gen/passwd-gen.sh | 52 ++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 posts/passwd-gen.mdwn create mode 100755 posts/passwd-gen/passwd-gen.sh diff --git a/posts/passwd-gen.mdwn b/posts/passwd-gen.mdwn new file mode 100644 index 0000000..ddbb71d --- /dev/null +++ b/posts/passwd-gen.mdwn @@ -0,0 +1,13 @@ +[[passwd-gen.sh]] is a `/dev/random` sanitizer for generating random +passwords of various length and complexity. + + $ ./passwd-gen.sh 16 + NEmXOH2djJvUamdh + Length of password: 16 + Total bytes read: 52 + +The script is well commented, so there's not much more to say here... + +[[!tag tags/bash]] +[[!tag tags/linux]] +[[!tag tags/programming]] diff --git a/posts/passwd-gen/passwd-gen.sh b/posts/passwd-gen/passwd-gen.sh new file mode 100755 index 0000000..5ddc9de --- /dev/null +++ b/posts/passwd-gen/passwd-gen.sh @@ -0,0 +1,52 @@ +#!/bin/bash +# +# Copyright (C) 2008-2010, William Trevor King +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# generate a random password, defaults to 8 characters +# +# usage: passwd-gen [num-characters] + +# You may want to modify NC to get the best performance depending on +# your usual choice of DEVICE and N. For slower DEVICEs, it's better +# to have lower NC (wastes less usable characters). For faster +# DEVICEs, it's better to have a higher NC (fewer slow shell loops). + +N=8 # password length +NC=4 # number of characters in a "chunk" +#SET="[:alnum:][:punct:]" # set of allowed characters (see tr(1)) +SET="[:alnum:]" +DEVICE=/dev/random # use /dev/urandom for faster, weaker passwords + +if [ -n "$1" ] +then + N="$1" +fi + +NA=0 +PWD="" +while [ "$N" -gt 0 ]; do + ALL=$(head -c "$NC" "$DEVICE") + let "NA = NA + ${#ALL}" + NEW=$(echo "$ALL" | tr -d -c $SET | head -c "$N") + PWD="$PWD$NEW" + let "N = N - ${#NEW}" +done + +echo "$PWD" +echo "Length of password: ${#PWD}" +echo "Total bytes read: $NA" + +exit 0 -- 2.26.2