summaryrefslogtreecommitdiff
path: root/misc/passwdgen
blob: 21dff293f6f2559ae807ebb81060f0239b144ed8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/sh
[ -n "`echo $1$2 | tr -d '[:digit:]'`" ] && echo "Usage: $0 [Length] [Itterations] [Set]" && exit
[ -z "$1" ] && len=20 || len=$1
[ -z "$2" ] && itt=5  || itt=$2
[ -z "$3" ] && set='[:graph:]' || set=$3
genpasswd() { tr -cd "$3" < /dev/urandom | fold -w$1 | head -n$2; }
genpasswd $len $itt $set

# Old solution using recursion which was much slower (and apparently less posix compliant) but also much cooler
## This should all work in dash (its all posix compliant shell), but there are frequent segmentation faults and formatting errors in the output. Bash fixes that all for some reason. Probably an issue of recursion depth and some other bugs or something in dash I have no idea.
#genpasswd() { pass=$pass`head -1 /dev/urandom | tr -cd '[:graph:]'` && [ `echo -n $pass | wc -m` -ge $len ] && echo $pass | cut -c -$len && pass="" || genpasswd; }
#for i in `seq $itt`; do genpasswd; done

# Other old solution that is much faster for extremely long passwords, but slower for many shorter ones since it utilizes a for loop
#genpasswd() { tr -cd '[:graph:]' < /dev/urandom | head -c$1 && echo; }
#for i in `seq $itt`; do genpasswd $len; done