From: Steven Baltakatei Sandoval Date: Wed, 9 Mar 2022 14:37:27 +0000 (+0000) Subject: feat(unitproc):Add script to check if string is gpg fingerprint X-Git-Tag: 0.5.0~16 X-Git-Url: https://zdv2.bktei.com/gitweb/BK-2020-03.git/commitdiff_plain/466cd0f1c38d44757e7f45e53f7c3cf358824a77?ds=sidebyside;hp=--cc feat(unitproc):Add script to check if string is gpg fingerprint --- 466cd0f1c38d44757e7f45e53f7c3cf358824a77 diff --git a/unitproc/bktemp-check_gpg_fingerprint b/unitproc/bktemp-check_gpg_fingerprint new file mode 100644 index 0000000..e2af768 --- /dev/null +++ b/unitproc/bktemp-check_gpg_fingerprint @@ -0,0 +1,54 @@ +#!/usr/bin/env bash + +yell() { echo "$0: $*" >&2; } # print script path and all args to stderr +die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status +try() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails +check_gpg_fingerprint() { + # Desc: Checks if input string looks like gpg fingerprint + # Usage: check_gpg_fingerprint arg1 + # Input: arg1: string + # Output: exit code: 0 if arg1 is fingerprint, 1 otherwise + # Depends: yell(), die(), try() + # Version: 0.0.1 + local pattern1 pattern2 input input_length + + # Check args + if [[ $# -ne 1 ]]; then + die "ERROR:Invalid number of arguments:$#"; + else + input="$1"; + fi; + + ## Trim leading `0x` + pattern1="(0x)(.*)"; + if [[ $input =~ $pattern1 ]]; then + input="$(echo "$input" | sed 's/^..//')"; + #yell "DEBUG:input:$input"; + fi; + + ## Check if char count multiple of 8 + input_length="${#input}"; + if [[ ! $(( input_length % 8 )) -eq 0 ]]; then + yell "DEBUG:Length not a multiple of 8:$input_length:$input"; + return 1; + fi; + + ## Check if hexadecimal + pattern2="[0-9A-Fa-f]{8,40}"; + if [[ $1 =~ $pattern2 ]]; then + #yell "DEBUG:is a fingerprint:$arg"; + return 0; + else + #yell "DEBUG:Not a fingerprint:$arg"; + return 1; + fi; +}; # Checks if input string looks like gpg fingerprint + + +# test code +myVar="0xdc3469c9"; +if check_gpg_fingerprint "$myVar"; then + yell "Looks like a gpg fingerprint:$myVar"; +else + yell "Doesn't look like a gpg fingerprint:$myVar"; +fi;