aboutsummaryrefslogtreecommitdiff
path: root/gpg/card/makecard
diff options
context:
space:
mode:
Diffstat (limited to 'gpg/card/makecard')
-rwxr-xr-xgpg/card/makecard111
1 files changed, 111 insertions, 0 deletions
diff --git a/gpg/card/makecard b/gpg/card/makecard
new file mode 100755
index 0000000..cf6be0b
--- /dev/null
+++ b/gpg/card/makecard
@@ -0,0 +1,111 @@
+#!/bin/bash
+#
+# Creates an OpenPGP fingerprint business card.
+# This script uses an SVG file as a template and replaces
+# values such as $NAME and $FINGERPRINT with contents extracted
+# from a key.
+#
+# Note that this script uses "gpg --with-colons" to parse key information.
+# Documentation on the returned format can be found in "doc/DETAILS"
+# of the GnuPG distribution.
+
+set -e
+
+# Show help
+print_usage() {
+cat <<- EOF
+makecard - Create business cards with GnuPG
+
+ $0 [-t template] gpgid
+
+Print an SVG business card containing an OpenPGP fingerprint to standard output.
+
+ -t template
+ Use the file <template> as an SVG template for the business card.
+
+ gpgid
+ GnuPG id used on card. This may be anything accepted by gpg, such as an email address, key id or fingerprint.
+
+EOF
+}
+
+# Parameters
+#
+
+# GPG key identity to use
+GPGID=
+
+# Template file in which to replace variables
+TEMPLATE="multiple.svg"
+
+# Process command-line parameters
+while test $# -gt 0; do
+ case "$1" in
+ -h)
+ print_usage
+ exit 0
+ ;;
+ -t)
+ shift
+ if [ $# -gt 0 ]; then
+ TEMPLATE=$1
+ else
+ echo "No template specified. Aborting." 1>&2
+ exit 1
+ fi
+ ;;
+ *)
+ GPGID=$1
+ ;;
+ esac
+ shift
+done
+
+# Parameters processed beyond this point
+if [ -z "$GPGID" ]; then
+ echo "No gpg identity specified. Aborting." 1>&2
+ exit 1
+fi
+if [ ! -e "$TEMPLATE" ]; then
+ echo "Template not found. Aborting." 1>&2
+ exit 1
+fi
+
+
+# Compute number of keys available under given identity
+NUMKEYS=$(gpg --with-colons --list-keys "$GPGID" | awk -F: '/^fpr/{ print $10 }' | wc -l)
+
+# Only one key is supported by this script
+if [ "$NUMKEYS" -eq 0 ]; then
+ echo "No keys found for $GPGID. Aborting." 1>&2
+ exit 1
+fi
+if [ "$NUMKEYS" -gt 1 ]; then
+ echo "More than one key found for $GPGID. Aborting." 1>&2;
+ exit 1
+fi
+
+# Extract name
+USERID=$(gpg --with-colons --list-keys --fingerprint "$GPGID" | awk -F: '/^pub/{ print $10 }')
+NAME=$(echo "$USERID"| awk -F' <' '{ print $1 }')
+
+# Extract fingerprint
+FINGERPRINT=$(gpg --with-colons --list-keys "$GPGID" | awk -F: '/^fpr/{ print $10 }')
+# First 20 characters of fingerprint, separated intp groups of 4
+FP1=$(echo "$FINGERPRINT" | cut -c1-20 | fold -w4 | paste -sd\ -)
+# Second 20 characters of fingerprint, separated intp groups of 4
+FP2=$(echo "$FINGERPRINT" | cut -c21-40 | fold -w4 | paste -sd\ -)
+
+# Data to include in QR code
+# Remove "options=mr" from URL query to get human-readable output
+QRDATA="https://pgp.mit.edu/pks/lookup?op=get&options=mr&search=0x$FINGERPRINT"
+
+# Encode QR code
+QRCODE="data:image/png;base64,$(echo "$QRDATA" | qrencode -t PNG -m 0 -o - | base64)"
+
+# Replace variables in tamplate
+cat "$TEMPLATE" \
+ | sed "s/\$NAME/$NAME/g" \
+ | sed "s/\$FP1/$FP1/g" \
+ | sed "s/\$FP2/$FP2/g" \
+ | awk -v QRCODE="$QRCODE" '{ gsub(/\$QRCODE/, QRCODE); print }' \ No newline at end of file