Skip to content

Instantly share code, notes, and snippets.

@kou1okada
Last active March 14, 2022 06:27
Show Gist options
  • Save kou1okada/8cf7c0d556b269d9f30b7c28a89f4b55 to your computer and use it in GitHub Desktop.
Save kou1okada/8cf7c0d556b269d9f30b7c28a89f4b55 to your computer and use it in GitHub Desktop.
checkkey.sh - Check key about GNUPG.
#!/usr/bin/env bash
#
# checkkey.sh - Check key about GNUPG.
# Copyright (c) 2022 Koichi OKADA. All rights reserved.
# This script is distributed under the MIT license.
#
SGR0="\e[0m"
SGR1="\e[1m"
SGR31="\e[31m"
SGR32="\e[32m"
function checkkey () # <keyfile>
# Check key about GNUPG.
# Arguments:
# <keyfile> Key file for the GNU PG.
# Environment variables:
# $OPT_UTC If set, use UTC timestamp.
{
local pubring stamp GNUPGHOME trash
pubring="$1"
if ! [[ -f "$pubring" && -r "$pubring" ]]; then
echo -e "$SGR31${SGR1}Error:$SGR0 Can not access pubring. : $pubring"
return 1
fi >&2
[ -z "$stamp" ] && read stamp < <(date ${OPT_UTC:+-u }-r "$pubring" "+%Y%m%dT%H%M%S%z")
if [ -z "$stamp" ]; then
echo -e "$SGR31${SGR1}Error:$SGR0 filename does not have time stamp."
return 1
fi >&2
printf -v trash "/tmp/trash/%(%Y%m%d_%H%M%S)T"
if ! mkdir -p "$trash"; then
echo -e "$SGR31${SGR1}Error:$SGR0 Can not prepare trash directory. : $trash"
return 1
fi >&2
GNUPGHOME="/tmp/.checkkey/$stamp"
if [ -d "$GNUPGHOME" ]; then
echo -e "$SGR31${SGR1}Error:$SGR0 Tempolary GNUPGHOME is already exist. : $GNUPGHOME"
echo "Suggestion:"
echo " rm -rv \"$GNUPGHOME\""
return 1
fi >&2
if ! { mkdir -p "$GNUPGHOME" && chmod 700 "$GNUPGHOME"; }; then
echo -e "$SGR31${SGR1}Error:$SGR0 Can not prepare GNUPGHOME. : $GNUPGHOME"
return 1
fi
gpg --import "$pubring" >&/dev/null
echo -e "$SGR32$SGR1[Public keys]$SGR0"
gpg -k
echo -e "$SGR32$SGR1[Secret keys]$SGR0"
gpg -K
echo -e "$SGR32$SGR1[Signatures]$SGR0"
gpg --check-sigs
mv "$GNUPGHOME" "$trash/"
}
function gpg ()
# GNUPG wrapper
{
LANG=C GNUPGHOME="$GNUPGHOME" gpg2 "$@"
}
function function_usage () # <file> <regex>
# Show usage of function.
# Arguments:
# <file> File of source code.
# <regex> Pattern for matching function name.
{
awk -vpat="${2:-.}" '
match($0, /^(function\s+)?(\S+)\s*\(\)\s*(\x23.*)$/, m) {name=m[2];n=-1;}
name!="" {if (match($0, /^{/)) {name=null;} else {n++;}}
match(name, pat) {
if (n == 0) {
gsub(/^function\s*/,"");
sub(/\s*\(\)\s*\x23?/,"");
printf("Usage: ");
}
sub(/^\x23 ?/,"");
if (!match($0,/^{/)) print $0;
}
' "$1"
}
if (( $# <= 0 )); then
function_usage "$BASH_SOURCE" checkkey
exit
fi
for i; do
checkkey "$i"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment