1
1
"""Key interface and the default implementations"""
2
2
3
+ from __future__ import annotations
4
+
3
5
import logging
4
6
from abc import ABCMeta , abstractmethod
5
- from typing import Any , Optional , cast
7
+ from typing import Any , cast
6
8
7
9
from securesystemslib ._vendor .ed25519 .ed25519 import (
8
10
SignatureMismatch ,
@@ -94,7 +96,7 @@ def __init__(
94
96
keytype : str ,
95
97
scheme : str ,
96
98
keyval : dict [str , Any ],
97
- unrecognized_fields : Optional [ dict [str , Any ]] = None ,
99
+ unrecognized_fields : dict [str , Any ] | None = None ,
98
100
):
99
101
if not all (
100
102
isinstance (at , str ) for at in [keyid , keytype , scheme ]
@@ -124,7 +126,7 @@ def __eq__(self, other: Any) -> bool:
124
126
125
127
@classmethod
126
128
@abstractmethod
127
- def from_dict (cls , keyid : str , key_dict : dict [str , Any ]) -> " Key" :
129
+ def from_dict (cls , keyid : str , key_dict : dict [str , Any ]) -> Key :
128
130
"""Creates ``Key`` object from a serialization dict
129
131
130
132
Key implementations must override this factory constructor that is used
@@ -207,14 +209,14 @@ def __init__(
207
209
keytype : str ,
208
210
scheme : str ,
209
211
keyval : dict [str , Any ],
210
- unrecognized_fields : Optional [ dict [str , Any ]] = None ,
212
+ unrecognized_fields : dict [str , Any ] | None = None ,
211
213
):
212
214
if "public" not in keyval or not isinstance (keyval ["public" ], str ):
213
215
raise ValueError (f"public key string required for scheme { scheme } " )
214
216
super ().__init__ (keyid , keytype , scheme , keyval , unrecognized_fields )
215
217
216
218
@classmethod
217
- def from_dict (cls , keyid : str , key_dict : dict [str , Any ]) -> " SSlibKey" :
219
+ def from_dict (cls , keyid : str , key_dict : dict [str , Any ]) -> SSlibKey :
218
220
keytype , scheme , keyval = cls ._from_dict (key_dict )
219
221
220
222
# All fields left in the key_dict are unrecognized.
@@ -223,13 +225,13 @@ def from_dict(cls, keyid: str, key_dict: dict[str, Any]) -> "SSlibKey":
223
225
def to_dict (self ) -> dict [str , Any ]:
224
226
return self ._to_dict ()
225
227
226
- def _crypto_key (self ) -> " PublicKeyTypes" :
228
+ def _crypto_key (self ) -> PublicKeyTypes :
227
229
"""Helper to get a `cryptography` public key for this SSlibKey."""
228
230
public_bytes = self .keyval ["public" ].encode ("utf-8" )
229
231
return load_pem_public_key (public_bytes )
230
232
231
233
@staticmethod
232
- def _from_crypto (public_key : " PublicKeyTypes" ) -> tuple [str , str , str ]:
234
+ def _from_crypto (public_key : PublicKeyTypes ) -> tuple [str , str , str ]:
233
235
"""Return tuple of keytype, default scheme and serialized public key
234
236
value for the passed public key.
235
237
@@ -269,10 +271,10 @@ def _pem() -> str:
269
271
@classmethod
270
272
def from_crypto (
271
273
cls ,
272
- public_key : " PublicKeyTypes" ,
273
- keyid : Optional [ str ] = None ,
274
- scheme : Optional [ str ] = None ,
275
- ) -> " SSlibKey" :
274
+ public_key : PublicKeyTypes ,
275
+ keyid : str | None = None ,
276
+ scheme : str | None = None ,
277
+ ) -> SSlibKey :
276
278
"""Create SSlibKey from pyca/cryptography public key.
277
279
278
280
Args:
@@ -306,7 +308,7 @@ def from_crypto(
306
308
return SSlibKey (keyid , keytype , scheme , keyval )
307
309
308
310
@staticmethod
309
- def _get_hash_algorithm (name : str ) -> " HashAlgorithm" :
311
+ def _get_hash_algorithm (name : str ) -> HashAlgorithm :
310
312
"""Helper to return hash algorithm for name."""
311
313
algorithm : HashAlgorithm
312
314
if name == "sha224" :
@@ -321,9 +323,7 @@ def _get_hash_algorithm(name: str) -> "HashAlgorithm":
321
323
return algorithm
322
324
323
325
@staticmethod
324
- def _get_rsa_padding (
325
- name : str , hash_algorithm : "HashAlgorithm"
326
- ) -> "AsymmetricPadding" :
326
+ def _get_rsa_padding (name : str , hash_algorithm : HashAlgorithm ) -> AsymmetricPadding :
327
327
"""Helper to return rsa signature padding for name."""
328
328
padding : AsymmetricPadding
329
329
if name == "pss" :
0 commit comments