Skip to content

Instantly share code, notes, and snippets.

@Tobiaqs
Created May 1, 2024 08:39
Show Gist options
  • Save Tobiaqs/450a4516ae44813792b7d84028c366c0 to your computer and use it in GitHub Desktop.
Save Tobiaqs/450a4516ae44813792b7d84028c366c0 to your computer and use it in GitHub Desktop.
Generate VAPID keys
# Based on https://gist.github.com/cjies/cc014d55976db80f610cd94ccb2ab21e
import base64
import ecdsa
def generate_vapid_keypair():
"""
Generate a new set of encoded key-pair for VAPID
"""
pk = ecdsa.SigningKey.generate(curve=ecdsa.NIST256p)
vk = pk.get_verifying_key()
return {
'private_key': base64.urlsafe_b64encode(pk.to_string()).strip(b"="),
'public_key': base64.urlsafe_b64encode(b"\x04" + vk.to_string()).strip(b"=")
}
keys = generate_vapid_keypair()
print("\nPrivate key:\n")
print(keys["private_key"].decode())
print("\nPublic key:\n")
print(keys["public_key"].decode())
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment