JWK API

文档的这一部分涵盖了 joserfc.jwk 的所有接口。

class joserfc.jwk.ECKey(raw_value: NativePrivateKey | NativePublicKey, original_value: Any, parameters: KeyParameters | None = None)
classmethod generate_key(crv: str = 'P-256', parameters: KeyParameters | None = None, private: bool = True, auto_kid: bool = False) ECKey

生成具有给定 "crv" 值的 ECKey

参数:
  • crv -- ECKey 曲线名称

  • parameters -- JWK 中的额外参数

  • private -- 生成私钥或公钥

  • auto_kid -- 自动添加 kid

value_registry: t.ClassVar[KeyParameterRegistryDict] = {'crv': <joserfc.registry.KeyParameter object>, 'd': <joserfc.registry.KeyParameter object>, 'x': <joserfc.registry.KeyParameter object>, 'y': <joserfc.registry.KeyParameter object>}

EC 密钥注册表定义 https://www.rfc-editor.org/rfc/rfc7518#section-6.2

class joserfc.jwk.JWKRegistry

JWK 的注册表,用于记录 joserfc 支持的密钥类型。通常,您会使用显式密钥类型,如 OctKeyRSAKey;此注册表提供了一种动态导入和生成密钥的方法。例如:

from joserfc.jwk import JWKRegistry

# instead of choosing which key type to use yourself,
# JWKRegistry can import it automatically
data = {"kty": "oct", "k": "..."}
key = JWKRegistry.import_key(data)
classmethod generate_key(key_type: str, crv_or_size: str | int, parameters: KeyParameters | None = None, private: bool = True, auto_kid: bool = False) OctKey | RSAKey | ECKey | OKPKey

根据给定的密钥类型生成密钥的类方法。当 key_type 为 "oct" 和 "RSA" 时,第二个参数应为位大小。当 key_type 为 "EC" 和 "OKP" 时,第二个参数应为 "crv" 字符串。

JWKRegistry.generate_key("RSA", 2048)
JWKRegistry.generate_key("EC", "P-256")
classmethod import_key(data: str | bytes | Dict[str, str | List[str]], key_type: str | None = None, parameters: KeyParameters | None = None) OctKey | RSAKey | ECKey | OKPKey

从字节、字符串和字典导入密钥的类方法。当 value 为字典时,此方法可以自动识别密钥类型,否则,开发人员应自行传递 key_type

参数:
  • data -- 字节、字符串或字典形式的密钥数据。

  • key_type -- 可选的密钥类型字符串。

  • parameters -- 额外的密钥参数

返回:

OctKey、RSAKey、ECKey 或 OKPKey

class joserfc.jwk.OKPKey(raw_value: NativePrivateKey | NativePublicKey, original_value: Any, parameters: KeyParameters | None = None)

OKP 密钥类型的密钥类。

classmethod generate_key(crv: str = 'Ed25519', parameters: KeyParameters | None = None, private: bool = True, auto_kid: bool = False) OKPKey

生成具有给定 "crv" 值的 OKPKey

参数:
  • crv -- OKPKey 曲线名称

  • parameters -- JWK 中的额外参数

  • private -- 生成私钥或公钥

  • auto_kid -- 自动添加 kid

value_registry: t.ClassVar[KeyParameterRegistryDict] = {'crv': <joserfc.registry.KeyParameter object>, 'd': <joserfc.registry.KeyParameter object>, 'x': <joserfc.registry.KeyParameter object>}

OKP 密钥注册表定义 https://www.rfc-editor.org/rfc/rfc8037#section-2

class joserfc.jwk.OctKey(raw_value: NativePrivateKey | NativePublicKey, original_value: Any, parameters: KeyParameters | None = None)

OctKey 是对称密钥,由 RFC7518 第 6.4 节定义。

classmethod generate_key(key_size: int = 256, parameters: KeyParameters | None = None, private: bool = True, auto_kid: bool = False) OctKey

生成具有给定位大小(不是字节)的 OctKey

参数:
  • key_size -- 位大小

  • parameters -- JWK 中的额外参数

  • private -- 必须为 True

  • auto_kid -- 自动添加 kid

value_registry: t.ClassVar[KeyParameterRegistryDict] = {'k': <joserfc.registry.KeyParameter object>}

https://www.rfc-editor.org/rfc/rfc7518#section-6.4

class joserfc.jwk.RSAKey(raw_value: NativePrivateKey | NativePublicKey, original_value: Any, parameters: KeyParameters | None = None)
classmethod generate_key(key_size: int = 2048, parameters: KeyParameters | None = None, private: bool = True, auto_kid: bool = False) RSAKey

生成具有给定位大小(不是字节)的 RSAKey

参数:
  • key_size -- 位大小

  • parameters -- JWK 中的额外参数

  • private -- 生成私钥或公钥

  • auto_kid -- 自动添加 kid

value_registry: t.ClassVar[KeyParameterRegistryDict] = {'d': <joserfc.registry.KeyParameter object>, 'dp': <joserfc.registry.KeyParameter object>, 'dq': <joserfc.registry.KeyParameter object>, 'e': <joserfc.registry.KeyParameter object>, 'n': <joserfc.registry.KeyParameter object>, 'oth': <joserfc.registry.KeyParameter object>, 'p': <joserfc.registry.KeyParameter object>, 'q': <joserfc.registry.KeyParameter object>, 'qi': <joserfc.registry.KeyParameter object>}

RSA 密钥注册表定义 https://www.rfc-editor.org/rfc/rfc7518#section-6.3

joserfc.jwk.guess_key(key: str | bytes | OctKey | RSAKey | ECKey | OKPKey | KeySet | Callable[[GuestProtocol], str | bytes | OctKey | RSAKey | ECKey | OKPKey | KeySet], obj: GuestProtocol, use_random: bool = False) OctKey | RSAKey | ECKey | OKPKey

从各种来源猜测密钥。

参数:
  • key -- 非常灵活的密钥

  • obj -- 具有 headersset_kid 方法的协议

  • use_random -- 从密钥集中随机选择一个密钥