Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/PyNaCl-1.6.0.dev1-py3.8-linux-x86_64.egg/nacl/hash.py: 88%
34 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-06 06:06 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-06 06:06 +0000
1# Copyright 2013 Donald Stufft and individual contributors
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""
15The :mod:`nacl.hash` module exposes one-shot interfaces
16for libsodium selected hash primitives and the constants needed
17for their usage.
18"""
21import nacl.bindings
22import nacl.encoding
25BLAKE2B_BYTES = nacl.bindings.crypto_generichash_BYTES
26"""Default digest size for :func:`blake2b` hash"""
27BLAKE2B_BYTES_MIN = nacl.bindings.crypto_generichash_BYTES_MIN
28"""Minimum allowed digest size for :func:`blake2b` hash"""
29BLAKE2B_BYTES_MAX = nacl.bindings.crypto_generichash_BYTES_MAX
30"""Maximum allowed digest size for :func:`blake2b` hash"""
31BLAKE2B_KEYBYTES = nacl.bindings.crypto_generichash_KEYBYTES
32"""Default size of the ``key`` byte array for :func:`blake2b` hash"""
33BLAKE2B_KEYBYTES_MIN = nacl.bindings.crypto_generichash_KEYBYTES_MIN
34"""Minimum allowed size of the ``key`` byte array for :func:`blake2b` hash"""
35BLAKE2B_KEYBYTES_MAX = nacl.bindings.crypto_generichash_KEYBYTES_MAX
36"""Maximum allowed size of the ``key`` byte array for :func:`blake2b` hash"""
37BLAKE2B_SALTBYTES = nacl.bindings.crypto_generichash_SALTBYTES
38"""Maximum allowed length of the ``salt`` byte array for
39:func:`blake2b` hash"""
40BLAKE2B_PERSONALBYTES = nacl.bindings.crypto_generichash_PERSONALBYTES
41"""Maximum allowed length of the ``personalization``
42byte array for :func:`blake2b` hash"""
44SIPHASH_BYTES = nacl.bindings.crypto_shorthash_siphash24_BYTES
45"""Size of the :func:`siphash24` digest"""
46SIPHASH_KEYBYTES = nacl.bindings.crypto_shorthash_siphash24_KEYBYTES
47"""Size of the secret ``key`` used by the :func:`siphash24` MAC"""
49SIPHASHX_AVAILABLE = nacl.bindings.has_crypto_shorthash_siphashx24
50"""``True`` if :func:`siphashx24` is available to be called"""
52SIPHASHX_BYTES = nacl.bindings.crypto_shorthash_siphashx24_BYTES
53"""Size of the :func:`siphashx24` digest"""
54SIPHASHX_KEYBYTES = nacl.bindings.crypto_shorthash_siphashx24_KEYBYTES
55"""Size of the secret ``key`` used by the :func:`siphashx24` MAC"""
57_b2b_hash = nacl.bindings.crypto_generichash_blake2b_salt_personal
58_sip_hash = nacl.bindings.crypto_shorthash_siphash24
59_sip_hashx = nacl.bindings.crypto_shorthash_siphashx24
62def sha256(
63 message: bytes, encoder: nacl.encoding.Encoder = nacl.encoding.HexEncoder
64) -> bytes:
65 """
66 Hashes ``message`` with SHA256.
68 :param message: The message to hash.
69 :type message: bytes
70 :param encoder: A class that is able to encode the hashed message.
71 :returns: The hashed message.
72 :rtype: bytes
73 """
74 return encoder.encode(nacl.bindings.crypto_hash_sha256(message))
77def sha512(
78 message: bytes, encoder: nacl.encoding.Encoder = nacl.encoding.HexEncoder
79) -> bytes:
80 """
81 Hashes ``message`` with SHA512.
83 :param message: The message to hash.
84 :type message: bytes
85 :param encoder: A class that is able to encode the hashed message.
86 :returns: The hashed message.
87 :rtype: bytes
88 """
89 return encoder.encode(nacl.bindings.crypto_hash_sha512(message))
92def blake2b(
93 data: bytes,
94 digest_size: int = BLAKE2B_BYTES,
95 key: bytes = b"",
96 salt: bytes = b"",
97 person: bytes = b"",
98 encoder: nacl.encoding.Encoder = nacl.encoding.HexEncoder,
99) -> bytes:
100 """
101 Hashes ``data`` with blake2b.
103 :param data: the digest input byte sequence
104 :type data: bytes
105 :param digest_size: the requested digest size; must be at most
106 :const:`BLAKE2B_BYTES_MAX`;
107 the default digest size is
108 :const:`BLAKE2B_BYTES`
109 :type digest_size: int
110 :param key: the key to be set for keyed MAC/PRF usage; if set, the key
111 must be at most :data:`~nacl.hash.BLAKE2B_KEYBYTES_MAX` long
112 :type key: bytes
113 :param salt: an initialization salt at most
114 :const:`BLAKE2B_SALTBYTES` long;
115 it will be zero-padded if needed
116 :type salt: bytes
117 :param person: a personalization string at most
118 :const:`BLAKE2B_PERSONALBYTES` long;
119 it will be zero-padded if needed
120 :type person: bytes
121 :param encoder: the encoder to use on returned digest
122 :type encoder: class
123 :returns: The hashed message.
124 :rtype: bytes
125 """
127 digest = _b2b_hash(
128 data, digest_size=digest_size, key=key, salt=salt, person=person
129 )
130 return encoder.encode(digest)
133generichash = blake2b
136def siphash24(
137 message: bytes,
138 key: bytes = b"",
139 encoder: nacl.encoding.Encoder = nacl.encoding.HexEncoder,
140) -> bytes:
141 """
142 Computes a keyed MAC of ``message`` using the short-input-optimized
143 siphash-2-4 construction.
145 :param message: The message to hash.
146 :type message: bytes
147 :param key: the message authentication key for the siphash MAC construct
148 :type key: bytes(:const:`SIPHASH_KEYBYTES`)
149 :param encoder: A class that is able to encode the hashed message.
150 :returns: The hashed message.
151 :rtype: bytes(:const:`SIPHASH_BYTES`)
152 """
153 digest = _sip_hash(message, key)
154 return encoder.encode(digest)
157shorthash = siphash24
160def siphashx24(
161 message: bytes,
162 key: bytes = b"",
163 encoder: nacl.encoding.Encoder = nacl.encoding.HexEncoder,
164) -> bytes:
165 """
166 Computes a keyed MAC of ``message`` using the 128 bit variant of the
167 siphash-2-4 construction.
169 :param message: The message to hash.
170 :type message: bytes
171 :param key: the message authentication key for the siphash MAC construct
172 :type key: bytes(:const:`SIPHASHX_KEYBYTES`)
173 :param encoder: A class that is able to encode the hashed message.
174 :returns: The hashed message.
175 :rtype: bytes(:const:`SIPHASHX_BYTES`)
176 :raises nacl.exceptions.UnavailableError: If called when using a
177 minimal build of libsodium.
179 .. versionadded:: 1.2
180 """
181 digest = _sip_hashx(message, key)
182 return encoder.encode(digest)