From 89b3a40b24b12f8d51f8d25a0b277a6931bc9d43 Mon Sep 17 00:00:00 2001 From: David Robertson Date: Tue, 30 Nov 2021 23:15:12 +0000 Subject: [PATCH] Annotations for `nacl.bindings.crypto_generichash` (#709) * Annotations for `nacl.bindings.crypto_generichash` * Run `black` --- pyproject.toml | 1 + src/nacl/bindings/crypto_generichash.py | 57 ++++++++++++++++--------- 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 61a08e99..532f50b0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -32,6 +32,7 @@ module = [ "nacl.bindings.crypto_aead", "nacl.bindings.crypto_box", "nacl.bindings.crypto_core", + "nacl.bindings.crypto_generichash", "nacl.encoding", "nacl.exceptions", "nacl.utils", diff --git a/src/nacl/bindings/crypto_generichash.py b/src/nacl/bindings/crypto_generichash.py index 038bebf1..6ab385a5 100644 --- a/src/nacl/bindings/crypto_generichash.py +++ b/src/nacl/bindings/crypto_generichash.py @@ -11,30 +11,36 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - +from typing import NoReturn, TypeVar from nacl import exceptions as exc from nacl._sodium import ffi, lib from nacl.exceptions import ensure -crypto_generichash_BYTES = lib.crypto_generichash_blake2b_bytes() -crypto_generichash_BYTES_MIN = lib.crypto_generichash_blake2b_bytes_min() -crypto_generichash_BYTES_MAX = lib.crypto_generichash_blake2b_bytes_max() -crypto_generichash_KEYBYTES = lib.crypto_generichash_blake2b_keybytes() -crypto_generichash_KEYBYTES_MIN = lib.crypto_generichash_blake2b_keybytes_min() -crypto_generichash_KEYBYTES_MAX = lib.crypto_generichash_blake2b_keybytes_max() -crypto_generichash_SALTBYTES = lib.crypto_generichash_blake2b_saltbytes() -crypto_generichash_PERSONALBYTES = ( +crypto_generichash_BYTES: int = lib.crypto_generichash_blake2b_bytes() +crypto_generichash_BYTES_MIN: int = lib.crypto_generichash_blake2b_bytes_min() +crypto_generichash_BYTES_MAX: int = lib.crypto_generichash_blake2b_bytes_max() +crypto_generichash_KEYBYTES: int = lib.crypto_generichash_blake2b_keybytes() +crypto_generichash_KEYBYTES_MIN: int = ( + lib.crypto_generichash_blake2b_keybytes_min() +) +crypto_generichash_KEYBYTES_MAX: int = ( + lib.crypto_generichash_blake2b_keybytes_max() +) +crypto_generichash_SALTBYTES: int = lib.crypto_generichash_blake2b_saltbytes() +crypto_generichash_PERSONALBYTES: int = ( lib.crypto_generichash_blake2b_personalbytes() ) -crypto_generichash_STATEBYTES = lib.crypto_generichash_statebytes() +crypto_generichash_STATEBYTES: int = lib.crypto_generichash_statebytes() _OVERLONG = "{0} length greater than {1} bytes" _TOOBIG = "{0} greater than {1}" -def _checkparams(digest_size, key, salt, person): +def _checkparams( + digest_size: int, key: bytes, salt: bytes, person: bytes +) -> None: """Check hash parameters""" ensure( isinstance(key, bytes), @@ -86,11 +92,16 @@ def _checkparams(digest_size, key, salt, person): def generichash_blake2b_salt_personal( - data, digest_size=crypto_generichash_BYTES, key=b"", salt=b"", person=b"" -): + data: bytes, + digest_size: int = crypto_generichash_BYTES, + key: bytes = b"", + salt: bytes = b"", + person: bytes = b"", +) -> bytes: """One shot hash interface :param data: the input data to the hash function + :type data: bytes :param digest_size: must be at most :py:data:`.crypto_generichash_BYTES_MAX`; the default digest size is @@ -136,6 +147,9 @@ def generichash_blake2b_salt_personal( return ffi.buffer(digest, digest_size)[:] +_Blake2State = TypeVar("_Blake2State", bound="Blake2State") + + class Blake2State: """ Python-level wrapper for the crypto_generichash_blake2b state buffer @@ -143,13 +157,13 @@ class Blake2State: __slots__ = ["_statebuf", "digest_size"] - def __init__(self, digest_size): + def __init__(self, digest_size: int): self._statebuf = ffi.new( "unsigned char[]", crypto_generichash_STATEBYTES ) self.digest_size = digest_size - def __reduce__(self): + def __reduce__(self) -> NoReturn: """ Raise the same exception as hashlib's blake implementation on copy.copy() @@ -158,7 +172,7 @@ def __reduce__(self): "can't pickle {} objects".format(self.__class__.__name__) ) - def copy(self): + def copy(self: _Blake2State) -> _Blake2State: _st = self.__class__(self.digest_size) ffi.memmove( _st._statebuf, self._statebuf, crypto_generichash_STATEBYTES @@ -167,8 +181,11 @@ def copy(self): def generichash_blake2b_init( - key=b"", salt=b"", person=b"", digest_size=crypto_generichash_BYTES -): + key: bytes = b"", + salt: bytes = b"", + person: bytes = b"", + digest_size: int = crypto_generichash_BYTES, +) -> Blake2State: """ Create a new initialized blake2b hash state @@ -211,7 +228,7 @@ def generichash_blake2b_init( return state -def generichash_blake2b_update(state, data): +def generichash_blake2b_update(state: Blake2State, data: bytes) -> None: """Update the blake2b hash state :param state: a initialized Blake2bState object as returned from @@ -239,7 +256,7 @@ def generichash_blake2b_update(state, data): ensure(rc == 0, "Unexpected failure", raising=exc.RuntimeError) -def generichash_blake2b_final(state): +def generichash_blake2b_final(state: Blake2State) -> bytes: """Finalize the blake2b hash state and return the digest. :param state: a initialized Blake2bState object as returned from