forked from 4bitfocus/asc-key-to-qr-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asc2qr.sh
executable file
·70 lines (63 loc) · 1.75 KB
/
asc2qr.sh
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
#!/bin/bash
#####
#
# Author: Kevin Douglas <[email protected]>
#
# Simple command line script to backup ascii armor gpg keys to paper. You can
# use the following commands to export your keys in ascii armor format:
#
# gpg --armor --export > pgp-public-keys.asc
# gpg --armor --export-secret-keys > pgp-private-keys.asc
# gpg --armor --gen-revoke [your key ID] > pgp-revocation.asc
#
# These can then be used to restore your keys if necessary.
#
# This script will allow you to convert the above ascii armor keys into a
# printable QR code for long-term archival.
#
# This script depends on the following libraries/applications:
#
# libqrencode (http://fukuchi.org/works/qrencode/)
#
# If you need to backup or restore binary keys, see this link to get started:
#
# https://gist.github.com/joostrijneveld/59ab61faa21910c8434c#file-gpg2qrcodes-sh
#
#####
# Maximum chuck size to send to the QR encoder. QR version 40 supports
# 2,953 bytes of storage.
max_qr_bytes=2800
# Prefix string for the PNG images that are produced
image_prefix="QR"
# Argument/usage check
if [ $# -ne 1 ]; then
echo "usage: `basename ${0}` <ascii armor key file>"
exit 1
fi
asc_key=${1}
if [ ! -f "${asc_key}" ]; then
echo "key file not found: '${asc_key}'"
exit 1
fi
## Split the key file into usable chunks that the QR encoder can consume
chunks=()
while true; do
IFS= read -r -d'\0' -n ${max_qr_bytes} s
if [ ${#s} -gt 0 ]; then
chunks+=("${s}")
else
break
fi
done < ${asc_key}
## For each chunk, encode it into a qr image
index=1
for c in "${chunks[@]}"; do
img="${image_prefix}${index}.png"
echo "generating ${img}"
echo -n "${c}" | qrencode -o ${img}
if [ $? -ne 0 ]; then
echo "failed to encode image"
exit 2
fi
index=$((index+1))
done