Skip to content

Instantly share code, notes, and snippets.

@cjies
Created November 22, 2019 04:44
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save cjies/cc014d55976db80f610cd94ccb2ab21e to your computer and use it in GitHub Desktop.
Save cjies/cc014d55976db80f610cd94ccb2ab21e to your computer and use it in GitHub Desktop.
Python based VAPID key-pair generator
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("="),
'public_key': base64.urlsafe_b64encode("\x04" + vk.to_string()).strip("=")
}
@rahulshw
Copy link

rahulshw commented Oct 5, 2020

Thanks for this code snippet. It worked like a charm.

@Tobiaqs
Copy link

Tobiaqs commented May 1, 2024

Updated to work with recent Python versions

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"=")
  }

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