feat(unitproc):Add script to check if string is gpg fingerprint
[BK-2020-03.git] / unitproc / bktemp-check_gpg_fingerprint
1 #!/usr/bin/env bash
2
3 yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
4 die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
5 try() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
6 check_gpg_fingerprint() {
7 # Desc: Checks if input string looks like gpg fingerprint
8 # Usage: check_gpg_fingerprint arg1
9 # Input: arg1: string
10 # Output: exit code: 0 if arg1 is fingerprint, 1 otherwise
11 # Depends: yell(), die(), try()
12 # Version: 0.0.1
13 local pattern1 pattern2 input input_length
14
15 # Check args
16 if [[ $# -ne 1 ]]; then
17 die "ERROR:Invalid number of arguments:$#";
18 else
19 input="$1";
20 fi;
21
22 ## Trim leading `0x`
23 pattern1="(0x)(.*)";
24 if [[ $input =~ $pattern1 ]]; then
25 input="$(echo "$input" | sed 's/^..//')";
26 #yell "DEBUG:input:$input";
27 fi;
28
29 ## Check if char count multiple of 8
30 input_length="${#input}";
31 if [[ ! $(( input_length % 8 )) -eq 0 ]]; then
32 yell "DEBUG:Length not a multiple of 8:$input_length:$input";
33 return 1;
34 fi;
35
36 ## Check if hexadecimal
37 pattern2="[0-9A-Fa-f]{8,40}";
38 if [[ $1 =~ $pattern2 ]]; then
39 #yell "DEBUG:is a fingerprint:$arg";
40 return 0;
41 else
42 #yell "DEBUG:Not a fingerprint:$arg";
43 return 1;
44 fi;
45 }; # Checks if input string looks like gpg fingerprint
46
47
48 # test code
49 myVar="0xdc3469c9";
50 if check_gpg_fingerprint "$myVar"; then
51 yell "Looks like a gpg fingerprint:$myVar";
52 else
53 yell "Doesn't look like a gpg fingerprint:$myVar";
54 fi;