aboutsummaryrefslogtreecommitdiff
path: root/gpg/card/makecard
blob: cf6be0b79b5b8d4fb2889f07cb231ec2188735ee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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 }'