| 1 | #!/usr/bin/env bash |
| 2 | # Desc: Gets gpg fingerprint but replaces uid email addresses with hashes |
| 3 | # Usage: bk-gpgfp-noemail 0xdc3469c9 74810B012346C9A6 |
| 4 | # Depends: gpg, b2sum |
| 5 | |
| 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 |
| 12 | # Input: arg1: string |
| 13 | # Output: exit code: 0 if arg1 is fingerprint, 1 otherwise |
| 14 | # Depends: yell(), die(), try() |
| 15 | # Version: 0.0.1 |
| 16 | local pattern1 pattern2 input input_length |
| 17 | |
| 18 | # Check args |
| 19 | if [[ $# -ne 1 ]]; then |
| 20 | die "ERROR:Invalid number of arguments:$#"; |
| 21 | else |
| 22 | input="$1"; |
| 23 | fi; |
| 24 | |
| 25 | ## Trim leading `0x` |
| 26 | pattern1="(0x)(.*)"; |
| 27 | if [[ $input =~ $pattern1 ]]; then |
| 28 | input="${input:2}"; |
| 29 | #yell "DEBUG:input:$input"; |
| 30 | fi; |
| 31 | |
| 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"; |
| 36 | return 1; |
| 37 | fi; |
| 38 | |
| 39 | ## Check if hexadecimal |
| 40 | pattern2="[0-9A-Fa-f]{8,40}"; |
| 41 | if [[ $1 =~ $pattern2 ]]; then |
| 42 | #yell "DEBUG:is a fingerprint:$arg"; |
| 43 | return 0; |
| 44 | else |
| 45 | #yell "DEBUG:Not a fingerprint:$arg"; |
| 46 | return 1; |
| 47 | fi; |
| 48 | }; # Checks if input string looks like gpg fingerprint |
| 49 | main() { |
| 50 | # Desc: main program |
| 51 | # Depends: gpg, b2sum |
| 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 |
| 55 | |
| 56 | # Check arguments |
| 57 | n=0; |
| 58 | for arg in "$@"; do |
| 59 | ((n++)); |
| 60 | #yell "DEBUG:n:$n"; |
| 61 | #yell "DEBUG:arg:$arg"; |
| 62 | |
| 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)"; |
| 68 | else |
| 69 | die "ERROR:Doesn't resemble a gpg fingerprint:$arg"; |
| 70 | fi; |
| 71 | done; |
| 72 | |
| 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"; |
| 78 | |
| 79 | # Trim emails |
| 80 | gpg_text_buffer=""; |
| 81 | while IFS= read -r line; do |
| 82 | #yell "DEBUG:line:$line"; |
| 83 | ## Read $gpg_text line-by-line |
| 84 | |
| 85 | # Skip lines that don't start with 'uid' |
| 86 | pattern="^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")"; |
| 90 | continue; |
| 91 | fi; |
| 92 | |
| 93 | # Trim email from $line |
| 94 | ## Get email |
| 95 | email="$(expr match "$line" '.*\(<.*$\)')"; |
| 96 | #yell "DEBUG:email1:$email"; |
| 97 | email="${email%>*}"; |
| 98 | #yell "DEBUG:email2:$email"; |
| 99 | email="${email#*<}"; |
| 100 | #yell "DEBUG:email3:$email"; |
| 101 | |
| 102 | ## Strip email from $line |
| 103 | line="${line% <*}"; # See [1] |
| 104 | #yell "DEBUG:line1:$line"; |
| 105 | |
| 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"; |
| 112 | fi; |
| 113 | |
| 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"; |
| 119 | done; |
| 120 | }; # main program |
| 121 | |
| 122 | main "$@"; |
| 123 | |
| 124 | # Author: Steven Baltakatei Sandoval |
| 125 | # License: GPLv3+ |