Skip to content

Instantly share code, notes, and snippets.

@Aleksanaa
Created November 29, 2022 19:13
Show Gist options
  • Save Aleksanaa/9886c9d7d50f1c815400657578ee9a76 to your computer and use it in GitHub Desktop.
Save Aleksanaa/9886c9d7d50f1c815400657578ee9a76 to your computer and use it in GitHub Desktop.
Generate wireguard public and private key with openssl and bash
#!/bin/bash
# This is used by openssl to tell the type of the key
private_head="MC4CAQAwBQYDK2VuBCIEIA=="
public_head="MCowBQYDK2VuAyEA"
# generate one string (containing '\x0') from two base64
b64join() {
echo -e "$(printf ${1} \
| base64 -d \
| sed -e 's/\x0/whatsup/g')$(printf ${2} \
| base64 -d \
| sed -e 's/\x0/whatsup/g')" \
| sed -e "s/whatsup/\x0/g"
}
case ${1} in
genkey)
openssl genpkey -algorithm X25519 -outform der \
| tail -c 32 \
| base64 \
;;
pubkey)
b64join ${private_head} ${2} \
| openssl pkey -inform DER -outform DER -pubout \
| tail -c 32 \
| base64
;;
exchange)
openssl pkeyutl -derive \
-keyform DER -inkey <(b64join ${private_head} ${2}) \
-peerform DER -peerkey <(b64join ${public_head} ${3} | sed 's/K$/=/') \
| base64
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment