X-Git-Url: https://zdv2.bktei.com/gitweb/BK-2020-03.git/blobdiff_plain/0d22dfcdb7eab1975d56ed36967885707470c828..203add48226f4fb9e6dbdde301e70daa15ab1a54:/user/bknpass?ds=sidebyside diff --git a/user/bknpass b/user/bknpass index 4f53da0..678ab7f 100755 --- a/user/bknpass +++ b/user/bknpass @@ -1,62 +1,37 @@ #!/bin/bash - -# Date: 2020-01-20T16:34Z -# -# Author: Steven Baltakatei Sandoval (baltakatei.com) -# -# License: This bash script, `bknpass`, is licensed under GPLv3 or -# later by Steven Baltakatei Sandoval: -# -# `bknpass`, an alphanumeric password generator -# Copyright (C) 2020 Steven Baltakatei Sandoval (baltakatei.com) -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# A copy of the GNU General Public License may be found at -# . -# -# Description: This bash script generates alphanumeric passphrases -# with a char-count determined by a user-provided number of bits of -# entropy. The passphrase is then outputted to stdout with a trailing -# newline. It works as follows: +# Desc: Generate alphanumeric passphrase with specified bits of entropy. +# Usage: bknpass [int] +# Example: bknpass 128 +# Depends: bash, echo, bc, awk, tr, head, Debian 10 +# Version: 0.1.0 +# Note: This bash script generates alphanumeric passphrases +# with a char-count determined by a user-provided number of bits of +# entropy. The passphrase is then outputted to stdout with a trailing +# newline. It works as follows: # -# - Prompt user for an integer. This integer is the number of bits -# of entropy that the generated password should have. -# -# - Check if user-provided string is an integer using `bash` regular -# expression test. -# -# - Calculate the minimum number of alphanumeric characters required -# to encode the specified number of bits of entropy. +# - Prompt user for an integer. This integer is the number of bits +# of entropy that the generated password should have. # -# - This step uses `bc` to calculate a logarithm float string -# and `awk` to convert the float into an integer, rounding up. +# - Check if user-provided string is an integer using `bash` regular +# expression test. # -# - Use `tr`, `/dev/urandom`, and `head` to generate a random -# alphanumeric string with the length calculated in the previous -# step. +# - Calculate the minimum number of bech32 base32 characters +# required to encode the specified number of bits of entropy. # -# - Use `echo` to display the passphrase in stdout with a trailing -# newline. +# - This step uses `bc` to calculate a logarithm float string +# and `awk` to convert the float into an integer, rounding up. # -# Dependencies: bash, echo, bc, awk, tr, head. See end of file +# - Use `tr`, `/dev/urandom`, and `head` to generate a random +# alphanumeric string with the length calculated in the previous +# step. # -# Tested on: -# -# - GNU/Linux Debian 10 +# - Use `echo` to display the passphrase in stdout with a trailing +# newline. #==Initialization== -let ALPHABET_SIZE="26+26+10" # number of unique chars in [:alnum:], argument fed to `tr -c` in 'Generate passphrase' step) +let ALPHABET_SIZE="32" # number of unique chars in bech32 base32 charset LOG_BASE=2 # Set logarithm base to 2 # Define `echoerr` function which outputs text to stderr @@ -65,17 +40,18 @@ function echoerr { echo "$@" 1>&2; } -# Define `rpass` function which generates an alphanumeric passphrase of length $1 (ex: `rpass 22` generates a 22-char string) - # Note: function adapted from https://www.thegeekstuff.com/2010/04/unix-bash-function-examples/ +# Define `rpass` function which generates a base32 passphrase of length $1 (ex: `rpass 20` generates a 20-char string) +# Note: function adapted from https://www.thegeekstuff.com/2010/04/unix-bash-function-examples/ +# Note: base32 charset uses bech32 charset function rpass { - cat /dev/urandom | LC_ALL=C tr -cd "[:alnum:]" | head -c ${1:-22} + cat /dev/urandom | LC_ALL=C tr -cd "qpzry9x8gf2tvdw0s3jn54khce6mua7l" | head -c ${1:-22} } #==Main Program== # Define $ENTROPY_BIT_COUNT1 as argument $1 or prompt user if $1 is not defined. - # note: argument test adapted from https://stackoverflow.com/a/6482403 +# Note: argument test adapted from https://stackoverflow.com/a/6482403 if [ -z "$1" ] then echo "Entropy bit count argument (\$1) not supplied." @@ -87,23 +63,24 @@ else fi # Check if $ENTROPY_BIT_COUNT1 is an non-negative integer - # Note: Regular expression test is adapted from https://stackoverflow.com/a/806923 +# Note: Regular expression test is adapted from https://stackoverflow.com/a/806923 RETEST1='^[0-9]+$' if ! [[ $ENTROPY_BIT_COUNT1 =~ $RETEST1 ]] ; then echo "error: Not an integer." >&2; exit 1 fi # Calculate minimum count of chars needed to encode $ENTROPY_BIT_COUNT1 with alphabet size of $ALPHABET_SIZE as float - # Solve ln(a^n)/ln(2)=b for n using `bc` where - # a=$ALPHABET_SIZE - # n=$CHAR_COUNT1_FLOAT - # b=$ENTROPY_BIT_COUNT1 - # Note: `bc` logarithm usage adapted from http://phodd.net/gnu-bc/bcfaq.html#bashlog +# Solve ln(a^n)/ln(2)=b for n using `bc` where +# a=$ALPHABET_SIZE +# n=$CHAR_COUNT1_FLOAT +# b=$ENTROPY_BIT_COUNT1 +# Note: `bc` logarithm usage adapted from http://phodd.net/gnu-bc/bcfaq.html#bashlog CHAR_COUNT1_FLOAT=$(echo "$ENTROPY_BIT_COUNT1*l($LOG_BASE)/l($ALPHABET_SIZE)" | bc -l) - # Note: Float will be of form "21.49744370650136860806". This particular float should be rounded to "22" later. +# Note: Float will be of form "21.49744370650136860806". +# Note: This particular example float should be rounded to "22" later. # Round $CHAR_COUNT1_FLOAT1 up to next highest integer for use as argument in later bash functions. - # Note: awk expression from https://bits.mdminhazulhaque.io/linux/round-number-in-bash-script.html +# Note: awk expression from https://bits.mdminhazulhaque.io/linux/round-number-in-bash-script.html CHAR_COUNT1=$(echo "$CHAR_COUNT1_FLOAT" | awk '{print ($0-int($0)>0)?int($0)+1:int($0)}') # Generate passphrase @@ -148,6 +125,13 @@ echo -e "$PASS1" # Date: 2010-04-21 # Date Accessed: 2020-01-20 # +# - Bech32 base32 charset +# https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki +# Author: Pieter Wuille +# Date: 2017-03-20 +# License: BSD-2-Clause +# Date: Accessed: 2021-01-23 +# # - Dependencies: bash, echo, bc, awk, tr, head. # # - GNU bash, version 5.0.3(1)-release (x86_64-pc-linux-gnu) @@ -198,3 +182,6 @@ echo -e "$PASS1" # There is NO WARRANTY, to the extent permitted by law. # # Written by David MacKenzie and Jim Meyering. + +# Author: Steven Baltakatei Sandoval +# License: GPLv3+