c9643ce7518467496732708fa1b0dc17f3673281
[BK-2020-03.git] / unitproc / bknpass
1 #!/bin/bash
2
3 # Author: Steven Baltakatei Sandoval (baltakatei.com)
4 #
5 # License: This bash script, `bknpass`, is licensed under GPLv3 or
6 # later by Steven Baltakatei Sandoval:
7 #
8 # `bknpass`, an alphanumeric password generator
9 # Copyright (C) 2021 Steven Baltakatei Sandoval (baltakatei.com)
10 #
11 # This program is free software: you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation, either version 3 of the License, or
14 # any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 #
21 # A copy of the GNU General Public License may be found at
22 # <https://www.gnu.org/licenses/>.
23 #
24 # Description: This bash script generates alphanumeric passphrases
25 # with a char-count determined by a user-provided number of bits of
26 # entropy. The passphrase is then outputted to stdout with a trailing
27 # newline. It works as follows:
28 #
29 # - Prompt user for an integer. This integer is the number of bits
30 # of entropy that the generated password should have.
31 #
32 # - Check if user-provided string is an integer using `bash` regular
33 # expression test.
34 #
35 # - Calculate the minimum number of bech32 base32 characters
36 # required to encode the specified number of bits of entropy.
37 #
38 # - This step uses `bc` to calculate a logarithm float string
39 # and `awk` to convert the float into an integer, rounding up.
40 #
41 # - Use `tr`, `/dev/urandom`, and `head` to generate a random
42 # alphanumeric string with the length calculated in the previous
43 # step.
44 #
45 # - Use `echo` to display the passphrase in stdout with a trailing
46 # newline.
47 #
48 # Usage: bknpass [int]
49 #
50 # Example: bknpass 256
51 #
52 # Dependencies: bash, echo, bc, awk, tr, head. See end of file
53 #
54 # Tested on:
55 #
56 # - GNU/Linux Debian 10
57
58
59 #==Initialization==
60
61 let ALPHABET_SIZE="32" # number of unique chars in bech32 base32 charset, argument fed to `tr -c` in 'Generate passphrase' step)
62 LOG_BASE=2 # Set logarithm base to 2
63
64 # Define `echoerr` function which outputs text to stderr
65 # Note: function copied from https://stackoverflow.com/a/2990533
66 function echoerr {
67 echo "$@" 1>&2;
68 }
69
70 # Define `rpass` function which generates a base32 passphrase of length $1 (ex: `rpass 22` generates a 22-char string)
71 # Note: function adapted from https://www.thegeekstuff.com/2010/04/unix-bash-function-examples/
72 # Note: base32 charset uses bech32 charset
73 function rpass {
74 cat /dev/urandom | LC_ALL=C tr -cd "qpzry9x8gf2tvdw0s3jn54khce6mua7l" | head -c ${1:-22}
75 }
76
77
78 #==Main Program==
79
80 # Define $ENTROPY_BIT_COUNT1 as argument $1 or prompt user if $1 is not defined.
81 # note: argument test adapted from https://stackoverflow.com/a/6482403
82 if [ -z "$1" ]
83 then
84 echo "Entropy bit count argument (\$1) not supplied."
85 # Get from user the number of bits of entropy.
86 echoerr -n "Please specify the required strength of the password in bits of entropy (ex: 256):" # prompt via stderr
87 read ENTROPY_BIT_COUNT1
88 else
89 ENTROPY_BIT_COUNT1="$1"
90 fi
91
92 # Check if $ENTROPY_BIT_COUNT1 is an non-negative integer
93 # Note: Regular expression test is adapted from https://stackoverflow.com/a/806923
94 RETEST1='^[0-9]+$'
95 if ! [[ $ENTROPY_BIT_COUNT1 =~ $RETEST1 ]] ; then
96 echo "error: Not an integer." >&2; exit 1
97 fi
98
99 # Calculate minimum count of chars needed to encode $ENTROPY_BIT_COUNT1 with alphabet size of $ALPHABET_SIZE as float
100 # Solve ln(a^n)/ln(2)=b for n using `bc` where
101 # a=$ALPHABET_SIZE
102 # n=$CHAR_COUNT1_FLOAT
103 # b=$ENTROPY_BIT_COUNT1
104 # Note: `bc` logarithm usage adapted from http://phodd.net/gnu-bc/bcfaq.html#bashlog
105 CHAR_COUNT1_FLOAT=$(echo "$ENTROPY_BIT_COUNT1*l($LOG_BASE)/l($ALPHABET_SIZE)" | bc -l)
106 # Note: Float will be of form "21.49744370650136860806". This particular float should be rounded to "22" later.
107
108 # Round $CHAR_COUNT1_FLOAT1 up to next highest integer for use as argument in later bash functions.
109 # Note: awk expression from https://bits.mdminhazulhaque.io/linux/round-number-in-bash-script.html
110 CHAR_COUNT1=$(echo "$CHAR_COUNT1_FLOAT" | awk '{print ($0-int($0)>0)?int($0)+1:int($0)}')
111
112 # Generate passphrase
113 PASS1=$(rpass "$CHAR_COUNT1")
114 echo -e "$PASS1"
115
116
117 #==References==
118 #
119 # - How to echo a string as stderr instead of stdout.
120 # https://stackoverflow.com/a/2990533
121 # Author: James Roth
122 # Date: 2010-06-07T14:52Z
123 # Date Accessed: 2020-01-20
124 #
125 # - How to check if script argument exists or not.
126 # https://stackoverflow.com/a/6482403
127 # Author: phoxix
128 # Date: 2011-06-26T05:55Z
129 # Date Accessed: 2020-01-20
130 #
131 # - How to check that a string is an integer using regular expression test.
132 # https://stackoverflow.com/a/806923
133 # Author: Charles Duffy
134 # Date: 2009-04-30T13:32Z
135 # Date Accessed: 2020-01-20
136 #
137 # - How to use `bc` to calculate logarithms in Bash
138 # http://phodd.net/gnu-bc/bcfaq.html#bashlog
139 # Author: unknown
140 # Date Accessed: 2020-01-20
141 #
142 # - How to use `awk` to convert and round up a float to an integer.
143 # https://bits.mdminhazulhaque.io/linux/round-number-in-bash-script.html
144 # Author: Md. Minhazul Haque
145 # Date: 2015-01-09
146 # Date Accessed: 2020-01-20
147 #
148 # - How to use `/dev/urandom`, `tr`, and `head` to generate a random password in Bash.
149 # https://www.thegeekstuff.com/2010/04/unix-bash-function-examples/
150 # Author: SASIKALA, Ramesh Natarajan
151 # Date: 2010-04-21
152 # Date Accessed: 2020-01-20
153 #
154 # - Bech32 base32 charset
155 # https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki
156 # Author: Pieter Wuille <pieter.wuille@gmail.com>
157 # Date: 2017-03-20
158 # License: BSD-2-Clause
159 # Date: Accessed: 2021-01-23
160 #
161 # - Dependencies: bash, echo, bc, awk, tr, head.
162 #
163 # - GNU bash, version 5.0.3(1)-release (x86_64-pc-linux-gnu)
164 # Copyright (C) 2019 Free Software Foundation, Inc.
165 # License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
166 # This is free software; you are free to change and redistribute it.
167 # There is NO WARRANTY, to the extent permitted by law.
168 #
169 # - echo (GNU coreutils) 8.30
170 # Copyright (C) 2018 Free Software Foundation, Inc.
171 # License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
172 # This is free software: you are free to change and redistribute it.
173 # There is NO WARRANTY, to the extent permitted by law.
174 #
175 # Written by Brian Fox and Chet Ramey.
176 #
177 # - bc 1.07.1
178 # Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
179 #
180 # - GNU Awk 4.2.1, API: 2.0 (GNU MPFR 4.0.2, GNU MP 6.1.2)
181 # Copyright (C) 1989, 1991-2018 Free Software Foundation.
182 #
183 # This program is free software; you can redistribute it and/or modify
184 # it under the terms of the GNU General Public License as published by
185 # the Free Software Foundation; either version 3 of the License, or
186 # (at your option) any later version.
187 #
188 # This program is distributed in the hope that it will be useful,
189 # but WITHOUT ANY WARRANTY; without even the implied warranty of
190 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
191 # GNU General Public License for more details.
192 #
193 # You should have received a copy of the GNU General Public License
194 # along with this program. If not, see http://www.gnu.org/licenses/.
195 #
196 # - tr (GNU coreutils) 8.30
197 # Copyright (C) 2018 Free Software Foundation, Inc.
198 # License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
199 # This is free software: you are free to change and redistribute it.
200 # There is NO WARRANTY, to the extent permitted by law.
201 #
202 # Written by Jim Meyering.
203 #
204 # - head (GNU coreutils) 8.30
205 # Copyright (C) 2018 Free Software Foundation, Inc.
206 # License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
207 # This is free software: you are free to change and redistribute it.
208 # There is NO WARRANTY, to the extent permitted by law.
209 #
210 # Written by David MacKenzie and Jim Meyering.