pythonjsonsocketsnacl-cryptography

Sending a public key in pynacl as JSON object


I have a server and client setup where when the client connects, it sends over a JSON object with a username and a public_key.

My current code:

private_key = PrivateKey.generate()
public_key = private_key.public_key
payload = json.dumps({"username": username, "public_key": public_key}).encode('hex')

But I get:

TypeError: <nacl.public.PublicKey object at 0x7fc6ecff18d0> is not JSON serializable

Any solutions?


Solution

  • This is probably a bit late but it's explained here

    If you want it as Base64 for example you do

    from nacl.encoding import Base64Encoder
    print(public_key.encode(Base64Encoder).decode())