-1

Hi I have this code from this article https://medium.com/@alexastrum/firebase-auth-for-iot-devices-286679a8b59e

I was wondering what the equivalent python code would be thanks.

String getDeviceToken()
{
  String header = "";
  DynamicJsonDocument json(1024);
  // ATECCx08 crypto chips only support ES256:
  // https://github.com/MicrochipTech/cryptoauthlib/blob/master/lib/jwt/atca_jwt.c
  json["alg"] = "ES256";
  json["kid"] = DEVICE_ID;
  serializeJson(json, header);

  String payload;
  json.clear();
  json["nonce"] = NONCE;
  serializeJson(json, payload);
  return ECCX08JWS.sign(/* slot */ 0, header, payload);
}
GILO
  • 2,444
  • 21
  • 47
  • 1
    Stack Overflow is a Q&A site, not a code translation service. Try to translate the code yourself first, then come to us when you are stuck, making sure to show us what you have tried and create a [mre]. – gre_gor Feb 25 '22 at 11:50

1 Answers1

0
import jwt as jwt
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import ec

# Generates the private key
private_key = ec.generate_private_key(ec.SECP256R1(), default_backend())

# Generates the public key
public_key = private_key.public_key()

jwt.encode(
    payload={"nonce": NONCE},
    key=private_key,
    algorithm="ES256",
    headers={"alg": "ES256", "kid": DEVICE_ID},
)
GILO
  • 2,444
  • 21
  • 47