File dkim-genkey.sh of Package netqmail
#!/bin/sh
# (C) 2011 Peter Conrad <conrad@quisquis.de>
#
# This program is licensed under the terms of the
# GNU General Public License Version 2. A copy of these terms should be
# enclosed as "gpl-2.0.txt" in the package containing this file.
if [ $# != 1 -o "$1" = "-h" ]; then
echo "Usage: $0 <basename>"
echo "Generates an RSA secret key for DKIM and writes it to <basename>.key."
echo "Also creates a TXT record containing the public key and writes it to"
echo "<basename>.pub."
exit 1
fi
if [ -r "$1.key" -o -r "$1.pub" ]; then
echo "Target file(s) exist(s)! Cancelled..."
exit 1
fi
PRIV="$(openssl genrsa 1024 2>/dev/null)"
PUB="$(echo "$PRIV" | openssl rsa -pubout -outform PEM 2>/dev/null | grep -v '^-----')"
umask 027
echo $PRIV | sed 's=-----[^-]*-----==g;s:[^a-zA-Z0-9/+=]*::g;' >"$1.key"
chgrp mail "$1.key"
echo -n "v=DKIM1; p=" >"$1.pub"
echo $PUB | sed 's=[^a-zA-Z0-9/+]*==g' >>"$1.pub"