feat(user):Add script to show gpg fingerprint with obscured email
authorSteven Baltakatei Sandoval <baltakatei@gmail.com>
Wed, 9 Mar 2022 16:34:25 +0000 (16:34 +0000)
committerSteven Baltakatei Sandoval <baltakatei@gmail.com>
Wed, 9 Mar 2022 16:34:25 +0000 (16:34 +0000)
- note: requires `b2sum`

 Changes to be committed:
new file:   user/bk-gpgfp-noemail

user/bk-gpgfp-noemail [new file with mode: 0644]

diff --git a/user/bk-gpgfp-noemail b/user/bk-gpgfp-noemail
new file mode 100644 (file)
index 0000000..9c77e65
--- /dev/null
@@ -0,0 +1,125 @@
+#!/usr/bin/env bash
+# Desc: Gets gpg fingerprint but replaces uid email addresses with hashes
+# Usage: bk-gpgfp-noemail 0xdc3469c9 74810B012346C9A6
+# Depends: gpg, b2sum
+
+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_resembles_gpg_fingerprint() {
+    # Desc: Checks if input string looks like gpg fingerprint
+    # Usage: check_resembles_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="${input:2}";
+       #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
+main() {
+    # Desc: main program
+    # Depends: gpg, b2sum
+    # Ref/Attrib: [1] Manipulating Strings. https://tldp.org/LDP/abs/html/string-manipulation.html
+    declare -a fingerprints
+    local gpg_text gpg_text_buffer pattern email
+    
+    # Check arguments
+    n=0;
+    for arg in "$@"; do
+       ((n++));
+       #yell "DEBUG:n:$n";
+       #yell "DEBUG:arg:$arg";
+
+       # Check if arg resembles gpg fignerprint
+       if check_resembles_gpg_fingerprint "$arg"; then
+           #yell "DEBUG:Resembles a gpg fingerprint:$arg";
+           fingerprints+=("$arg");
+           #yell "DEBUG:fingerprints:$(declare -p fingerprints)";
+       else
+           die "ERROR:Doesn't resemble a gpg fingerprint:$arg";
+       fi;
+    done;
+
+    # Process fingerprints
+    for arg in "${fingerprints[@]}"; do
+       # Get gpg fingerprint text
+       gpg_text="$(gpg --fingerprint --fingerprint "$arg" 2>&1)";
+       #yell "DEBUG:gpg_text:$gpg_text";
+
+       # Trim emails
+       gpg_text_buffer="";
+       while IFS= read -r line; do
+           #yell "DEBUG:line:$line";
+           ## Read $gpg_text line-by-line
+
+           # Skip lines that don't start with 'uid'
+           pattern="^uid";
+           if [[ ! $line =~ $pattern ]]; then
+               #yell "DEBUG:line doesn't start with \"uid\":$line";
+               gpg_text_buffer="$(printf "%s\n%s" "$gpg_text_buffer" "$line")";
+               continue;
+           fi;
+
+           # Trim email from $line
+           ## Get email
+           email="$(expr match "$line" '.*\(<.*$\)')";
+           #yell "DEBUG:email1:$email";
+           email="${email%>*}";
+           #yell "DEBUG:email2:$email";
+           email="${email#*<}";
+           #yell "DEBUG:email3:$email";
+
+           ## Strip email from $line
+           line="${line% <*}"; # See [1]
+           #yell "DEBUG:line1:$line";
+           
+           ## Add hashed email if $email length non-zero
+           if [[ -n $email ]]; then
+               email_hash="$(echo -n "$email" | b2sum -l32 | cut -d' ' -f1)"; # hash email using b2sum
+               #yell "DEBUG:email_hash:$email_hash";
+               line="$line <$email_hash>";
+               #yell "DEBUG:line3:$line";
+           fi;
+           
+           ## Append $line to $gpg_text_buffer
+           gpg_text_buffer="$(printf "%s\n%s" "$gpg_text_buffer" "$line")";
+           #done < <(echo "$gpg_text");
+       done < <(printf "%s" "$gpg_text");
+       echo "$gpg_text_buffer";
+    done;
+}; # main program
+
+main "$@";
+
+# Author: Steven Baltakatei Sandoval
+# License: GPLv3+