在 python 中生成 JWS 令牌(C 中的示例)
Generating JWS token in python (Example in C)
您好,我有这篇文章中的代码
https://medium.com/@alexastrum/firebase-auth-for-iot-devices-286679a8b59e
我想知道等效的 python 代码是什么。
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);
}
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},
)
您好,我有这篇文章中的代码 https://medium.com/@alexastrum/firebase-auth-for-iot-devices-286679a8b59e
我想知道等效的 python 代码是什么。
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);
}
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},
)