From d48e5707c34a19661815e554db598d9485859cdd Mon Sep 17 00:00:00 2001 From: "Brock A. Martin" Date: Sun, 17 Jul 2016 13:41:41 -0500 Subject: [PATCH 1/7] Switched to using bash arrays to hold split versions of key instead of temp files. --- asc2qr.sh | 45 +++++++++++++++++---------------------------- qr2asc.sh | 22 +++++++++------------- 2 files changed, 26 insertions(+), 41 deletions(-) diff --git a/asc2qr.sh b/asc2qr.sh index c5d6ea8..e98842f 100755 --- a/asc2qr.sh +++ b/asc2qr.sh @@ -28,7 +28,7 @@ # Maximum chuck size to send to the QR encoder. QR version 40 supports # 2,953 bytes of storage. -file_split_size=2800 +max_qr_bytes=2800 # Prefix string for the PNG images that are produced image_prefix="QR" @@ -45,38 +45,27 @@ if [ ! -f ${asc_key} ]; then exit 1 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 +## Split the key file into usable chunks that the QR encoder can consume +chunks=() +while true; do + s=$( head -c ${max_qr_bytes} ) + echo "$s" + if [ ${#s} -gt 0 ]; then + chunks+=("${s}") + else + break + fi +done <<< "$( cat ${asc_key} )" -# Split the key file into usable chunks that the QR encoder can consume -split -b ${file_split_size} ${asc_key} "${tmp_file}." - -# For each chunk, encode it into a qc image +## For each chunk, encode it into a qr image index=1 -for file in ${tmp_file}.*; do - img="${image_prefix}${index}.png" - echo "generating ${img}" - cat ${file} | qrencode -o ${img} +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 - -# 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}.* diff --git a/qr2asc.sh b/qr2asc.sh index fdb4e8f..8ead0e5 100755 --- a/qr2asc.sh +++ b/qr2asc.sh @@ -43,6 +43,7 @@ if [ $? -ne 0 ]; then fi # For each image on the command line, decode it into text +chunks=() index=1 for img in "$@"; do if [ ! -f ${img} ]; then @@ -51,24 +52,19 @@ for img in "$@"; do fi asc_key="${tmp_file}.${index}" 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' ) if [ $? -ne 0 ]; then echo "failed to decode QR image" exit 2 fi + chunks+=("${chunk}") index=$((index+1)) done +asc_key="" +for c in "${chunks[@]}"; do + asc_key+="${c}" +done + echo "creating ${output_key_name}" -cat ${tmp_file}.* > ${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}.* +echo "${asc_key}" > ${output_key_name} From cf04e7a8a4676a3f17cd7c03116152f91c7605b8 Mon Sep 17 00:00:00 2001 From: "Brock A. Martin" Date: Sat, 23 Jul 2016 13:05:59 -0500 Subject: [PATCH 2/7] Removed temporary file generation --- qr2asc.sh | 9 --------- 1 file changed, 9 deletions(-) diff --git a/qr2asc.sh b/qr2asc.sh index 8ead0e5..1aeb7ca 100755 --- a/qr2asc.sh +++ b/qr2asc.sh @@ -33,15 +33,6 @@ if [ $# -lt 1 ]; then exit 1 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 chunks=() index=1 From 2bfcc74b5af5343fac0c81c454e56d0c96efe60a Mon Sep 17 00:00:00 2001 From: "Brock A. Martin" Date: Sat, 23 Jul 2016 15:01:34 -0500 Subject: [PATCH 3/7] Adding test script --- test.sh | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100755 test.sh diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..d85c9a1 --- /dev/null +++ b/test.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +if [ $# -eq 0 ]; then + echo "usage: $(basename ${0}) " + 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 From 64638766a034785dc3e659996a91205f54620f8f Mon Sep 17 00:00:00 2001 From: "Brock A. Martin" Date: Sat, 23 Jul 2016 15:04:58 -0500 Subject: [PATCH 4/7] Removing 'echo' of private asc key --- asc2qr.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/asc2qr.sh b/asc2qr.sh index e98842f..2f0fe02 100755 --- a/asc2qr.sh +++ b/asc2qr.sh @@ -49,7 +49,6 @@ fi chunks=() while true; do s=$( head -c ${max_qr_bytes} ) - echo "$s" if [ ${#s} -gt 0 ]; then chunks+=("${s}") else From d80f849f4ac9aad03f08f3cc044848575d3fbebc Mon Sep 17 00:00:00 2001 From: "Brock A. Martin" Date: Sat, 23 Jul 2016 15:06:23 -0500 Subject: [PATCH 5/7] Adding suggested decode line as comment (I don't think the other one works always anyway) --- qr2asc.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/qr2asc.sh b/qr2asc.sh index 1aeb7ca..fe5ae0a 100755 --- a/qr2asc.sh +++ b/qr2asc.sh @@ -44,6 +44,10 @@ for img in "$@"; do asc_key="${tmp_file}.${index}" echo "decoding ${img}" 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 echo "failed to decode QR image" exit 2 From 5094f35d05664fcf2819e9665110298bbac867cf Mon Sep 17 00:00:00 2001 From: "Brock A. Martin" Date: Tue, 26 Jul 2016 07:21:04 -0500 Subject: [PATCH 6/7] switched out 'head' for 'dd' in order to do chunking --- asc2qr.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/asc2qr.sh b/asc2qr.sh index 2f0fe02..708d869 100755 --- a/asc2qr.sh +++ b/asc2qr.sh @@ -48,13 +48,13 @@ fi ## Split the key file into usable chunks that the QR encoder can consume chunks=() while true; do - s=$( head -c ${max_qr_bytes} ) + s=$( dd bs=${max_qr_bytes} count=1 2>/dev/null ) if [ ${#s} -gt 0 ]; then chunks+=("${s}") else break fi -done <<< "$( cat ${asc_key} )" +done < ${asc_key} ## For each chunk, encode it into a qr image index=1 From 9aa3a998d4a0e9a6ae67242039fd7d131b4afc64 Mon Sep 17 00:00:00 2001 From: "Brock A. Martin" Date: Tue, 26 Jul 2016 21:49:29 -0500 Subject: [PATCH 7/7] switched to read instead of 'dd' --- asc2qr.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/asc2qr.sh b/asc2qr.sh index 708d869..a10e3ef 100755 --- a/asc2qr.sh +++ b/asc2qr.sh @@ -48,7 +48,7 @@ fi ## Split the key file into usable chunks that the QR encoder can consume chunks=() while true; do - s=$( dd bs=${max_qr_bytes} count=1 2>/dev/null ) + IFS= read -r -d'\0' -n ${max_qr_bytes} s if [ ${#s} -gt 0 ]; then chunks+=("${s}") else