Use the ReadableBuffer type in more places (#4245)

This is a follow-up on #4232. memoryview, hashlib, and hmac are updated
to use ReadableBuffer type instead of their own home-spun unions of
bytes, bytearray and whatever else each use case used. mmap is being
handled in #4244, and I'll leave BinaryIO for another day (or possibly
another person) because it's going to require some messy code
duplication because the relevant methods are defined in IO[AnyStr].

There's one corner case I'm not quite sure how best to handle: the
documentation for hmac.digest claim that the parmaeters have the same
meanings as in hmac.new, but in CPython the latter has an explicit check
that `key` is bytes or bytearray while the former works with a
memory-view. For now I've matched the documentation.

Also, the documentation for HMAC.update says that `msg` can be any type
supported by hashlib from Python 3.4; but I can't see anything in the
Python 2.7 implementation that would prevent it also taking bytes-like
objects, so I've not tried to treat Python 2 any different to Python 3.
This commit is contained in:
Bruce Merry
2020-06-22 15:17:24 +02:00
committed by GitHub
parent e1d89e5742
commit fb398b1d59
5 changed files with 58 additions and 46 deletions

View File

@@ -2,8 +2,7 @@
import sys
from typing import AbstractSet, Optional, Union
_DataType = Union[bytes, bytearray, memoryview]
from _typeshed import ReadableBuffer
class _Hash(object):
digest_size: int
@@ -14,41 +13,41 @@ class _Hash(object):
# formally specified, so may not exist on some platforms
name: str
def __init__(self, data: _DataType = ...) -> None: ...
def __init__(self, data: ReadableBuffer = ...) -> None: ...
def copy(self) -> _Hash: ...
def digest(self) -> bytes: ...
def hexdigest(self) -> str: ...
def update(self, __data: _DataType) -> None: ...
def update(self, __data: ReadableBuffer) -> None: ...
if sys.version_info >= (3, 9):
def md5(string: _DataType = ..., *, usedforsecurity: bool = ...) -> _Hash: ...
def sha1(string: _DataType = ..., *, usedforsecurity: bool = ...) -> _Hash: ...
def sha224(string: _DataType = ..., *, usedforsecurity: bool = ...) -> _Hash: ...
def sha256(string: _DataType = ..., *, usedforsecurity: bool = ...) -> _Hash: ...
def sha384(string: _DataType = ..., *, usedforsecurity: bool = ...) -> _Hash: ...
def sha512(string: _DataType = ..., *, usedforsecurity: bool = ...) -> _Hash: ...
def md5(string: ReadableBuffer = ..., *, usedforsecurity: bool = ...) -> _Hash: ...
def sha1(string: ReadableBuffer = ..., *, usedforsecurity: bool = ...) -> _Hash: ...
def sha224(string: ReadableBuffer = ..., *, usedforsecurity: bool = ...) -> _Hash: ...
def sha256(string: ReadableBuffer = ..., *, usedforsecurity: bool = ...) -> _Hash: ...
def sha384(string: ReadableBuffer = ..., *, usedforsecurity: bool = ...) -> _Hash: ...
def sha512(string: ReadableBuffer = ..., *, usedforsecurity: bool = ...) -> _Hash: ...
elif sys.version_info >= (3, 8):
def md5(string: _DataType = ...) -> _Hash: ...
def sha1(string: _DataType = ...) -> _Hash: ...
def sha224(string: _DataType = ...) -> _Hash: ...
def sha256(string: _DataType = ...) -> _Hash: ...
def sha384(string: _DataType = ...) -> _Hash: ...
def sha512(string: _DataType = ...) -> _Hash: ...
def md5(string: ReadableBuffer = ...) -> _Hash: ...
def sha1(string: ReadableBuffer = ...) -> _Hash: ...
def sha224(string: ReadableBuffer = ...) -> _Hash: ...
def sha256(string: ReadableBuffer = ...) -> _Hash: ...
def sha384(string: ReadableBuffer = ...) -> _Hash: ...
def sha512(string: ReadableBuffer = ...) -> _Hash: ...
else:
def md5(__string: _DataType = ...) -> _Hash: ...
def sha1(__string: _DataType = ...) -> _Hash: ...
def sha224(__string: _DataType = ...) -> _Hash: ...
def sha256(__string: _DataType = ...) -> _Hash: ...
def sha384(__string: _DataType = ...) -> _Hash: ...
def sha512(__string: _DataType = ...) -> _Hash: ...
def md5(__string: ReadableBuffer = ...) -> _Hash: ...
def sha1(__string: ReadableBuffer = ...) -> _Hash: ...
def sha224(__string: ReadableBuffer = ...) -> _Hash: ...
def sha256(__string: ReadableBuffer = ...) -> _Hash: ...
def sha384(__string: ReadableBuffer = ...) -> _Hash: ...
def sha512(__string: ReadableBuffer = ...) -> _Hash: ...
def new(name: str, data: _DataType = ...) -> _Hash: ...
def new(name: str, data: ReadableBuffer = ...) -> _Hash: ...
algorithms_guaranteed: AbstractSet[str]
algorithms_available: AbstractSet[str]
def pbkdf2_hmac(hash_name: str, password: _DataType, salt: _DataType, iterations: int, dklen: Optional[int] = ...) -> bytes: ...
def pbkdf2_hmac(hash_name: str, password: ReadableBuffer, salt: ReadableBuffer, iterations: int, dklen: Optional[int] = ...) -> bytes: ...
if sys.version_info >= (3, 6):
class _VarLenHash(object):
@@ -56,12 +55,12 @@ if sys.version_info >= (3, 6):
block_size: int
name: str
def __init__(self, data: _DataType = ...) -> None: ...
def __init__(self, data: ReadableBuffer = ...) -> None: ...
def copy(self) -> _VarLenHash: ...
def digest(self, __length: int) -> bytes: ...
def hexdigest(self, __length: int) -> str: ...
def update(self, __data: _DataType) -> None: ...
def update(self, __data: ReadableBuffer) -> None: ...
sha3_224 = _Hash
sha3_256 = _Hash
@@ -70,7 +69,7 @@ if sys.version_info >= (3, 6):
shake_128 = _VarLenHash
shake_256 = _VarLenHash
def scrypt(password: _DataType, *, salt: Optional[_DataType] = ..., n: Optional[int] = ..., r: Optional[int] = ..., p: Optional[int] = ..., maxmem: int = ..., dklen: int = ...) -> bytes: ...
def scrypt(password: ReadableBuffer, *, salt: Optional[ReadableBuffer] = ..., n: Optional[int] = ..., r: Optional[int] = ..., p: Optional[int] = ..., maxmem: int = ..., dklen: int = ...) -> bytes: ...
class _BlakeHash(_Hash):
MAX_DIGEST_SIZE: int
@@ -79,9 +78,9 @@ if sys.version_info >= (3, 6):
SALT_SIZE: int
if sys.version_info >= (3, 9):
def __init__(self, __data: _DataType = ..., *, digest_size: int = ..., key: _DataType = ..., salt: _DataType = ..., person: _DataType = ..., fanout: int = ..., depth: int = ..., leaf_size: int = ..., node_offset: int = ..., node_depth: int = ..., inner_size: int = ..., last_node: bool = ..., usedforsecurity: bool = ...) -> None: ...
def __init__(self, __data: ReadableBuffer = ..., *, digest_size: int = ..., key: ReadableBuffer = ..., salt: ReadableBuffer = ..., person: ReadableBuffer = ..., fanout: int = ..., depth: int = ..., leaf_size: int = ..., node_offset: int = ..., node_depth: int = ..., inner_size: int = ..., last_node: bool = ..., usedforsecurity: bool = ...) -> None: ...
else:
def __init__(self, __data: _DataType = ..., *, digest_size: int = ..., key: _DataType = ..., salt: _DataType = ..., person: _DataType = ..., fanout: int = ..., depth: int = ..., leaf_size: int = ..., node_offset: int = ..., node_depth: int = ..., inner_size: int = ..., last_node: bool = ...) -> None: ...
def __init__(self, __data: ReadableBuffer = ..., *, digest_size: int = ..., key: ReadableBuffer = ..., salt: ReadableBuffer = ..., person: ReadableBuffer = ..., fanout: int = ..., depth: int = ..., leaf_size: int = ..., node_offset: int = ..., node_depth: int = ..., inner_size: int = ..., last_node: bool = ...) -> None: ...
blake2b = _BlakeHash
blake2s = _BlakeHash