feat(user/rsync_tranches):Make more granular
[BK-2020-03.git] / user / bknpass
CommitLineData
2feb5df8
SBS
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
61let ALPHABET_SIZE="32" # number of unique chars in bech32 base32 charset
62LOG_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
66function echoerr {
67 echo "$@" 1>&2;
68}
69
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
73function 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
82if [ -z "$1" ]
83then
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
88else
89 ENTROPY_BIT_COUNT1="$1"
90fi
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
94RETEST1='^[0-9]+$'
95if ! [[ $ENTROPY_BIT_COUNT1 =~ $RETEST1 ]] ; then
96 echo "error: Not an integer." >&2; exit 1
97fi
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
105CHAR_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.
108
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
111CHAR_COUNT1=$(echo "$CHAR_COUNT1_FLOAT" | awk '{print ($0-int($0)>0)?int($0)+1:int($0)}')
112
113# Generate passphrase
114PASS1=$(rpass "$CHAR_COUNT1")
115echo -e "$PASS1"
116
117
118#==References==
119#
120# - How to echo a string as stderr instead of stdout.
121# https://stackoverflow.com/a/2990533
122# Author: James Roth
123# Date: 2010-06-07T14:52Z
124# Date Accessed: 2020-01-20
125#
126# - How to check if script argument exists or not.
127# https://stackoverflow.com/a/6482403
128# Author: phoxix
129# Date: 2011-06-26T05:55Z
130# Date Accessed: 2020-01-20
131#
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
137#
138# - How to use `bc` to calculate logarithms in Bash
139# http://phodd.net/gnu-bc/bcfaq.html#bashlog
140# Author: unknown
141# Date Accessed: 2020-01-20
142#
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
146# Date: 2015-01-09
147# Date Accessed: 2020-01-20
148#
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
152# Date: 2010-04-21
153# Date Accessed: 2020-01-20
154#
155# - Bech32 base32 charset
156# https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki
157# Author: Pieter Wuille <pieter.wuille@gmail.com>
158# Date: 2017-03-20
159# License: BSD-2-Clause
160# Date: Accessed: 2021-01-23
161#
162# - Dependencies: bash, echo, bc, awk, tr, head.
163#
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.
169#
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.
175#
176# Written by Brian Fox and Chet Ramey.
177#
178# - bc 1.07.1
179# Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
180#
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.
183#
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.
188#
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.
193#
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/.
196#
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.
202#
203# Written by Jim Meyering.
204#
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.
210#
211# Written by David MacKenzie and Jim Meyering.