# Usage: bknpass [integer]
# Example: bknpass 256
# Result: 9tnzcl0m4dsm22a95525zj93jj
-# Version: 0.2.0
+# Version: 0.2.1
# Depends: bash 5.1.8, GNU coreutils 8.32, bc 1.07.1, awk 5.1.0
# License:
# `bknpass`, an alphanumeric password generator
# [4] "Logarithms in GNU bc". https://web.archive.org/web/20210211050732/http://phodd.net/gnu-bc/bcfaq.html#bclog
# [5] "BIP 173". Note: bech32 charset. https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki
# [6] "Bash Function to generate random password". https://www.thegeekstuff.com/2010/04/unix-bash-function-examples/
- charset="qpzry9x8gf2tvdw0s3jn54khce6mua7l"; # bech32. See [2].
+ charset="qpzry9x8gf2tvdw0s3jn54khce6mua7l"; # bech32. See [5].
+ charset="$(tr -s "$charset" < <(echo -n "$charset"))"; # remove repeat chars
alphabet_size="$(echo -n "$charset" | wc -c)"; # number of unique chars
log_base=2; # entropy unit base (i.e. 2 for "bits of entropy")
+ # Check inputs
+ if [[ $# -gt 1 ]]; then showUsage; die "ERROR:Too many arguments:$#"; fi;
+
# Check for bc
if ! command -v bc 1>/dev/null 2>&1; then die "ERROR:bc not available"; fi;
### a=$alphabet_size
### n=$char_count_float
### b=$entropy_bit_count
- char_count_float="$(echo "$entropy_bit_count*l($log_base)/l($alphabet_size)" | bc -l)";
+ char_count_float="$(echo "$entropy_bit_count*l($log_base)/l($alphabet_size)" | bc -l)"; # See [4]
## Round $char_count_float to next highest integer
char_count="$(echo "$char_count_float" | awk '{print ($0-int($0)>0)?int($0)+1:int($0)}')"; # See [3]
echo "$(LC_ALL=C tr -cd "$charset" < /dev/urandom | head -c "$char_count")"; # See [6]
}; # main program
-main "$@";
+( main "$@" ); # run 'main()' in subshell for 'die()' portability of script as sourced bash function.
#==References==
#