chore(user/mw_get_audiobook_chapters.sh):Clean up desc
[BK-2020-03.git] / unitproc / bkt-check_resembles_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_resembles_gpg_fingerprint() {
7 # Desc: Checks if input string looks like gpg fingerprint
8 # Usage: check_resembles_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="${input:2}";
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_resembles_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;
55
56 myVar="69B4C4CDC628F8F9";
57 if check_resembles_gpg_fingerprint "$myVar"; then
58 yell "Looks like a gpg fingerprint:$myVar";
59 else
60 yell "Doesn't look like a gpg fingerprint:$myVar";
61 fi;
62
63 myVar="26646D99CBAEC9B81982EF6029D9EE6B1FC730C1";
64 if check_resembles_gpg_fingerprint "$myVar"; then
65 yell "Looks like a gpg fingerprint:$myVar";
66 else
67 yell "Doesn't look like a gpg fingerprint:$myVar";
68 fi;
69
70 myVar="deadbeef";
71 if check_resembles_gpg_fingerprint "$myVar"; then
72 yell "Looks like a gpg fingerprint:$myVar";
73 else
74 yell "Doesn't look like a gpg fingerprint:$myVar";
75 fi;
76
77 myVar="foobar";
78 if check_resembles_gpg_fingerprint "$myVar"; then
79 yell "Looks like a gpg fingerprint:$myVar";
80 else
81 yell "Doesn't look like a gpg fingerprint:$myVar";
82 fi;
83
84 myVar="zzzzzzzz";
85 if check_resembles_gpg_fingerprint "$myVar"; then
86 yell "Looks like a gpg fingerprint:$myVar";
87 else
88 yell "Doesn't look like a gpg fingerprint:$myVar";
89 fi;