3 # Author: Steven Baltakatei Sandoval (baltakatei.com)
5 # License: This bash script, `bknpass`, is licensed under GPLv3 or
6 # later by Steven Baltakatei Sandoval:
8 # `bknpass`, an alphanumeric password generator
9 # Copyright (C) 2021 Steven Baltakatei Sandoval (baltakatei.com)
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
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.
21 # A copy of the GNU General Public License may be found at
22 # <https://www.gnu.org/licenses/>.
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:
29 # - Prompt user for an integer. This integer is the number of bits
30 # of entropy that the generated password should have.
32 # - Check if user-provided string is an integer using `bash` regular
35 # - Calculate the minimum number of bech32 base32 characters
36 # required to encode the specified number of bits of entropy.
38 # - This step uses `bc` to calculate a logarithm float string
39 # and `awk` to convert the float into an integer, rounding up.
41 # - Use `tr`, `/dev/urandom`, and `head` to generate a random
42 # alphanumeric string with the length calculated in the previous
45 # - Use `echo` to display the passphrase in stdout with a trailing
48 # Usage: bknpass [int]
50 # Example: bknpass 256
52 # Dependencies: bash, echo, bc, awk, tr, head. See end of file
56 # - GNU/Linux Debian 10
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
64 # Define `echoerr` function which outputs text to stderr
65 # Note: function copied from https://stackoverflow.com/a/2990533
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
74 cat /dev
/urandom | LC_ALL
=C
tr -cd "qpzry9x8gf2tvdw0s3jn54khce6mua7l" |
head -c ${1:-22}
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
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
89 ENTROPY_BIT_COUNT1
="$1"
92 # Check if $ENTROPY_BIT_COUNT1 is an non-negative integer
93 # Note: Regular expression test is adapted from https://stackoverflow.com/a/806923
95 if ! [[ $ENTROPY_BIT_COUNT1 =~
$RETEST1 ]] ; then
96 echo "error: Not an integer." >&2; exit 1
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
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.
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)}')
112 # Generate passphrase
113 PASS1
=$
(rpass
"$CHAR_COUNT1")
119 # - How to echo a string as stderr instead of stdout.
120 # https://stackoverflow.com/a/2990533
122 # Date: 2010-06-07T14:52Z
123 # Date Accessed: 2020-01-20
125 # - How to check if script argument exists or not.
126 # https://stackoverflow.com/a/6482403
128 # Date: 2011-06-26T05:55Z
129 # Date Accessed: 2020-01-20
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
137 # - How to use `bc` to calculate logarithms in Bash
138 # http://phodd.net/gnu-bc/bcfaq.html#bashlog
140 # Date Accessed: 2020-01-20
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
146 # Date Accessed: 2020-01-20
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
152 # Date Accessed: 2020-01-20
154 # - Bech32 base32 charset
155 # https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki
156 # Author: Pieter Wuille <pieter.wuille@gmail.com>
158 # License: BSD-2-Clause
159 # Date: Accessed: 2021-01-23
161 # - Dependencies: bash, echo, bc, awk, tr, head.
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.
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.
175 # Written by Brian Fox and Chet Ramey.
178 # Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
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.
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.
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.
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/.
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.
202 # Written by Jim Meyering.
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.
210 # Written by David MacKenzie and Jim Meyering.