2 # Desc: Gets gpg fingerprint but replaces uid email addresses with hashes
3 # Usage: bk-gpgfp-noemail 0xdc3469c9 74810B012346C9A6
6 yell
() { echo "$0: $*" >&2; } # print script path and all args to stderr
7 die
() { yell
"$*"; exit 111; } # same as yell() but non-zero exit status
8 try
() { "$@" || die
"cannot $*"; } # runs args as command, reports args if command fails
9 check_resembles_gpg_fingerprint
() {
10 # Desc: Checks if input string looks like gpg fingerprint
11 # Usage: check_resembles_gpg_fingerprint arg1
13 # Output: exit code: 0 if arg1 is fingerprint, 1 otherwise
14 # Depends: yell(), die(), try()
16 local pattern1 pattern2 input input_length
19 if [[ $# -ne 1 ]]; then
20 die
"ERROR:Invalid number of arguments:$#";
27 if [[ $input =~
$pattern1 ]]; then
29 #yell "DEBUG:input:$input";
32 ## Check if char count multiple of 8
33 input_length
="${#input}";
34 if [[ ! $
(( input_length
% 8 )) -eq 0 ]]; then
35 yell
"DEBUG:Length not a multiple of 8:$input_length:$input";
39 ## Check if hexadecimal
40 pattern2
="[0-9A-Fa-f]{8,40}";
41 if [[ $1 =~
$pattern2 ]]; then
42 #yell "DEBUG:is a fingerprint:$arg";
45 #yell "DEBUG:Not a fingerprint:$arg";
48 }; # Checks if input string looks like gpg fingerprint
52 # Ref/Attrib: [1] Manipulating Strings. https://tldp.org/LDP/abs/html/string-manipulation.html
53 declare -a fingerprints
54 local gpg_text gpg_text_buffer pattern email
61 #yell "DEBUG:arg:$arg";
63 # Check if arg resembles gpg fignerprint
64 if check_resembles_gpg_fingerprint
"$arg"; then
65 #yell "DEBUG:Resembles a gpg fingerprint:$arg";
66 fingerprints
+=("$arg");
67 #yell "DEBUG:fingerprints:$(declare -p fingerprints)";
69 die
"ERROR:Doesn't resemble a gpg fingerprint:$arg";
73 # Process fingerprints
74 for arg
in "${fingerprints[@]}"; do
75 # Get gpg fingerprint text
76 gpg_text
="$(gpg --fingerprint --fingerprint "$arg" 2>&1)";
77 #yell "DEBUG:gpg_text:$gpg_text";
81 while IFS
= read -r line
; do
82 #yell "DEBUG:line:$line";
83 ## Read $gpg_text line-by-line
85 # Skip lines that don't start with 'uid'
87 if [[ ! $line =~
$pattern ]]; then
88 #yell "DEBUG:line doesn't start with \"uid\":$line";
89 gpg_text_buffer
="$(printf "%s
\n%s
" "$gpg_text_buffer" "$line")";
93 # Trim email from $line
95 email
="$(expr match "$line" '.*\(<.*$\)')";
96 #yell "DEBUG:email1:$email";
98 #yell "DEBUG:email2:$email";
100 #yell "DEBUG:email3:$email";
102 ## Strip email from $line
103 line
="${line% <*}"; # See [1]
104 #yell "DEBUG:line1:$line";
106 ## Add hashed email if $email length non-zero
107 if [[ -n $email ]]; then
108 email_hash
="$(echo -n "$email" | b2sum -l32 | cut -d' ' -f1)"; # hash email using b2sum
109 #yell "DEBUG:email_hash:$email_hash";
110 line
="$line <$email_hash>";
111 #yell "DEBUG:line3:$line";
114 ## Append $line to $gpg_text_buffer
115 gpg_text_buffer
="$(printf "%s
\n%s
" "$gpg_text_buffer" "$line")";
116 #done < <(echo "$gpg_text");
117 done < <(printf "%s" "$gpg_text");
118 echo "$gpg_text_buffer";
124 # Author: Steven Baltakatei Sandoval