Skip to content

Instantly share code, notes, and snippets.

@cosimo
Created May 29, 2024 12:58
Show Gist options
  • Save cosimo/053bc6e4b1f5b52b3cd3b394939cfcbe to your computer and use it in GitHub Desktop.
Save cosimo/053bc6e4b1f5b52b3cd3b394939cfcbe to your computer and use it in GitHub Desktop.
Generate Google Pay private/public keys
"""
Generate private/public keys for use in Google Pay Business Console
https://pay.google.com/business/console/
"""
import base64
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.primitives import serialization
# Generate the ECDSA private key
private_key = ec.generate_private_key(ec.SECP256R1())
# Get the public key
public_key = private_key.public_key()
# Serialize the public key in uncompressed point format
uncompressed_public_key = public_key.public_bytes(
encoding=serialization.Encoding.X962,
format=serialization.PublicFormat.UncompressedPoint
)
# Print the length of the public key to ensure it is 88 bytes
print(f"Uncompressed Public Key Length: {len(uncompressed_public_key)} bytes")
print(f"Uncompressed Public Key: {uncompressed_public_key.hex()}")
# Convert the uncompressed public key to base64 format
base64_public_key = base64.b64encode(uncompressed_public_key).decode("utf-8")
print(f"Base64 Public Key: {base64_public_key}")
with open("base64_public_key.txt", "wb") as key_file:
key_file.write(base64_public_key.encode("utf-8"))
print("Public key saved in base64 format to base64_public_key.txt")
# Serialize the private key to PEM format and save it to a file
pem_private_key = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption() # No encryption for simplicity
)
# Write the PEM private key to a file
with open("ecdsa_private_key.pem", "wb") as key_file:
key_file.write(pem_private_key)
print("Private key saved to ecdsa_private_key.pem")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment