fix(unitproc/bkshuf):Make executable
[BK-2020-03.git] / unitproc / bkt-check_resembles_gpg_fingerprint
CommitLineData
466cd0f1
SBS
1#!/usr/bin/env bash
2
3yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
4die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
5try() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
9ac02fe8 6check_resembles_gpg_fingerprint() {
466cd0f1 7 # Desc: Checks if input string looks like gpg fingerprint
9ac02fe8 8 # Usage: check_resembles_gpg_fingerprint arg1
466cd0f1
SBS
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
9ac02fe8 25 input="${input:2}";
466cd0f1
SBS
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
49myVar="0xdc3469c9";
9ac02fe8
SBS
50if check_resembles_gpg_fingerprint "$myVar"; then
51 yell "Looks like a gpg fingerprint:$myVar";
52else
53 yell "Doesn't look like a gpg fingerprint:$myVar";
54fi;
55
56myVar="69B4C4CDC628F8F9";
57if check_resembles_gpg_fingerprint "$myVar"; then
58 yell "Looks like a gpg fingerprint:$myVar";
59else
60 yell "Doesn't look like a gpg fingerprint:$myVar";
61fi;
62
63myVar="26646D99CBAEC9B81982EF6029D9EE6B1FC730C1";
64if check_resembles_gpg_fingerprint "$myVar"; then
65 yell "Looks like a gpg fingerprint:$myVar";
66else
67 yell "Doesn't look like a gpg fingerprint:$myVar";
68fi;
69
70myVar="deadbeef";
71if check_resembles_gpg_fingerprint "$myVar"; then
72 yell "Looks like a gpg fingerprint:$myVar";
73else
74 yell "Doesn't look like a gpg fingerprint:$myVar";
75fi;
76
77myVar="foobar";
78if check_resembles_gpg_fingerprint "$myVar"; then
79 yell "Looks like a gpg fingerprint:$myVar";
80else
81 yell "Doesn't look like a gpg fingerprint:$myVar";
82fi;
83
84myVar="zzzzzzzz";
85if check_resembles_gpg_fingerprint "$myVar"; then
466cd0f1
SBS
86 yell "Looks like a gpg fingerprint:$myVar";
87else
88 yell "Doesn't look like a gpg fingerprint:$myVar";
89fi;