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.

Revisions

  1. cosimo created this gist May 29, 2024.
    44 changes: 44 additions & 0 deletions generate-google-pay-keys.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    """
    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")