From 1ce9781d88df50b4aebe888d17737eb4a252775e Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Tue, 9 May 2023 02:31:46 -0700 Subject: [PATCH] Remove use of typing.ByteString typing.ByteString's behaviour was poorly specified. It is currently scheduled for removal in Python 3.14. See also https://github.com/python/cpython/issues/91896 --- lib/Crypto/Cipher/AES.pyi | 62 ++++++++++++------------- lib/Crypto/Cipher/ARC2.pyi | 12 ++--- lib/Crypto/Cipher/ARC4.pyi | 10 ++-- lib/Crypto/Cipher/Blowfish.pyi | 12 ++--- lib/Crypto/Cipher/CAST.pyi | 12 ++--- lib/Crypto/Cipher/ChaCha20.pyi | 16 +++---- lib/Crypto/Cipher/ChaCha20_Poly1305.pyi | 22 ++++----- lib/Crypto/Cipher/DES.pyi | 12 ++--- lib/Crypto/Cipher/DES3.pyi | 12 ++--- lib/Crypto/Cipher/Salsa20.pyi | 17 ++++--- lib/Crypto/Protocol/KDF.pyi | 10 ++-- 11 files changed, 98 insertions(+), 99 deletions(-) diff --git a/lib/Crypto/Cipher/AES.pyi b/lib/Crypto/Cipher/AES.pyi index 98056c09..c9869847 100644 --- a/lib/Crypto/Cipher/AES.pyi +++ b/lib/Crypto/Cipher/AES.pyi @@ -1,4 +1,4 @@ -from typing import ByteString, Dict, Optional, Tuple, Union, overload +from typing import Dict, Tuple, overload from typing_extensions import Literal from Crypto.Cipher._mode_ecb import EcbMode @@ -27,88 +27,88 @@ MODE_OCB: Literal[12] # MODE_ECB @overload -def new(key: ByteString, +def new(key: bytes | bytearray, mode: Literal[1], use_aesni : bool = ...) -> \ EcbMode: ... # MODE_CBC @overload -def new(key: ByteString, +def new(key: bytes | bytearray, mode: Literal[2], - iv : Optional[ByteString] = ..., + iv : bytes | bytearray | None = ..., use_aesni : bool = ...) -> \ CbcMode: ... @overload -def new(key: ByteString, +def new(key: bytes | bytearray, mode: Literal[2], - IV : Optional[ByteString] = ..., + IV : bytes | bytearray | None = ..., use_aesni : bool = ...) -> \ CbcMode: ... # MODE_CFB @overload -def new(key: ByteString, +def new(key: bytes | bytearray, mode: Literal[3], - iv : Optional[ByteString] = ..., + iv : bytes | bytearray | None = ..., segment_size : int = ..., use_aesni : bool = ...) -> \ CfbMode: ... @overload -def new(key: ByteString, +def new(key: bytes | bytearray, mode: Literal[3], - IV : Optional[ByteString] = ..., + IV : bytes | bytearray | None = ..., segment_size : int = ..., use_aesni : bool = ...) -> \ CfbMode: ... # MODE_OFB @overload -def new(key: ByteString, +def new(key: bytes | bytearray, mode: Literal[5], - iv : Optional[ByteString] = ..., + iv : bytes | bytearray | None = ..., use_aesni : bool = ...) -> \ OfbMode: ... @overload -def new(key: ByteString, +def new(key: bytes | bytearray, mode: Literal[5], - IV : Optional[ByteString] = ..., + IV : bytes | bytearray | None = ..., use_aesni : bool = ...) -> \ OfbMode: ... # MODE_CTR @overload -def new(key: ByteString, +def new(key: bytes | bytearray, mode: Literal[6], - nonce : Optional[ByteString] = ..., - initial_value : Union[int, ByteString] = ..., + nonce : bytes | bytearray | None = ..., + initial_value : int | bytes | bytearray = ..., counter : Dict = ..., use_aesni : bool = ...) -> \ CtrMode: ... # MODE_OPENPGP @overload -def new(key: ByteString, +def new(key: bytes | bytearray, mode: Literal[7], - iv : Optional[ByteString] = ..., + iv : bytes | bytearray | None = ..., use_aesni : bool = ...) -> \ OpenPgpMode: ... @overload -def new(key: ByteString, +def new(key: bytes | bytearray, mode: Literal[7], - IV : Optional[ByteString] = ..., + IV : bytes | bytearray | None = ..., use_aesni : bool = ...) -> \ OpenPgpMode: ... # MODE_CCM @overload -def new(key: ByteString, +def new(key: bytes | bytearray, mode: Literal[8], - nonce : Optional[ByteString] = ..., + nonce : bytes | bytearray | None = ..., mac_len : int = ..., assoc_len : int = ..., use_aesni : bool = ...) -> \ @@ -116,35 +116,35 @@ def new(key: ByteString, # MODE_EAX @overload -def new(key: ByteString, +def new(key: bytes | bytearray, mode: Literal[9], - nonce : Optional[ByteString] = ..., + nonce : bytes | bytearray | None = ..., mac_len : int = ..., use_aesni : bool = ...) -> \ EaxMode: ... # MODE_GCM @overload -def new(key: ByteString, +def new(key: bytes | bytearray, mode: Literal[10], - nonce : Optional[ByteString] = ..., + nonce : bytes | bytearray | None = ..., use_aesni : bool = ...) -> \ SivMode: ... # MODE_SIV @overload -def new(key: ByteString, +def new(key: bytes | bytearray, mode: Literal[11], - nonce : Optional[ByteString] = ..., + nonce : bytes | bytearray | None = ..., mac_len : int = ..., use_aesni : bool = ...) -> \ GcmMode: ... # MODE_OCB @overload -def new(key: ByteString, +def new(key: bytes | bytearray, mode: Literal[12], - nonce : Optional[ByteString] = ..., + nonce : bytes | bytearray | None = ..., mac_len : int = ..., use_aesni : bool = ...) -> \ OcbMode: ... diff --git a/lib/Crypto/Cipher/ARC2.pyi b/lib/Crypto/Cipher/ARC2.pyi index 56505bf7..9ed8623d 100644 --- a/lib/Crypto/Cipher/ARC2.pyi +++ b/lib/Crypto/Cipher/ARC2.pyi @@ -1,4 +1,4 @@ -from typing import Union, Dict, Iterable, Optional, ByteString +from typing import Union, Dict, Iterable from Crypto.Cipher._mode_ecb import EcbMode from Crypto.Cipher._mode_cbc import CbcMode @@ -18,14 +18,14 @@ MODE_CTR: ARC2Mode MODE_OPENPGP: ARC2Mode MODE_EAX: ARC2Mode -def new(key: ByteString, +def new(key: bytes | bytearray, mode: ARC2Mode, - iv : Optional[ByteString] = ..., - IV : Optional[ByteString] = ..., - nonce : Optional[ByteString] = ..., + iv : bytes | bytearray | None = ..., + IV : bytes | bytearray | None = ..., + nonce : bytes | bytearray | None = ..., segment_size : int = ..., mac_len : int = ..., - initial_value : Union[int, ByteString] = ..., + initial_value : int | bytes | bytearray = ..., counter : Dict = ...) -> \ Union[EcbMode, CbcMode, CfbMode, OfbMode, CtrMode, OpenPgpMode]: ... diff --git a/lib/Crypto/Cipher/ARC4.pyi b/lib/Crypto/Cipher/ARC4.pyi index 96bf6e25..45d60367 100644 --- a/lib/Crypto/Cipher/ARC4.pyi +++ b/lib/Crypto/Cipher/ARC4.pyi @@ -1,14 +1,14 @@ -from typing import Any, Union, Iterable, ByteString +from typing import Any, Iterable class ARC4Cipher: block_size: int key_size: int - def __init__(self, key: ByteString, *args: Any, **kwargs: Any) -> None: ... - def encrypt(self, plaintext: ByteString) -> bytes: ... - def decrypt(self, ciphertext: ByteString) -> bytes: ... + def __init__(self, key: bytes | bytearray, *args: Any, **kwargs: Any) -> None: ... + def encrypt(self, plaintext: bytes | bytearray) -> bytes: ... + def decrypt(self, ciphertext: bytes | bytearray) -> bytes: ... -def new(key: ByteString, drop : int = ...) -> ARC4Cipher: ... +def new(key: bytes | bytearray, drop : int = ...) -> ARC4Cipher: ... block_size: int key_size: Iterable[int] diff --git a/lib/Crypto/Cipher/Blowfish.pyi b/lib/Crypto/Cipher/Blowfish.pyi index 460d8334..69d1afef 100644 --- a/lib/Crypto/Cipher/Blowfish.pyi +++ b/lib/Crypto/Cipher/Blowfish.pyi @@ -1,4 +1,4 @@ -from typing import Union, Dict, Iterable, ByteString, Optional +from typing import Union, Dict, Iterable from Crypto.Cipher._mode_ecb import EcbMode from Crypto.Cipher._mode_cbc import CbcMode @@ -18,14 +18,14 @@ MODE_CTR: BlowfishMode MODE_OPENPGP: BlowfishMode MODE_EAX: BlowfishMode -def new(key: ByteString, +def new(key: bytes | bytearray, mode: BlowfishMode, - iv : Optional[ByteString] = ..., - IV : Optional[ByteString] = ..., - nonce : Optional[ByteString] = ..., + iv : bytes | bytearray | None = ..., + IV : bytes | bytearray | None = ..., + nonce : bytes | bytearray | None = ..., segment_size : int = ..., mac_len : int = ..., - initial_value : Union[int, ByteString] = ..., + initial_value : int | bytes | bytearray = ..., counter : Dict = ...) -> \ Union[EcbMode, CbcMode, CfbMode, OfbMode, CtrMode, OpenPgpMode]: ... diff --git a/lib/Crypto/Cipher/CAST.pyi b/lib/Crypto/Cipher/CAST.pyi index 90caf651..fd6901ee 100644 --- a/lib/Crypto/Cipher/CAST.pyi +++ b/lib/Crypto/Cipher/CAST.pyi @@ -1,4 +1,4 @@ -from typing import Union, Dict, Iterable, Optional, ByteString +from typing import Union, Dict, Iterable from Crypto.Cipher._mode_ecb import EcbMode from Crypto.Cipher._mode_cbc import CbcMode @@ -18,14 +18,14 @@ MODE_CTR: CASTMode MODE_OPENPGP: CASTMode MODE_EAX: CASTMode -def new(key: ByteString, +def new(key: bytes | bytearray, mode: CASTMode, - iv : Optional[ByteString] = ..., - IV : Optional[ByteString] = ..., - nonce : Optional[ByteString] = ..., + iv : bytes | bytearray | None = ..., + IV : bytes | bytearray | None = ..., + nonce : bytes | bytearray | None = ..., segment_size : int = ..., mac_len : int = ..., - initial_value : Union[int, ByteString] = ..., + initial_value : int | bytes | bytearray = ..., counter : Dict = ...) -> \ Union[EcbMode, CbcMode, CfbMode, OfbMode, CtrMode, OpenPgpMode]: ... diff --git a/lib/Crypto/Cipher/ChaCha20.pyi b/lib/Crypto/Cipher/ChaCha20.pyi index ba2f6997..bd84db49 100644 --- a/lib/Crypto/Cipher/ChaCha20.pyi +++ b/lib/Crypto/Cipher/ChaCha20.pyi @@ -1,23 +1,23 @@ -from typing import Union, overload, ByteString, Optional +from typing import Union, overload -def _HChaCha20(key: ByteString, nonce: ByteString) -> bytearray: ... +def _HChaCha20(key: bytes | bytearray, nonce: bytes | bytearray) -> bytearray: ... class ChaCha20Cipher: block_size: int nonce: bytes - def __init__(self, key: ByteString, nonce: ByteString) -> None: ... + def __init__(self, key: bytes | bytearray, nonce: bytes | bytearray) -> None: ... @overload - def encrypt(self, plaintext: ByteString) -> bytes: ... + def encrypt(self, plaintext: bytes | bytearray) -> bytes: ... @overload - def encrypt(self, plaintext: ByteString, output: Union[bytearray, memoryview]) -> None: ... + def encrypt(self, plaintext: bytes | bytearray, output: Union[bytearray, memoryview]) -> None: ... @overload - def decrypt(self, plaintext: ByteString) -> bytes: ... + def decrypt(self, plaintext: bytes | bytearray) -> bytes: ... @overload - def decrypt(self, plaintext: ByteString, output: Union[bytearray, memoryview]) -> None: ... + def decrypt(self, plaintext: bytes | bytearray, output: Union[bytearray, memoryview]) -> None: ... def seek(self, position: int) -> None: ... -def new(key: ByteString, nonce: Optional[ByteString] = ...) -> ChaCha20Cipher: ... +def new(key: bytes | bytearray, nonce: bytes | bytearray | None = ...) -> ChaCha20Cipher: ... block_size: int key_size: int diff --git a/lib/Crypto/Cipher/ChaCha20_Poly1305.pyi b/lib/Crypto/Cipher/ChaCha20_Poly1305.pyi index f4b2a42a..74e05c7c 100644 --- a/lib/Crypto/Cipher/ChaCha20_Poly1305.pyi +++ b/lib/Crypto/Cipher/ChaCha20_Poly1305.pyi @@ -1,26 +1,26 @@ -from typing import Union, Tuple, overload, ByteString, Optional +from typing import Union, Tuple, overload class ChaCha20Poly1305Cipher: nonce: bytes - def __init__(self, key: ByteString, nonce: ByteString) -> None: ... - def update(self, data: ByteString) -> None: ... + def __init__(self, key: bytes | bytearray, nonce: bytes | bytearray) -> None: ... + def update(self, data: bytes | bytearray) -> None: ... @overload - def encrypt(self, plaintext: ByteString) -> bytes: ... + def encrypt(self, plaintext: bytes | bytearray) -> bytes: ... @overload - def encrypt(self, plaintext: ByteString, output: Union[bytearray, memoryview]) -> None: ... + def encrypt(self, plaintext: bytes | bytearray, output: Union[bytearray, memoryview]) -> None: ... @overload - def decrypt(self, plaintext: ByteString) -> bytes: ... + def decrypt(self, plaintext: bytes | bytearray) -> bytes: ... @overload - def decrypt(self, plaintext: ByteString, output: Union[bytearray, memoryview]) -> None: ... + def decrypt(self, plaintext: bytes | bytearray, output: Union[bytearray, memoryview]) -> None: ... def digest(self) -> bytes: ... def hexdigest(self) -> str: ... - def verify(self, received_mac_tag: ByteString) -> None: ... + def verify(self, received_mac_tag: bytes | bytearray) -> None: ... def hexverify(self, received_mac_tag: str) -> None: ... - def encrypt_and_digest(self, plaintext: ByteString) -> Tuple[bytes, bytes]: ... - def decrypt_and_verify(self, ciphertext: ByteString, received_mac_tag: ByteString) -> bytes: ... + def encrypt_and_digest(self, plaintext: bytes | bytearray) -> Tuple[bytes, bytes]: ... + def decrypt_and_verify(self, ciphertext: bytes | bytearray, received_mac_tag: bytes | bytearray) -> bytes: ... -def new(key: ByteString, nonce: Optional[ByteString] = ...) -> ChaCha20Poly1305Cipher: ... +def new(key: bytes | bytearray, nonce: bytes | bytearray | None = ...) -> ChaCha20Poly1305Cipher: ... block_size: int key_size: int diff --git a/lib/Crypto/Cipher/DES.pyi b/lib/Crypto/Cipher/DES.pyi index 1795baa9..cc16edee 100644 --- a/lib/Crypto/Cipher/DES.pyi +++ b/lib/Crypto/Cipher/DES.pyi @@ -1,4 +1,4 @@ -from typing import Union, Dict, Iterable, ByteString, Optional +from typing import Union, Dict from Crypto.Cipher._mode_ecb import EcbMode from Crypto.Cipher._mode_cbc import CbcMode @@ -18,14 +18,14 @@ MODE_CTR: DESMode MODE_OPENPGP: DESMode MODE_EAX: DESMode -def new(key: ByteString, +def new(key: bytes | bytearray, mode: DESMode, - iv : Optional[ByteString] = ..., - IV : Optional[ByteString] = ..., - nonce : Optional[ByteString] = ..., + iv : bytes | bytearray | None = ..., + IV : bytes | bytearray | None = ..., + nonce : bytes | bytearray | None = ..., segment_size : int = ..., mac_len : int = ..., - initial_value : Union[int, ByteString] = ..., + initial_value : int | bytes | bytearray = ..., counter : Dict = ...) -> \ Union[EcbMode, CbcMode, CfbMode, OfbMode, CtrMode, OpenPgpMode]: ... diff --git a/lib/Crypto/Cipher/DES3.pyi b/lib/Crypto/Cipher/DES3.pyi index 3da1d79b..b1245e68 100644 --- a/lib/Crypto/Cipher/DES3.pyi +++ b/lib/Crypto/Cipher/DES3.pyi @@ -1,4 +1,4 @@ -from typing import Union, Dict, Tuple, ByteString, Optional +from typing import Union, Dict, Tuple from Crypto.Cipher._mode_ecb import EcbMode from Crypto.Cipher._mode_cbc import CbcMode @@ -20,14 +20,14 @@ MODE_CTR: DES3Mode MODE_OPENPGP: DES3Mode MODE_EAX: DES3Mode -def new(key: ByteString, +def new(key: bytes | bytearray, mode: DES3Mode, - iv : Optional[ByteString] = ..., - IV : Optional[ByteString] = ..., - nonce : Optional[ByteString] = ..., + iv : bytes | bytearray | None = ..., + IV : bytes | bytearray | None = ..., + nonce : bytes | bytearray | None = ..., segment_size : int = ..., mac_len : int = ..., - initial_value : Union[int, ByteString] = ..., + initial_value : int | bytes | bytearray = ..., counter : Dict = ...) -> \ Union[EcbMode, CbcMode, CfbMode, OfbMode, CtrMode, OpenPgpMode]: ... diff --git a/lib/Crypto/Cipher/Salsa20.pyi b/lib/Crypto/Cipher/Salsa20.pyi index cc56808d..a9d3dc33 100644 --- a/lib/Crypto/Cipher/Salsa20.pyi +++ b/lib/Crypto/Cipher/Salsa20.pyi @@ -1,4 +1,4 @@ -from typing import Union, Tuple, Optional, overload, ByteString, Optional +from typing import Union, Tuple, overload class Salsa20Cipher: nonce: bytes @@ -6,19 +6,18 @@ class Salsa20Cipher: key_size: int def __init__(self, - key: ByteString, - nonce: ByteString) -> None: ... + key: bytes | bytearray, + nonce: bytes | bytearray) -> None: ... @overload - def encrypt(self, plaintext: ByteString) -> bytes: ... + def encrypt(self, plaintext: bytes | bytearray) -> bytes: ... @overload - def encrypt(self, plaintext: ByteString, output: Union[bytearray, memoryview]) -> None: ... + def encrypt(self, plaintext: bytes | bytearray, output: Union[bytearray, memoryview]) -> None: ... @overload - def decrypt(self, plaintext: ByteString) -> bytes: ... + def decrypt(self, plaintext: bytes | bytearray) -> bytes: ... @overload - def decrypt(self, plaintext: ByteString, output: Union[bytearray, memoryview]) -> None: ... + def decrypt(self, plaintext: bytes | bytearray, output: Union[bytearray, memoryview]) -> None: ... -def new(key: ByteString, nonce: Optional[ByteString] = ...) -> Salsa20Cipher: ... +def new(key: bytes | bytearray, nonce: bytes | bytearray | None = ...) -> Salsa20Cipher: ... block_size: int key_size: Tuple[int, int] - diff --git a/lib/Crypto/Protocol/KDF.pyi b/lib/Crypto/Protocol/KDF.pyi index df6c2873..31e1718e 100644 --- a/lib/Crypto/Protocol/KDF.pyi +++ b/lib/Crypto/Protocol/KDF.pyi @@ -1,5 +1,5 @@ from types import ModuleType -from typing import Optional, Callable, Tuple, Union, Dict, Any, ByteString, overload +from typing import Optional, Callable, Tuple, Union, Dict, Any, overload from typing_extensions import Literal RNG = Callable[[int], bytes] @@ -26,15 +26,15 @@ def bcrypt(password: Union[bytes, str], cost: int, salt: Optional[bytes]=None) - def bcrypt_check(password: Union[bytes, str], bcrypt_hash: Union[bytes, bytearray, str]) -> None: ... @overload -def SP800_108_Counter(master: ByteString, +def SP800_108_Counter(master: bytes | bytearray, key_len: int, prf: PRF, num_keys: Literal[None] = None, - label: ByteString = b'', context: ByteString = b'') -> bytes: ... + label: bytes | bytearray = b'', context: bytes | bytearray = b'') -> bytes: ... @overload -def SP800_108_Counter(master: ByteString, +def SP800_108_Counter(master: bytes | bytearray, key_len: int, prf: PRF, num_keys: int, - label: ByteString = b'', context: ByteString = b'') -> Tuple[bytes]: ... + label: bytes | bytearray = b'', context: bytes | bytearray = b'') -> Tuple[bytes]: ...