Skip to content

Instantly share code, notes, and snippets.

@morgwai
Last active Jan 28, 2022
Embed
What would you like to do?
A script that gets WKD URL for the email address passed as the argument. See https://datatracker.ietf.org/doc/html/draft-koch-openpgp-webkey-service-13#section-3.1
#!/bin/bash
# Copyright (c) Piotr Morgwai Kotarbinski, Licensed under the Apache License, Version 2.0
usage='usage: wkdurl [--direct | --advanced] user@example.com' ;
if [[ "${#}" -lt 1 ]] || [[ "${#}" -gt 2 ]] ; then
echo "${usage}" >&2 ;
exit 1 ;
fi ;
direct='false' ;
if [[ "${#}" -eq 2 ]] ; then
case "${1}" in
'--direct')
direct='true' ;;
'--advanced')
;; # advanced is the default setting above
*)
echo "unrecognized option '${1}', ${usage}" >&2 ;
exit 1 ;;
esac ;
shift ;
fi ;
IFS='@' read user domain <<< "${1}" ;
domain="${domain,,}" ;
if [[ -z "${user}" ]] || [[ -z "${domain}" ]] ; then
#TODO: validate email address format more
echo "invalid email address" >&2 ;
exit 2 ;
fi ;
hashedUser=$( echo -n "${user,,}" |sha1sum |cut -d ' ' -f 1 |xxd -r -p - |base32 |sed -e 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ234567/ybndrfg8ejkmcpqxot1uwisza345h769/' ) ;
urlencodedUser=$( echo -n "${user}" |python3 -c "import urllib.parse, sys; print(urllib.parse.quote(sys.stdin.read()))" ) ;
if "${direct}" ; then
echo "https://${domain}/.well-known/openpgpkey/hu/${hashedUser}?l=${urlencodedUser}" ;
else
echo "https://openpgpkey.${domain}/.well-known/openpgpkey/${domain}/hu/${hashedUser}?l=${urlencodedUser}" ;
fi ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment