Skip to content

Commit 0221ac6

Browse files
committed
Refactored validate_gpg_keyid ()
1 parent 8b21a17 commit 0221ac6

File tree

1 file changed

+26
-59
lines changed

1 file changed

+26
-59
lines changed

notes

Lines changed: 26 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -99,65 +99,32 @@ validate_gpg_keyid () {
9999

100100
gpg --list-secret-keys --with-colons --keyid-format short | grep sec | cut -d':' -f 5 > $keyids
101101

102-
echo testing key length
103-
if [ "${#testkey}" != 16 ] ; then
104-
echo Key supplied $testkey is wrong length: length = ${#testkey}
105-
echo Key length should be 16 characters
106-
echo Valid keys are:
107-
cat $keyids
108-
rm $keyids
109-
exit 1
110-
fi
111-
112-
113-
echo valid keyids are ...
114-
cat $keyids
115-
echo Testing $testkey
116-
117-
grep $testkey $keyids > /dev/null
118-
isvalidgpgkey="$?"
119-
echo testing result = $isvalidgpgkey
120-
121-
if [ "$isvalidgpgkey" = "0" ] ; then
122-
echo valid given
123-
124-
else
125-
echo Invalid GPG keyid \n\n
126-
127-
echo Valid GPG keys are
128-
cat $keyids
129-
echo \n\n Type 'gpg -k <keyid> to find out more about key'
130-
#rm $keyids
131-
exit 1
132-
fi
133-
102+
# test key character length = the last 16 (short) or the full 40 (long) are acceptable
103+
case ${#testkey} in
104+
105+
16|40)
106+
shortkey="`echo ${testkey} | tail -c 17`"
107+
if [ "`grep ${shortkey} ${keyids}`" = "0" ] ; then
108+
echo Invalid GPG keyid
109+
cat $keyids
110+
echo \n\n Type 'gpg -k <keyid> to find out more about key'
111+
rm $keyids
112+
exit 1
113+
else
114+
echo Valid key $testkey
115+
fi
116+
;;
117+
*)
118+
echo Provided key is invalid: keys should be 16 or 40 characters
119+
echo Valid keys are ...
120+
cat $keyids
121+
echo \n\n Type 'gpg -k <keyid> to find out more about key'
122+
rm $keyids
123+
exit 1;;
124+
esac
134125
}
135126

136-
recrypt_file () { ## not used
137-
newkey = "$1"
138-
target = "$2"
139-
140-
get_recipient
141-
142-
echo decrypt from $KEY
143-
echo filename = $target
144-
echo new gpg key = $newkey
145127

146-
}
147-
148-
get_gpg_key_uids () { # unused but logic may be used at further date
149-
keyids="/tmp/keyids.$$"
150-
gpguids="/tmp/uids.$$"
151-
152-
gpg --list-secret-keys --with-colons --keyid-format short | grep sec | cut -d':' -f 5 > $keyids
153-
154-
echo keyids are
155-
cat $keyids
156-
echo Available keys
157-
cat $keyids | xargs -n 1 gpg -k | grep ^uid | sed 's/>$//; s/^.*<//' > $gpguids
158-
cat $gpguids | sort | nl
159-
160-
}
161128
#
162129
# note functions
163130
#
@@ -530,13 +497,13 @@ cmd_newkey () {
530497
while read filen ;
531498
do
532499
echo $filen;
533-
gpg -o "${filen}" -d "${filen}.gpg"
534-
gpg -r ${mynewkey} -e "${filen}" && ${SHREDCMD} "${filen}"
500+
gpg -o "${filen}" --yes -d "${filen}.gpg"
501+
gpg -r ${mynewkey} --yes -e "${filen}" && ${SHREDCMD} "${filen}"
535502

536503
done
537504

538505
# need to fix up config file now ...
539-
NEWUSER="`gpg -k ${mynewkey} | grep uid | head -1 | sed 's/>$//; s/^.*<//' `"
506+
NEWUSER="`gpg -k ${mynewkey} | grep uid | head -1 | sed 's/>$//; s/^.*<//' `"
540507
sed -i s/^KEY.*$/KEY\\t${NEWUSER}/ ${CONFIGFILE}
541508
}
542509

0 commit comments

Comments
 (0)