From: Steven Baltakatei Sandoval Date: Thu, 3 Mar 2022 00:56:05 +0000 (+0000) Subject: feat(unitproc/bknpass):Make portable if script used in bash function X-Git-Tag: 0.5.0~17 X-Git-Url: https://zdv2.bktei.com/gitweb/BK-2020-03.git/commitdiff_plain/6536fdb116be68445d5f712f1b07ff9950628d0d feat(unitproc/bknpass):Make portable if script used in bash function - feat(unitproc/bknpass): - Exit on too many arguments. - Remove repeat chars (for future version when charset specified by option) --- diff --git a/unitproc/bknpass b/unitproc/bknpass index 094b7d6..f4930a0 100755 --- a/unitproc/bknpass +++ b/unitproc/bknpass @@ -3,7 +3,7 @@ # 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 @@ -52,10 +52,14 @@ main() { # [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; @@ -78,7 +82,7 @@ main() { ### 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] @@ -87,7 +91,7 @@ main() { 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== #