Blame


1 ee89c9f1 2022-12-03 op #!/bin/sh
2 ee89c9f1 2022-12-03 op #
3 ee89c9f1 2022-12-03 op # Copyright (c) 2022 Omar Polo <op@omarpolo.com>
4 ee89c9f1 2022-12-03 op #
5 ee89c9f1 2022-12-03 op # Permission to use, copy, modify, and distribute this software for any
6 ee89c9f1 2022-12-03 op # purpose with or without fee is hereby granted, provided that the above
7 ee89c9f1 2022-12-03 op # copyright notice and this permission notice appear in all copies.
8 ee89c9f1 2022-12-03 op #
9 ee89c9f1 2022-12-03 op # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 ee89c9f1 2022-12-03 op # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 ee89c9f1 2022-12-03 op # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 ee89c9f1 2022-12-03 op # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 ee89c9f1 2022-12-03 op # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 ee89c9f1 2022-12-03 op # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 ee89c9f1 2022-12-03 op # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 ee89c9f1 2022-12-03 op
17 ee89c9f1 2022-12-03 op me=$(basename "$0")
18 ee89c9f1 2022-12-03 op
19 ee89c9f1 2022-12-03 op usage() {
20 ee89c9f1 2022-12-03 op echo "usage: $me [-an] [-w wordlist] [len]" >&2
21 ee89c9f1 2022-12-03 op exit 1
22 ee89c9f1 2022-12-03 op }
23 ee89c9f1 2022-12-03 op
24 ee89c9f1 2022-12-03 op wordlist=
25 ee89c9f1 2022-12-03 op chars="[:print:]"
26 ee89c9f1 2022-12-03 op len=32
27 ee89c9f1 2022-12-03 op
28 ee89c9f1 2022-12-03 op while getopts anw: ch; do
29 ee89c9f1 2022-12-03 op case $ch in
30 fb0a6924 2022-12-17 op a) chars="[:alnum:]" ;;
31 fb0a6924 2022-12-17 op n) chars="[:digit:]" ;;
32 fb0a6924 2022-12-17 op w) wordlist="$OPTARG"; len=6 ;;
33 fb0a6924 2022-12-17 op ?) usage ;;
34 ee89c9f1 2022-12-03 op esac
35 ee89c9f1 2022-12-03 op done
36 ee89c9f1 2022-12-03 op shift $(($OPTIND - 1))
37 ee89c9f1 2022-12-03 op
38 ee89c9f1 2022-12-03 op [ $# -gt 1 ] && usage
39 ee89c9f1 2022-12-03 op [ $# -eq 1 ] && len="$1"
40 ee89c9f1 2022-12-03 op
41 ee89c9f1 2022-12-03 op if [ -n "$wordlist" ]; then
42 b0639dc6 2023-05-16 op passphrase=$(sort -R -- "$wordlist" | head -n "$len")
43 b0639dc6 2023-05-16 op [ -n "$passphrase" ] && printf '%s\n' "$passphrase"
44 ee89c9f1 2022-12-03 op else
45 ee89c9f1 2022-12-03 op export LC_ALL=C
46 ee89c9f1 2022-12-03 op tr -cd "$chars" </dev/urandom | dd bs=1 count="$len" 2>/dev/null && \
47 ee89c9f1 2022-12-03 op echo
48 ee89c9f1 2022-12-03 op fi