X-Git-Url: https://zdv2.bktei.com/gitweb/BK-2020-03.git/blobdiff_plain/c21c5eeec4a0ed0112707c4699b45b04f4c79edd..e2912511058c16ba658f02e34278c3c352d5b1ed:/unitproc/bknpass?ds=sidebyside 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== #