4f53da0fa3d0d1035c0153abf0926f4bb7d47f7b
3 # Date: 2020-01-20T16:34Z
5 # Author: Steven Baltakatei Sandoval (baltakatei.com)
7 # License: This bash script, `bknpass`, is licensed under GPLv3 or
8 # later by Steven Baltakatei Sandoval:
10 # `bknpass`, an alphanumeric password generator
11 # Copyright (C) 2020 Steven Baltakatei Sandoval (baltakatei.com)
13 # This program is free software: you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation, either version 3 of the License, or
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
23 # A copy of the GNU General Public License may be found at
24 # <https://www.gnu.org/licenses/>.
26 # Description: This bash script generates alphanumeric passphrases
27 # with a char-count determined by a user-provided number of bits of
28 # entropy. The passphrase is then outputted to stdout with a trailing
29 # newline. It works as follows:
31 # - Prompt user for an integer. This integer is the number of bits
32 # of entropy that the generated password should have.
34 # - Check if user-provided string is an integer using `bash` regular
37 # - Calculate the minimum number of alphanumeric characters required
38 # to encode the specified number of bits of entropy.
40 # - This step uses `bc` to calculate a logarithm float string
41 # and `awk` to convert the float into an integer, rounding up.
43 # - Use `tr`, `/dev/urandom`, and `head` to generate a random
44 # alphanumeric string with the length calculated in the previous
47 # - Use `echo` to display the passphrase in stdout with a trailing
50 # Dependencies: bash, echo, bc, awk, tr, head. See end of file
54 # - GNU/Linux Debian 10
59 let ALPHABET_SIZE
="26+26+10" # number of unique chars in [:alnum:], argument fed to `tr -c` in 'Generate passphrase' step)
60 LOG_BASE
=2 # Set logarithm base to 2
62 # Define `echoerr` function which outputs text to stderr
63 # Note: function copied from https://stackoverflow.com/a/2990533
68 # Define `rpass` function which generates an alphanumeric passphrase of length $1 (ex: `rpass 22` generates a 22-char string)
69 # Note: function adapted from https://www.thegeekstuff.com/2010/04/unix-bash-function-examples/
71 cat /dev
/urandom | LC_ALL
=C
tr -cd "[:alnum:]" |
head -c ${1:-22}
77 # Define $ENTROPY_BIT_COUNT1 as argument $1 or prompt user if $1 is not defined.
78 # note: argument test adapted from https://stackoverflow.com/a/6482403
81 echo "Entropy bit count argument (\$1) not supplied."
82 # Get from user the number of bits of entropy.
83 echoerr
-n "Please specify the required strength of the password in bits of entropy (ex: 256):" # prompt via stderr
84 read ENTROPY_BIT_COUNT1
86 ENTROPY_BIT_COUNT1
="$1"
89 # Check if $ENTROPY_BIT_COUNT1 is an non-negative integer
90 # Note: Regular expression test is adapted from https://stackoverflow.com/a/806923
92 if ! [[ $ENTROPY_BIT_COUNT1 =~
$RETEST1 ]] ; then
93 echo "error: Not an integer." >&2; exit 1
96 # Calculate minimum count of chars needed to encode $ENTROPY_BIT_COUNT1 with alphabet size of $ALPHABET_SIZE as float
97 # Solve ln(a^n)/ln(2)=b for n using `bc` where
99 # n=$CHAR_COUNT1_FLOAT
100 # b=$ENTROPY_BIT_COUNT1
101 # Note: `bc` logarithm usage adapted from http://phodd.net/gnu-bc/bcfaq.html#bashlog
102 CHAR_COUNT1_FLOAT
=$
(echo "$ENTROPY_BIT_COUNT1*l($LOG_BASE)/l($ALPHABET_SIZE)" |
bc -l)
103 # Note: Float will be of form "21.49744370650136860806". This particular float should be rounded to "22" later.
105 # Round $CHAR_COUNT1_FLOAT1 up to next highest integer for use as argument in later bash functions.
106 # Note: awk expression from https://bits.mdminhazulhaque.io/linux/round-number-in-bash-script.html
107 CHAR_COUNT1
=$
(echo "$CHAR_COUNT1_FLOAT" |
awk '{print ($0-int($0)>0)?int($0)+1:int($0)}')
109 # Generate passphrase
110 PASS1
=$
(rpass
"$CHAR_COUNT1")
116 # - How to echo a string as stderr instead of stdout.
117 # https://stackoverflow.com/a/2990533
119 # Date: 2010-06-07T14:52Z
120 # Date Accessed: 2020-01-20
122 # - How to check if script argument exists or not.
123 # https://stackoverflow.com/a/6482403
125 # Date: 2011-06-26T05:55Z
126 # Date Accessed: 2020-01-20
128 # - How to check that a string is an integer using regular expression test.
129 # https://stackoverflow.com/a/806923
130 # Author: Charles Duffy
131 # Date: 2009-04-30T13:32Z
132 # Date Accessed: 2020-01-20
134 # - How to use `bc` to calculate logarithms in Bash
135 # http://phodd.net/gnu-bc/bcfaq.html#bashlog
137 # Date Accessed: 2020-01-20
139 # - How to use `awk` to convert and round up a float to an integer.
140 # https://bits.mdminhazulhaque.io/linux/round-number-in-bash-script.html
141 # Author: Md. Minhazul Haque
143 # Date Accessed: 2020-01-20
145 # - How to use `/dev/urandom`, `tr`, and `head` to generate a random password in Bash.
146 # https://www.thegeekstuff.com/2010/04/unix-bash-function-examples/
147 # Author: SASIKALA, Ramesh Natarajan
149 # Date Accessed: 2020-01-20
151 # - Dependencies: bash, echo, bc, awk, tr, head.
153 # - GNU bash, version 5.0.3(1)-release (x86_64-pc-linux-gnu)
154 # Copyright (C) 2019 Free Software Foundation, Inc.
155 # License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
156 # This is free software; you are free to change and redistribute it.
157 # There is NO WARRANTY, to the extent permitted by law.
159 # - echo (GNU coreutils) 8.30
160 # Copyright (C) 2018 Free Software Foundation, Inc.
161 # License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
162 # This is free software: you are free to change and redistribute it.
163 # There is NO WARRANTY, to the extent permitted by law.
165 # Written by Brian Fox and Chet Ramey.
168 # Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
170 # - GNU Awk 4.2.1, API: 2.0 (GNU MPFR 4.0.2, GNU MP 6.1.2)
171 # Copyright (C) 1989, 1991-2018 Free Software Foundation.
173 # This program is free software; you can redistribute it and/or modify
174 # it under the terms of the GNU General Public License as published by
175 # the Free Software Foundation; either version 3 of the License, or
176 # (at your option) any later version.
178 # This program is distributed in the hope that it will be useful,
179 # but WITHOUT ANY WARRANTY; without even the implied warranty of
180 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
181 # GNU General Public License for more details.
183 # You should have received a copy of the GNU General Public License
184 # along with this program. If not, see http://www.gnu.org/licenses/.
186 # - tr (GNU coreutils) 8.30
187 # Copyright (C) 2018 Free Software Foundation, Inc.
188 # License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
189 # This is free software: you are free to change and redistribute it.
190 # There is NO WARRANTY, to the extent permitted by law.
192 # Written by Jim Meyering.
194 # - head (GNU coreutils) 8.30
195 # Copyright (C) 2018 Free Software Foundation, Inc.
196 # License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
197 # This is free software: you are free to change and redistribute it.
198 # There is NO WARRANTY, to the extent permitted by law.
200 # Written by David MacKenzie and Jim Meyering.