|
1 |
| -from typing import Any |
| 1 | +import sys |
| 2 | +from hashlib import _Hash |
| 3 | +from typing import Any, Set, Dict, Optional, ClassVar, Union, Generic, TypeVar |
2 | 4 |
|
3 |
| -class Algorithm(Any): ... # type: ignore |
| 5 | +requires_cryptography = Set[str] |
| 6 | + |
| 7 | +def get_default_algorithms() -> Dict[str, Algorithm]: ... |
| 8 | + |
| 9 | +_K = TypeVar("_K") |
| 10 | + |
| 11 | +class Algorithm(Generic[_K]): |
| 12 | + def prepare_key(self, key: _K) -> _K: ... |
| 13 | + def sign(self, msg: bytes, key: _K) -> bytes: ... |
| 14 | + def verify(self, msg: bytes, key: _K, sig: bytes) -> bool: ... |
| 15 | + @staticmethod |
| 16 | + def to_jwk(key_obj: Any) -> str: ... # should be key_obj: _K, see python/mypy#1337 |
| 17 | + @staticmethod |
| 18 | + def from_jwk(jwk: str) -> Any: ... # should return _K, see python/mypy#1337 |
| 19 | + |
| 20 | + |
| 21 | +class NoneAlgorithm(Algorithm[None]): |
| 22 | + def prepare_key(self, key: Optional[str]) -> None: ... |
| 23 | + |
| 24 | +class _HashAlg: |
| 25 | + def __call__(self, arg: Union[bytes, bytearray, memoryview] = ...) -> _Hash: ... |
| 26 | + |
| 27 | +if sys.version_info >= (3, 6): |
| 28 | + _LoadsString = Union[str, bytes, bytearray] |
| 29 | +else: |
| 30 | + _LoadsString = str |
| 31 | + |
| 32 | +class HMACAlgorithm(Algorithm[bytes]): |
| 33 | + SHA256: ClassVar[_HashAlg] |
| 34 | + SHA384: ClassVar[_HashAlg] |
| 35 | + SHA512: ClassVar[_HashAlg] |
| 36 | + hash_alg: _HashAlg |
| 37 | + def __init__(self, _HashAlg) -> None: ... |
| 38 | + def prepare_key(self, key: Union[str, bytes]) -> bytes: ... |
| 39 | + @staticmethod |
| 40 | + def to_jwk(key_obj: Union[str, bytes]) -> str: ... |
| 41 | + @staticmethod |
| 42 | + def from_jwk(jwk: _LoadsString) -> bytes: ... |
| 43 | + |
| 44 | +# Only defined if cryptography is installed. Types should be tightened when |
| 45 | +# cryptography gets type hints. |
| 46 | +# See https://github.com/python/typeshed/issues/2542 |
| 47 | +class RSAAlgorithm(Algorithm): |
| 48 | + SHA256: ClassVar[Any] |
| 49 | + SHA384: ClassVar[Any] |
| 50 | + SHA512: ClassVar[Any] |
| 51 | + hash_alg: Any |
| 52 | + def __init__(self, hash_alg: Any) -> None: ... |
| 53 | + def prepare_key(self, key: Any) -> Any: ... |
| 54 | + @staticmethod |
| 55 | + def to_jwk(key_obj: Any) -> str: ... |
| 56 | + @staticmethod |
| 57 | + def from_jwk(jwk: _LoadsString) -> Any: ... |
| 58 | + def sign(self, msg: bytes, key: Any) -> bytes: ... |
| 59 | + def verify(self, msg: bytes, key: Any, sig: bytes) -> bool: ... |
| 60 | + |
| 61 | +# Only defined if cryptography is installed. Types should be tightened when |
| 62 | +# cryptography gets type hints. |
| 63 | +# See https://github.com/python/typeshed/issues/2542 |
| 64 | +class ECAlgorithm(Algorithm): |
| 65 | + SHA256: ClassVar[Any] |
| 66 | + SHA384: ClassVar[Any] |
| 67 | + SHA512: ClassVar[Any] |
| 68 | + hash_alg: Any |
| 69 | + def __init__(self, hash_alg: Any) -> None: ... |
| 70 | + def prepare_key(self, key: Any) -> Any: ... |
| 71 | + @staticmethod |
| 72 | + def to_jwk(key_obj: Any) -> str: ... |
| 73 | + @staticmethod |
| 74 | + def from_jwk(jwk: _LoadsString) -> Any: ... |
| 75 | + def sign(self, msg: bytes, key: Any) -> bytes: ... |
| 76 | + def verify(self, msg: bytes, key: Any, sig: bytes) -> bool: ... |
| 77 | + |
| 78 | +# Only defined if cryptography is installed. Types should be tightened when |
| 79 | +# cryptography gets type hints. |
| 80 | +# See https://github.com/python/typeshed/issues/2542 |
| 81 | +class RSAPSSAlgorithm(RSAAlgorithm): |
| 82 | + def sign(self, msg: bytes, key: Any) -> bytes: ... |
| 83 | + def verify(self, msg: bytes, key: Any, sig: bytes) -> bool: ... |
0 commit comments