Merge pull request #1 from bamartin125/hotfix/no-temp-files

Avoid using temporary files by reading the key into a bash array.
This commit is contained in:
Kevin Douglas 2016-07-26 22:44:29 -05:00 committed by GitHub
commit 483c2a3767
3 changed files with 53 additions and 50 deletions

View file

@ -28,7 +28,7 @@
# Maximum chuck size to send to the QR encoder. QR version 40 supports # Maximum chuck size to send to the QR encoder. QR version 40 supports
# 2,953 bytes of storage. # 2,953 bytes of storage.
file_split_size=2800 max_qr_bytes=2800
# Prefix string for the PNG images that are produced # Prefix string for the PNG images that are produced
image_prefix="QR" image_prefix="QR"
@ -45,38 +45,26 @@ if [ ! -f ${asc_key} ]; then
exit 1 exit 1
fi fi
# Create a temp file to use as a pattern for splitting the input key file. ## Split the key file into usable chunks that the QR encoder can consume
# This helps protect against file collisions in the current directory. chunks=()
export TMPDIR="" while true; do
tmp_file=$(mktemp keyparts.XXXXXX) IFS= read -r -d'\0' -n ${max_qr_bytes} s
if [ $? -ne 0 ]; then if [ ${#s} -gt 0 ]; then
echo "failed to create temporary file" chunks+=("${s}")
exit 1 else
fi break
fi
done < ${asc_key}
# Split the key file into usable chunks that the QR encoder can consume ## For each chunk, encode it into a qr image
split -b ${file_split_size} ${asc_key} "${tmp_file}."
# For each chunk, encode it into a qc image
index=1 index=1
for file in ${tmp_file}.*; do for c in "${chunks[@]}"; do
img="${image_prefix}${index}.png" img="${image_prefix}${index}.png"
echo "generating ${img}" echo "generating ${img}"
cat ${file} | qrencode -o ${img} echo -n "${c}" | qrencode -o ${img}
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "failed to encode image" echo "failed to encode image"
exit 2 exit 2
fi fi
index=$((index+1)) index=$((index+1))
done done
# Find the correct secure deletion utility (srm on Mac, shred on Linux)
sec_del="srm"
which ${sec_del} 2>&1 1>/dev/null
if [ $? -ne 0 ]; then
sec_del="shred --remove"
fi
# Securely clean up temporary files
${sec_del} ${tmp_file}
${sec_del} ${tmp_file}.*

View file

@ -33,16 +33,8 @@ if [ $# -lt 1 ]; then
exit 1 exit 1
fi fi
# Create a temp file to use as a pattern for splitting the input key file.
# This helps protect against file collisions in the current directory.
export TMPDIR=""
tmp_file=$(mktemp keyparts.XXXXXX)
if [ $? -ne 0 ]; then
echo "failed to create temporary file"
exit 1
fi
# For each image on the command line, decode it into text # For each image on the command line, decode it into text
chunks=()
index=1 index=1
for img in "$@"; do for img in "$@"; do
if [ ! -f ${img} ]; then if [ ! -f ${img} ]; then
@ -51,24 +43,23 @@ for img in "$@"; do
fi fi
asc_key="${tmp_file}.${index}" asc_key="${tmp_file}.${index}"
echo "decoding ${img}" echo "decoding ${img}"
zbarimg --raw ${img} 2>/dev/null | perl -p -e 'chomp if eof' > ${asc_key} chunk=$( zbarimg --raw ${img} 2>/dev/null | perl -p -e 'chomp if eof' )
# Please use this next line instead of teh one above if zbarimg does
# not decode the qr code properly in tests
# (zbarimg needs to be told it is being given a qr code to decode)
#chunk=$( zbarimg --raw --set disable --set qrcode.enable ${img} 2>/dev/null )
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "failed to decode QR image" echo "failed to decode QR image"
exit 2 exit 2
fi fi
chunks+=("${chunk}")
index=$((index+1)) index=$((index+1))
done done
asc_key=""
for c in "${chunks[@]}"; do
asc_key+="${c}"
done
echo "creating ${output_key_name}" echo "creating ${output_key_name}"
cat ${tmp_file}.* > ${output_key_name} echo "${asc_key}" > ${output_key_name}
# Find the correct secure deletion utility (srm on Mac, shred on Linux)
sec_del="srm"
which ${sec_del} 2>&1 1>/dev/null
if [ $? -ne 0 ]; then
sec_del="shred --remove"
fi
# Securely clean up temporary files
${sec_del} ${tmp_file}
${sec_del} ${tmp_file}.*

24
test.sh Executable file
View file

@ -0,0 +1,24 @@
#!/bin/bash
if [ $# -eq 0 ]; then
echo "usage: $(basename ${0}) <ascii armor key file>"
exit 1
fi
set -x
asc_key="$1"
./asc2qr.sh "${asc_key}"
./qr2asc.sh QR*.png
diff "${asc_key}" "./mykey.asc"
if [ $? -eq 0 ]; then
echo "Diff Test: PASS"
else
echo "Diff Test: FAIL"
fi
rm ./QR*.png
rm ./mykey.asc