~seirdy/dotfiles

dotfiles/Executables/shell-scripts/bin/genpw -rwxr-xr-x 840 bytes
c4120ec0Rohan Kumar Add some scripts 16 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/bin/sh
# generate a random password, using characters selected from the set of
# single-byte printable Unicode characters

displayUsage() {
	printf "genpw - generate a password from a set of 256 UTF-8 codepoints

USAGE:
  %s [OPTIONS] [LENGTH]

OPTIONS:
  -h, --help\t Display this help message.
  -a, --ascii\t Restrict charset to ASCII.

ARGS:
  LENGTH\t The number of desired characters in the password. Defaults to 64
" "$(basename "${0}")"
}

# Options
while [ $# -gt 0 ]; do
	case $1 in
		-h | --help)
			displayUsage
			exit 0
			;;
		-a | --ascii)
			ascii_only=1
			;;
		*)
			length="$1"
			;;
	esac
	shift
done

raw_pass() {
	tr -dc "$1" </dev/urandom \
		| dd ibs=1 obs=1 count="${length:-64}" 2>/dev/null
}

if [ -n "$ascii_only" ]; then
	raw_pass ' -~'
else
	raw_pass '\40-\377' | iconv -f ISO88591 -ct UTF8
fi

# vi:ft=sh