Skip to content

Instantly share code, notes, and snippets.

@rmoriz
Last active October 25, 2023 12:21
Show Gist options
  • Save rmoriz/58f1768218a4fbc4b318615bfb85111d to your computer and use it in GitHub Desktop.
Save rmoriz/58f1768218a4fbc4b318615bfb85111d to your computer and use it in GitHub Desktop.
WireGuard Key Generation (wg genkey)

wg genkey / pubkey

wg genkey
qJvFeHHuffBaPWx4veJGQqXw6j5zdo5cSOaBd1Z0Km4=


echo 'qJvFeHHuffBaPWx4veJGQqXw6j5zdo5cSOaBd1Z0Km4=' | wg pubkey
knL56pMLtyQVyZXOd9m2vEeOopPtbv4tMSU0ctBvGQo=

wg genkey: /dev/urandom (on Linux)

https://github.com/WireGuard/WireGuard/blob/master/src/tools/genkey.c#L69

fd = open("/dev/urandom", O_RDONLY);
	if (fd < 0)
		return fd;
	ret = read(fd, out, len);
	close(fd);
src/uapi/wireguard.h
135:#define WG_KEY_LEN 32

Pubkey: https://github.com/WireGuard/WireGuard/blob/master/src/tools/pubkey.c#L46

ruby

irb(main):014:0> require 'rb-pure25519'
=> false
irb(main):015:0> require 'base64'
=> false
irb(main):016:0> 
irb(main):017:0> priv_base64 = "qJvFeHHuffBaPWx4veJGQqXw6j5zdo5cSOaBd1Z0Km4="
=> "qJvFeHHuffBaPWx4veJGQqXw6j5zdo5cSOaBd1Z0Km4="
irb(main):018:0> priv        = Base64.strict_decode64(priv)
=> "\xA8\x9B\xC5xq\xEE}\xF0Z=lx\xBD\xE2FB\xA5\xF0\xEA>sv\x8E\\H\xE6\x81wVt*n"
irb(main):019:0> pub         = Rb25519.public_key_str(priv)
=> "\x92r\xF9\xEA\x93\v\xB7$\x15\xC9\x95\xCEw\xD9\xB6\xBCG\x8E\xA2\x93\xEDn\xFE-1%4r\xD0o\x19\n"
irb(main):020:0> pub_base64  = Base64.strict_encode64(pub)
=> "knL56pMLtyQVyZXOd9m2vEeOopPtbv4tMSU0ctBvGQo="
@cornerot
Copy link

cornerot commented Jun 8, 2023

Need to fix:

priv        = Base64.strict_decode64(priv_base64)

instead of

priv        = Base64.strict_decode64(priv)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment