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
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 20` generates a 20-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".
107 # Note: This particular example float should be rounded to "22" later.
109 # Round $CHAR_COUNT1_FLOAT1 up to next highest integer for use as argument in later bash functions.
110 # Note: awk expression from https://bits.mdminhazulhaque.io/linux/round-number-in-bash-script.html
111 CHAR_COUNT1
=$
(echo "$CHAR_COUNT1_FLOAT" |
awk '{print ($0-int($0)>0)?int($0)+1:int($0)}')
113 # Generate passphrase
114 PASS1
=$
(rpass
"$CHAR_COUNT1")
120 # - How to echo a string as stderr instead of stdout.
121 # https://stackoverflow.com/a/2990533
123 # Date: 2010-06-07T14:52Z
124 # Date Accessed: 2020-01-20
126 # - How to check if script argument exists or not.
127 # https://stackoverflow.com/a/6482403
129 # Date: 2011-06-26T05:55Z
130 # Date Accessed: 2020-01-20
132 # - How to check that a string is an integer using regular expression test.
133 # https://stackoverflow.com/a/806923
134 # Author: Charles Duffy
135 # Date: 2009-04-30T13:32Z
136 # Date Accessed: 2020-01-20
138 # - How to use `bc` to calculate logarithms in Bash
139 # http://phodd.net/gnu-bc/bcfaq.html#bashlog
141 # Date Accessed: 2020-01-20
143 # - How to use `awk` to convert and round up a float to an integer.
144 # https://bits.mdminhazulhaque.io/linux/round-number-in-bash-script.html
145 # Author: Md. Minhazul Haque
147 # Date Accessed: 2020-01-20
149 # - How to use `/dev/urandom`, `tr`, and `head` to generate a random password in Bash.
150 # https://www.thegeekstuff.com/2010/04/unix-bash-function-examples/
151 # Author: SASIKALA, Ramesh Natarajan
153 # Date Accessed: 2020-01-20
155 # - Bech32 base32 charset
156 # https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki
157 # Author: Pieter Wuille <pieter.wuille@gmail.com>
159 # License: BSD-2-Clause
160 # Date: Accessed: 2021-01-23
162 # - Dependencies: bash, echo, bc, awk, tr, head.
164 # - GNU bash, version 5.0.3(1)-release (x86_64-pc-linux-gnu)
165 # Copyright (C) 2019 Free Software Foundation, Inc.
166 # License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
167 # This is free software; you are free to change and redistribute it.
168 # There is NO WARRANTY, to the extent permitted by law.
170 # - echo (GNU coreutils) 8.30
171 # Copyright (C) 2018 Free Software Foundation, Inc.
172 # License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
173 # This is free software: you are free to change and redistribute it.
174 # There is NO WARRANTY, to the extent permitted by law.
176 # Written by Brian Fox and Chet Ramey.
179 # Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
181 # - GNU Awk 4.2.1, API: 2.0 (GNU MPFR 4.0.2, GNU MP 6.1.2)
182 # Copyright (C) 1989, 1991-2018 Free Software Foundation.
184 # This program is free software; you can redistribute it and/or modify
185 # it under the terms of the GNU General Public License as published by
186 # the Free Software Foundation; either version 3 of the License, or
187 # (at your option) any later version.
189 # This program is distributed in the hope that it will be useful,
190 # but WITHOUT ANY WARRANTY; without even the implied warranty of
191 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
192 # GNU General Public License for more details.
194 # You should have received a copy of the GNU General Public License
195 # along with this program. If not, see http://www.gnu.org/licenses/.
197 # - tr (GNU coreutils) 8.30
198 # Copyright (C) 2018 Free Software Foundation, Inc.
199 # License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
200 # This is free software: you are free to change and redistribute it.
201 # There is NO WARRANTY, to the extent permitted by law.
203 # Written by Jim Meyering.
205 # - head (GNU coreutils) 8.30
206 # Copyright (C) 2018 Free Software Foundation, Inc.
207 # License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
208 # This is free software: you are free to change and redistribute it.
209 # There is NO WARRANTY, to the extent permitted by law.
211 # Written by David MacKenzie and Jim Meyering.