Pyjo.GCP.RSA - Signatures of RS256 of a key of RSA

from Pyjo.GCP.RSA import RSA

key = RSA.from_pem(info['private_key'])
signature = key.sign(b'header.payload')

# Or import the module, as in Google Python Style Guide

import Pyjo.GCP.RSA

key = Pyjo.GCP.RSA.from_pem(pem, backend='python')
token = key.jwt({'iss': 'reader@myproject.iam.gserviceaccount.com', 'iat': 1762257600}, key_id='3c5a...')
print(key.verify(b'header.payload', signature))

Pyjo.GCP.RSA is a private key of RSA, as the key of a service account of Google Cloud has it, which signs with SHA-256 and the padding of PKCS #1 v1.5, as RS256 of JSON Web Signature, of RFC 7518, has it, and makes the JSON Web Tokens of Pyjo.GCP.Credentials.ServiceAccount.

The signature is the one of the package cryptography when it is installed, as the extra cryptography of the distribution installs it:

pip install 'Pyjo-GCP[cryptography]'

or of Python itself otherwise, whose signature is the same, as the padding of PKCS #1 v1.5 is not random. That one is not constant-time: it is for a key and a signature that are made on the machine that has the key, not for a server that signs what anyone sends it.

See also

Pyjo.GCP.Credentials.ServiceAccount, RFC 8017, RFC 7518, RFC 7519, https://cryptography.io/.

Classes

class Pyjo.GCP.RSA.Pyjo_GCP_RSA(n: int, e: int, d: int, p: int, q: int, *, backend: Backend | None = None)[source]

Pyjo.GCP.RSA inherits all attributes and methods from Pyjo.Base and implements the following new ones.

__init__(n: int, e: int, d: int, p: int, q: int, *, backend: Backend | None = None) → None[source]
key = Pyjo.GCP.RSA.new(n, e, d, p, q)
key = Pyjo.GCP.RSA.new(n, e, d, p, q, backend='python')

Construct a new Pyjo.GCP.RSA object of the modulus, the public and the private exponent and the primes of a key, with the backend of cryptography when it is installed, or of Python.

__weakref__

list of weak references to the object

backend: Backend
name = key.backend

The backend of the signatures, cryptography or python.

d
d = key.d

The private exponent of the key.

e
e = key.e

The public exponent of the key, as 65537.

classmethod from_pem(pem: str | bytes, *, backend: Backend | None = None) → Pyjo_GCP_RSA[source]
key = Pyjo.GCP.RSA.from_pem(pem)

A new Pyjo.GCP.RSA object of a private key of RSA in PEM, of PKCS #8, BEGIN PRIVATE KEY, as the key of a service account has it, or of PKCS #1, BEGIN RSA PRIVATE KEY, without a password.

jwt(claims: Mapping[str, Any], *, key_id: str | None = None) → str[source]
token = key.jwt({'iss': email, 'aud': 'https://oauth2.googleapis.com/token', 'iat': iat, 'exp': exp})
token = key.jwt(claims, key_id=info['private_key_id'])

A JSON Web Token of RS256 of the claims, with the ID of the key in kid of its header, as google-auth makes it.

n
n = key.n

The modulus of the key.

p
p = key.p

The first prime of the key.

q
q = key.q

The second prime of the key.

sign(data: bytes) → bytes[source]
signature = key.sign(b'header.payload')

The signature of RS256 of data, of the length of the modulus.

verify(data: bytes, signature: bytes) → bool[source]
boolean = key.verify(b'header.payload', signature)

Whether a signature of RS256 of data is the one of the public key.

Pyjo.GCP.RSA.from_pem(pem: str | bytes, *, backend: Backend | None = None) → Pyjo_GCP_RSA[source]
key = Pyjo.GCP.RSA.from_pem(pem)

Alias for Pyjo_GCP_RSA.from_pem().

Pyjo.GCP.RSA.verify(n: int, e: int, data: bytes, signature: bytes) → bool[source]
boolean = Pyjo.GCP.RSA.verify(n, e, b'header.payload', signature)

Whether a signature of RS256 of data is the one of a public key, of its modulus and its exponent, in Python.