Last active
June 23, 2022 04:42
-
-
Save jamilbk/536e88bf065b8a78deaac19d43af8afa to your computer and use it in GitHub Desktop.
Generate WireGuard-compatible private key in Elixir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Wg do | |
# Clamp random bytes for generating Curve25519 private key | |
# See https://github.com/tonarino/innernet/blob/main/wireguard-control/src/key.rs#L40 | |
# or | |
# https://github.com/WireGuard/wireguard-tools/blob/master/src/curve25519.h#L18 | |
def genkey do | |
bytes = :crypto.strong_rand_bytes(32) | |
<<head>> = binary_part(bytes, 0, 1) | |
<<tail>> = binary_part(bytes, 31, 1) | |
clamped_head = head &&& 248 | |
clamped_tail = (tail &&& 127) ||| 64 | |
<<clamped_head>> <> binary_part(bytes, 1, 30) <> <<clamped_tail>> | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment