feat(archive):Add baltakatei pgp signing public key
[BK-2020-03.git] / user / bknpass
1 #!/bin/bash
2
3 # Date: 2020-01-20T16:34Z
4 #
5 # Author: Steven Baltakatei Sandoval (baltakatei.com)
6 #
7 # License: This bash script, `bknpass`, is licensed under GPLv3 or
8 # later by Steven Baltakatei Sandoval:
9 #
10 # `bknpass`, an alphanumeric password generator
11 # Copyright (C) 2020 Steven Baltakatei Sandoval (baltakatei.com)
12 #
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
16 # any later version.
17 #
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.
22 #
23 # A copy of the GNU General Public License may be found at
24 # <https://www.gnu.org/licenses/>.
25 #
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:
30 #
31 # - Prompt user for an integer. This integer is the number of bits
32 # of entropy that the generated password should have.
33 #
34 # - Check if user-provided string is an integer using `bash` regular
35 # expression test.
36 #
37 # - Calculate the minimum number of alphanumeric characters required
38 # to encode the specified number of bits of entropy.
39 #
40 # - This step uses `bc` to calculate a logarithm float string
41 # and `awk` to convert the float into an integer, rounding up.
42 #
43 # - Use `tr`, `/dev/urandom`, and `head` to generate a random
44 # alphanumeric string with the length calculated in the previous
45 # step.
46 #
47 # - Use `echo` to display the passphrase in stdout with a trailing
48 # newline.
49 #
50 # Dependencies: bash, echo, bc, awk, tr, head. See end of file
51 #
52 # Tested on:
53 #
54 # - GNU/Linux Debian 10
55
56
57 #==Initialization==
58
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
61
62 # Define `echoerr` function which outputs text to stderr
63 # Note: function copied from https://stackoverflow.com/a/2990533
64 function echoerr {
65 echo "$@" 1>&2;
66 }
67
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/
70 function rpass {
71 cat /dev/urandom | LC_ALL=C tr -cd "[:alnum:]" | head -c ${1:-22}
72 }
73
74
75 #==Main Program==
76
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
79 if [ -z "$1" ]
80 then
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
85 else
86 ENTROPY_BIT_COUNT1="$1"
87 fi
88
89 # Check if $ENTROPY_BIT_COUNT1 is an non-negative integer
90 # Note: Regular expression test is adapted from https://stackoverflow.com/a/806923
91 RETEST1='^[0-9]+$'
92 if ! [[ $ENTROPY_BIT_COUNT1 =~ $RETEST1 ]] ; then
93 echo "error: Not an integer." >&2; exit 1
94 fi
95
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
98 # a=$ALPHABET_SIZE
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.
104
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)}')
108
109 # Generate passphrase
110 PASS1=$(rpass "$CHAR_COUNT1")
111 echo -e "$PASS1"
112
113
114 #==References==
115 #
116 # - How to echo a string as stderr instead of stdout.
117 # https://stackoverflow.com/a/2990533
118 # Author: James Roth
119 # Date: 2010-06-07T14:52Z
120 # Date Accessed: 2020-01-20
121 #
122 # - How to check if script argument exists or not.
123 # https://stackoverflow.com/a/6482403
124 # Author: phoxix
125 # Date: 2011-06-26T05:55Z
126 # Date Accessed: 2020-01-20
127 #
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
133 #
134 # - How to use `bc` to calculate logarithms in Bash
135 # http://phodd.net/gnu-bc/bcfaq.html#bashlog
136 # Author: unknown
137 # Date Accessed: 2020-01-20
138 #
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
142 # Date: 2015-01-09
143 # Date Accessed: 2020-01-20
144 #
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
148 # Date: 2010-04-21
149 # Date Accessed: 2020-01-20
150 #
151 # - Dependencies: bash, echo, bc, awk, tr, head.
152 #
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.
158 #
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.
164 #
165 # Written by Brian Fox and Chet Ramey.
166 #
167 # - bc 1.07.1
168 # Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
169 #
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.
172 #
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.
177 #
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.
182 #
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/.
185 #
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.
191 #
192 # Written by Jim Meyering.
193 #
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.
199 #
200 # Written by David MacKenzie and Jim Meyering.