Skip to content

Commit

Permalink
Annotations for nacl.bindings.crypto_generichash (#709)
Browse files Browse the repository at this point in the history
* Annotations for `nacl.bindings.crypto_generichash`

* Run `black`
  • Loading branch information
DMRobertson authored Nov 30, 2021
1 parent 494f3df commit 89b3a40
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
57 changes: 37 additions & 20 deletions src/nacl/bindings/crypto_generichash.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -136,20 +147,23 @@ 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
"""

__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()
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 89b3a40

Please sign in to comment.