-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcam2gpg
executable file
·106 lines (90 loc) · 2.63 KB
/
cam2gpg
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
#!/bin/sh -e
## Manage arguments.
##
## By default standard input / output are used.
output="/dev/stdout"
while getopts "o:p:" opt
do
case "${opt}" in
o) output=${OPTARG} ;;
p) public=${OPTARG} ;;
*) cat >&2 << EOF
usage: ${0##*/} [OPTIONS]
QR code to OpenPGP converter
-o OUTPUT Redirect output to this file instead of standard-output.
-p PUBLIC File to read public key information from.
EOF
exit 1 ;;
esac
done
## Manage the temporary directory.
##
## In modern Linux systems /dev/shm is a tmpfs mount available for unprivileged
## users. The /tmp directory is used as backup.
##
## The temporary directory is removed at exit.
tmp=$(test -d /dev/shm && TMPDIR=/dev/shm mktemp -d)
trap "rm --recursive ${tmp}; pkill --parent $$" EXIT
chmod 700 ${tmp}
## Read all the QR codes and reassemble them using the headers.
##
## <index: 000> + <size: 01B> + <payload>
## <index: 001> + <size: 01B> + <payload>
## ...
## <index: 01A> + <size: 01B> + <payload>
##
## The resulting data is a base 64 encoded string.
mkfifo ${tmp}/data.fifo
zbarcam --set disable --set qrcode.enable --raw > ${tmp}/data.fifo &
gawk ' {
i = strtonum("0x" substr($0, 0, 3));
s = strtonum("0x" substr($0, 4, 3));
if (i >= s) {
print "Invalid index / size pair" > "/dev/stderr";
exit 1;
} else if (size != 0 && size != s) {
print "Invalid size" > "/dev/stderr";
exit 1;
} else if (table[i] == 0) {
table[i] = substr($0, 7);
size = s;
read++;
if (read == size) {
print "Got all QR codes" > "/dev/stderr";
exit 0;
} else {
print "Read QR code #" i \
" (" read "/" size ")" > "/dev/stderr";
}
}
} END {
if (size == 0 || size != read) {
print "Missing QR codes" > "/dev/stderr";
exit 1;
}
for (i = 0; i < size; i++) {
printf("%s", table[i]);
}
}' < ${tmp}/data.fifo > ${tmp}/data.b64
# Decode the base 64 encoded string.
base64 --decode ${tmp}/data.b64 > ${tmp}/data.bin
## The input data can be a secret key or a revocation certificate.
##
## Revocation certificates are using GPG format and can be output directly.
##
## For size optimization, secret keys are using paperkey(1) to reduce the
## QR code payload. In order to re-create the secret key paperkey(1) use the
## public data in the public key that are included in the final secret key.
if gpg --list-packets < ${tmp}/data.bin > ${tmp}/data.info 2>/dev/null
then
cat ${tmp}/data.bin > ${output}
elif test -z "${public}"
then
echo >&2 "A public key is needed"
exit 1
else
# Convert the public key.
gpg --dearmor < ${public} > ${tmp}/public.gpg
paperkey --secrets ${tmp}/data.bin --input-type raw \
--pubring ${tmp}/public.gpg > ${output}
fi