blob: 6eb3a7bb4cc325fd0f92266e606fa58fb07678de [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`hashlib` --- Secure hashes and message digests
2====================================================
3
4.. module:: hashlib
5 :synopsis: Secure hash and message digest algorithms.
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04006
Benjamin Peterson058e31e2009-01-16 03:54:08 +00007.. moduleauthor:: Gregory P. Smith <greg@krypto.org>
8.. sectionauthor:: Gregory P. Smith <greg@krypto.org>
Georg Brandl116aa622007-08-15 14:28:22 +00009
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040010**Source code:** :source:`Lib/hashlib.py`
Georg Brandl116aa622007-08-15 14:28:22 +000011
Georg Brandl116aa622007-08-15 14:28:22 +000012.. index::
13 single: message digest, MD5
14 single: secure hash algorithm, SHA1, SHA224, SHA256, SHA384, SHA512
15
Zachary Ware4199bba2016-08-10 01:05:19 -050016.. testsetup::
17
18 import hashlib
19
20
Raymond Hettinger469271d2011-01-27 20:38:46 +000021--------------
22
Georg Brandl116aa622007-08-15 14:28:22 +000023This module implements a common interface to many different secure hash and
24message digest algorithms. Included are the FIPS secure hash algorithms SHA1,
25SHA224, SHA256, SHA384, and SHA512 (defined in FIPS 180-2) as well as RSA's MD5
Georg Brandl67ced422007-09-06 14:09:10 +000026algorithm (defined in Internet :rfc:`1321`). The terms "secure hash" and
27"message digest" are interchangeable. Older algorithms were called message
28digests. The modern term is secure hash.
Georg Brandl116aa622007-08-15 14:28:22 +000029
Christian Heimesd5e2b6f2008-03-19 21:50:51 +000030.. note::
Georg Brandl6e94a302013-10-06 18:26:36 +020031
32 If you want the adler32 or crc32 hash functions, they are available in
Christian Heimesd5e2b6f2008-03-19 21:50:51 +000033 the :mod:`zlib` module.
34
Georg Brandl116aa622007-08-15 14:28:22 +000035.. warning::
36
Georg Brandl6e94a302013-10-06 18:26:36 +020037 Some algorithms have known hash collision weaknesses, refer to the "See
38 also" section at the end.
Georg Brandl116aa622007-08-15 14:28:22 +000039
Christian Heimese92ef132013-10-13 00:52:43 +020040
R David Murraycde1a062013-12-20 16:33:52 -050041.. _hash-algorithms:
42
Christian Heimese92ef132013-10-13 00:52:43 +020043Hash algorithms
44---------------
45
Georg Brandl116aa622007-08-15 14:28:22 +000046There is one constructor method named for each type of :dfn:`hash`. All return
Gregory P. Smith8907dcd2016-06-11 17:56:12 -070047a hash object with the same simple interface. For example: use :func:`sha256` to
48create a SHA-256 hash object. You can now feed this object with :term:`bytes-like
Serhiy Storchakae5ea1ab2016-05-18 13:54:54 +030049objects <bytes-like object>` (normally :class:`bytes`) using the :meth:`update` method.
Ezio Melottic228e962013-05-04 18:06:34 +030050At any point you can ask it for the :dfn:`digest` of the
Georg Brandl67ced422007-09-06 14:09:10 +000051concatenation of the data fed to it so far using the :meth:`digest` or
52:meth:`hexdigest` methods.
53
54.. note::
55
Benjamin Peterson9cb7bd22012-12-20 20:24:37 -060056 For better multithreading performance, the Python :term:`GIL` is released for
Jesus Cea5b22dd82013-10-04 04:20:37 +020057 data larger than 2047 bytes at object creation or on update.
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +000058
59.. note::
60
Benjamin Petersonbd584d52012-12-20 20:22:47 -060061 Feeding string objects into :meth:`update` is not supported, as hashes work
Georg Brandl67ced422007-09-06 14:09:10 +000062 on bytes, not on characters.
Georg Brandl116aa622007-08-15 14:28:22 +000063
Thomas Wouters1b7f8912007-09-19 03:06:30 +000064.. index:: single: OpenSSL; (use in module hashlib)
Georg Brandl116aa622007-08-15 14:28:22 +000065
66Constructors for hash algorithms that are always present in this module are
Gregory P. Smith8907dcd2016-06-11 17:56:12 -070067:func:`sha1`, :func:`sha224`, :func:`sha256`, :func:`sha384`,
Christian Heimes121b9482016-09-06 22:03:25 +020068:func:`sha512`, :func:`blake2b`, and :func:`blake2s`.
69:func:`md5` is normally available as well, though it
Christian Heimes7cad53e2019-09-13 02:30:00 +020070may be missing or blocked if you are using a rare "FIPS compliant" build of Python.
Gregory P. Smith8907dcd2016-06-11 17:56:12 -070071Additional algorithms may also be available depending upon the OpenSSL
Christian Heimes6fe2a752016-09-07 11:58:24 +020072library that Python uses on your platform. On most platforms the
73:func:`sha3_224`, :func:`sha3_256`, :func:`sha3_384`, :func:`sha3_512`,
74:func:`shake_128`, :func:`shake_256` are also available.
75
76.. versionadded:: 3.6
77 SHA3 (Keccak) and SHAKE constructors :func:`sha3_224`, :func:`sha3_256`,
78 :func:`sha3_384`, :func:`sha3_512`, :func:`shake_128`, :func:`shake_256`.
Christian Heimes4a0270d2012-10-06 02:23:36 +020079
Christian Heimes121b9482016-09-06 22:03:25 +020080.. versionadded:: 3.6
81 :func:`blake2b` and :func:`blake2s` were added.
82
Christian Heimes7cad53e2019-09-13 02:30:00 +020083.. versionchanged:: 3.9
84 All hashlib constructors take a keyword-only argument *usedforsecurity*
85 with default value *True*. A false value allows the use of insecure and
86 blocked hashing algorithms in restricted environments. *False* indicates
87 that the hashing algorithm is not used in a security context, e.g. as a
88 non-cryptographic one-way compression function.
89
Georg Brandl67ced422007-09-06 14:09:10 +000090For example, to obtain the digest of the byte string ``b'Nobody inspects the
91spammish repetition'``::
Georg Brandl116aa622007-08-15 14:28:22 +000092
93 >>> import hashlib
Gregory P. Smith8907dcd2016-06-11 17:56:12 -070094 >>> m = hashlib.sha256()
Georg Brandl67ced422007-09-06 14:09:10 +000095 >>> m.update(b"Nobody inspects")
96 >>> m.update(b" the spammish repetition")
Georg Brandl116aa622007-08-15 14:28:22 +000097 >>> m.digest()
Gregory P. Smith8907dcd2016-06-11 17:56:12 -070098 b'\x03\x1e\xdd}Ae\x15\x93\xc5\xfe\\\x00o\xa5u+7\xfd\xdf\xf7\xbcN\x84:\xa6\xaf\x0c\x95\x0fK\x94\x06'
Guido van Rossuma19f80c2007-11-06 20:51:31 +000099 >>> m.digest_size
Gregory P. Smith8907dcd2016-06-11 17:56:12 -0700100 32
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000101 >>> m.block_size
102 64
Georg Brandl116aa622007-08-15 14:28:22 +0000103
Christian Heimesfe337bf2008-03-23 21:54:12 +0000104More condensed:
Georg Brandl116aa622007-08-15 14:28:22 +0000105
Georg Brandl67ced422007-09-06 14:09:10 +0000106 >>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
Benjamin Peterson0fa3f3d2008-12-29 20:52:09 +0000107 'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
Georg Brandl116aa622007-08-15 14:28:22 +0000108
Christian Heimes7cad53e2019-09-13 02:30:00 +0200109.. function:: new(name[, data], *, usedforsecurity=True)
Gregory P. Smith13b55292010-09-06 08:30:23 +0000110
Serhiy Storchakaf1d36d82018-07-31 09:50:16 +0300111 Is a generic constructor that takes the string *name* of the desired
Gregory P. Smith13b55292010-09-06 08:30:23 +0000112 algorithm as its first parameter. It also exists to allow access to the
113 above listed hashes as well as any other algorithms that your OpenSSL
114 library may offer. The named constructors are much faster than :func:`new`
115 and should be preferred.
Georg Brandl116aa622007-08-15 14:28:22 +0000116
Christian Heimesfe337bf2008-03-23 21:54:12 +0000117Using :func:`new` with an algorithm provided by OpenSSL:
Georg Brandl116aa622007-08-15 14:28:22 +0000118
119 >>> h = hashlib.new('ripemd160')
Georg Brandl67ced422007-09-06 14:09:10 +0000120 >>> h.update(b"Nobody inspects the spammish repetition")
Georg Brandl116aa622007-08-15 14:28:22 +0000121 >>> h.hexdigest()
Benjamin Peterson0fa3f3d2008-12-29 20:52:09 +0000122 'cc4a5ce1b3df48aec5d22d1f16b894a0b894eccc'
Georg Brandl116aa622007-08-15 14:28:22 +0000123
Gregory P. Smith13b55292010-09-06 08:30:23 +0000124Hashlib provides the following constant attributes:
Gregory P. Smith86508cc2010-03-01 02:05:26 +0000125
Gregory P. Smith13b55292010-09-06 08:30:23 +0000126.. data:: algorithms_guaranteed
Gregory P. Smith86508cc2010-03-01 02:05:26 +0000127
Larry Hastings3732ed22014-03-15 21:13:56 -0700128 A set containing the names of the hash algorithms guaranteed to be supported
Gregory P. Smith7bfb4152016-06-11 18:02:13 -0700129 by this module on all platforms. Note that 'md5' is in this list despite
130 some upstream vendors offering an odd "FIPS compliant" Python build that
131 excludes it.
Gregory P. Smith13b55292010-09-06 08:30:23 +0000132
133 .. versionadded:: 3.2
134
135.. data:: algorithms_available
136
Larry Hastings3732ed22014-03-15 21:13:56 -0700137 A set containing the names of the hash algorithms that are available in the
138 running Python interpreter. These names will be recognized when passed to
139 :func:`new`. :attr:`algorithms_guaranteed` will always be a subset. The
140 same algorithm may appear multiple times in this set under different names
141 (thanks to OpenSSL).
Gregory P. Smith86508cc2010-03-01 02:05:26 +0000142
143 .. versionadded:: 3.2
144
Georg Brandl116aa622007-08-15 14:28:22 +0000145The following values are provided as constant attributes of the hash objects
146returned by the constructors:
147
148
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000149.. data:: hash.digest_size
Georg Brandl116aa622007-08-15 14:28:22 +0000150
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000151 The size of the resulting hash in bytes.
152
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000153.. data:: hash.block_size
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000154
155 The internal block size of the hash algorithm in bytes.
Georg Brandl116aa622007-08-15 14:28:22 +0000156
Jason R. Coombsb2aa6f42013-08-03 11:39:39 +0200157A hash object has the following attributes:
158
159.. attribute:: hash.name
160
161 The canonical name of this hash, always lowercase and always suitable as a
162 parameter to :func:`new` to create another hash of this type.
163
164 .. versionchanged:: 3.4
165 The name attribute has been present in CPython since its inception, but
166 until Python 3.4 was not formally specified, so may not exist on some
167 platforms.
168
Georg Brandl116aa622007-08-15 14:28:22 +0000169A hash object has the following methods:
170
171
Serhiy Storchakaf1d36d82018-07-31 09:50:16 +0300172.. method:: hash.update(data)
Georg Brandl116aa622007-08-15 14:28:22 +0000173
Serhiy Storchakaf1d36d82018-07-31 09:50:16 +0300174 Update the hash object with the :term:`bytes-like object`.
175 Repeated calls are equivalent to a single call with the
Georg Brandl67ced422007-09-06 14:09:10 +0000176 concatenation of all the arguments: ``m.update(a); m.update(b)`` is
177 equivalent to ``m.update(a+b)``.
Georg Brandl116aa622007-08-15 14:28:22 +0000178
Georg Brandl705d9d52009-05-05 09:29:50 +0000179 .. versionchanged:: 3.1
Georg Brandl67b21b72010-08-17 15:07:14 +0000180 The Python GIL is released to allow other threads to run while hash
Jesus Cea5b22dd82013-10-04 04:20:37 +0200181 updates on data larger than 2047 bytes is taking place when using hash
Georg Brandl67b21b72010-08-17 15:07:14 +0000182 algorithms supplied by OpenSSL.
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000183
Georg Brandl116aa622007-08-15 14:28:22 +0000184
185.. method:: hash.digest()
186
Georg Brandl67ced422007-09-06 14:09:10 +0000187 Return the digest of the data passed to the :meth:`update` method so far.
Senthil Kumaran627284c2010-12-30 07:07:58 +0000188 This is a bytes object of size :attr:`digest_size` which may contain bytes in
Georg Brandl67ced422007-09-06 14:09:10 +0000189 the whole range from 0 to 255.
Georg Brandl116aa622007-08-15 14:28:22 +0000190
191
192.. method:: hash.hexdigest()
193
Georg Brandl67ced422007-09-06 14:09:10 +0000194 Like :meth:`digest` except the digest is returned as a string object of
195 double length, containing only hexadecimal digits. This may be used to
196 exchange the value safely in email or other non-binary environments.
Georg Brandl116aa622007-08-15 14:28:22 +0000197
198
199.. method:: hash.copy()
200
201 Return a copy ("clone") of the hash object. This can be used to efficiently
Georg Brandl67ced422007-09-06 14:09:10 +0000202 compute the digests of data sharing a common initial substring.
Georg Brandl116aa622007-08-15 14:28:22 +0000203
204
Christian Heimes6fe2a752016-09-07 11:58:24 +0200205SHAKE variable length digests
206-----------------------------
207
208The :func:`shake_128` and :func:`shake_256` algorithms provide variable
209length digests with length_in_bits//2 up to 128 or 256 bits of security.
210As such, their digest methods require a length. Maximum length is not limited
211by the SHAKE algorithm.
212
213.. method:: shake.digest(length)
214
215 Return the digest of the data passed to the :meth:`update` method so far.
Serhiy Storchakaf1d36d82018-07-31 09:50:16 +0300216 This is a bytes object of size *length* which may contain bytes in
Christian Heimes6fe2a752016-09-07 11:58:24 +0200217 the whole range from 0 to 255.
218
219
220.. method:: shake.hexdigest(length)
221
222 Like :meth:`digest` except the digest is returned as a string object of
223 double length, containing only hexadecimal digits. This may be used to
224 exchange the value safely in email or other non-binary environments.
225
226
Benjamin Petersonc402d8d2015-09-27 01:23:10 -0700227Key derivation
228--------------
Christian Heimese92ef132013-10-13 00:52:43 +0200229
230Key derivation and key stretching algorithms are designed for secure password
Benjamin Peterson0ccff4d2014-05-26 15:41:26 -0700231hashing. Naive algorithms such as ``sha1(password)`` are not resistant against
232brute-force attacks. A good password hashing function must be tunable, slow, and
Benjamin Peterson0d81d802014-05-26 15:42:29 -0700233include a `salt <https://en.wikipedia.org/wiki/Salt_%28cryptography%29>`_.
Christian Heimese92ef132013-10-13 00:52:43 +0200234
235
Martin Panterbc85e352016-02-22 09:21:49 +0000236.. function:: pbkdf2_hmac(hash_name, password, salt, iterations, dklen=None)
Christian Heimese92ef132013-10-13 00:52:43 +0200237
238 The function provides PKCS#5 password-based key derivation function 2. It
239 uses HMAC as pseudorandom function.
240
Martin Panterbc85e352016-02-22 09:21:49 +0000241 The string *hash_name* is the desired name of the hash digest algorithm for
Christian Heimese92ef132013-10-13 00:52:43 +0200242 HMAC, e.g. 'sha1' or 'sha256'. *password* and *salt* are interpreted as
243 buffers of bytes. Applications and libraries should limit *password* to
Martin Panterbc85e352016-02-22 09:21:49 +0000244 a sensible length (e.g. 1024). *salt* should be about 16 or more bytes from
Christian Heimese92ef132013-10-13 00:52:43 +0200245 a proper source, e.g. :func:`os.urandom`.
246
Martin Panterbc85e352016-02-22 09:21:49 +0000247 The number of *iterations* should be chosen based on the hash algorithm and
248 computing power. As of 2013, at least 100,000 iterations of SHA-256 are
249 suggested.
Christian Heimese92ef132013-10-13 00:52:43 +0200250
251 *dklen* is the length of the derived key. If *dklen* is ``None`` then the
Martin Panterbc85e352016-02-22 09:21:49 +0000252 digest size of the hash algorithm *hash_name* is used, e.g. 64 for SHA-512.
Christian Heimese92ef132013-10-13 00:52:43 +0200253
Ville Skyttä959625b2018-09-11 04:07:19 +0300254 >>> import hashlib
Christian Heimese92ef132013-10-13 00:52:43 +0200255 >>> dk = hashlib.pbkdf2_hmac('sha256', b'password', b'salt', 100000)
Ville Skyttä959625b2018-09-11 04:07:19 +0300256 >>> dk.hex()
257 '0394a2ede332c9a13eb82e9b24631604c31df978b4e2f0fbd2c549944f9d79a5'
Christian Heimese92ef132013-10-13 00:52:43 +0200258
259 .. versionadded:: 3.4
260
Benjamin Petersonf9ea5f32014-05-26 15:45:14 -0700261 .. note::
262
263 A fast implementation of *pbkdf2_hmac* is available with OpenSSL. The
264 Python implementation uses an inline version of :mod:`hmac`. It is about
265 three times slower and doesn't release the GIL.
Christian Heimese92ef132013-10-13 00:52:43 +0200266
Christian Heimes39093e92016-09-06 20:22:28 +0200267.. function:: scrypt(password, *, salt, n, r, p, maxmem=0, dklen=64)
268
269 The function provides scrypt password-based key derivation function as
270 defined in :rfc:`7914`.
271
Serhiy Storchakaf1d36d82018-07-31 09:50:16 +0300272 *password* and *salt* must be :term:`bytes-like objects
273 <bytes-like object>`. Applications and libraries should limit *password*
274 to a sensible length (e.g. 1024). *salt* should be about 16 or more
275 bytes from a proper source, e.g. :func:`os.urandom`.
Christian Heimes39093e92016-09-06 20:22:28 +0200276
277 *n* is the CPU/Memory cost factor, *r* the block size, *p* parallelization
Victor Stinner8c663fd2017-11-08 14:44:44 -0800278 factor and *maxmem* limits memory (OpenSSL 1.1.0 defaults to 32 MiB).
Christian Heimes39093e92016-09-06 20:22:28 +0200279 *dklen* is the length of the derived key.
280
Cheryl Sabella2d6097d2018-10-12 10:55:20 -0400281 .. availability:: OpenSSL 1.1+.
Christian Heimes39093e92016-09-06 20:22:28 +0200282
283 .. versionadded:: 3.6
284
Christian Heimese92ef132013-10-13 00:52:43 +0200285
Christian Heimes121b9482016-09-06 22:03:25 +0200286BLAKE2
287------
288
INADA Naokie2f9e772017-01-13 19:29:58 +0900289.. sectionauthor:: Dmitry Chestnykh
290
291.. index::
292 single: blake2b, blake2s
293
Serhiy Storchaka0a36ac12018-05-31 07:39:00 +0300294BLAKE2_ is a cryptographic hash function defined in :rfc:`7693` that comes in two
INADA Naokie2f9e772017-01-13 19:29:58 +0900295flavors:
296
297* **BLAKE2b**, optimized for 64-bit platforms and produces digests of any size
298 between 1 and 64 bytes,
299
300* **BLAKE2s**, optimized for 8- to 32-bit platforms and produces digests of any
301 size between 1 and 32 bytes.
302
303BLAKE2 supports **keyed mode** (a faster and simpler replacement for HMAC_),
304**salted hashing**, **personalization**, and **tree hashing**.
305
306Hash objects from this module follow the API of standard library's
307:mod:`hashlib` objects.
308
309
310Creating hash objects
311^^^^^^^^^^^^^^^^^^^^^
312
313New hash objects are created by calling constructor functions:
314
315
Serhiy Storchakaf1d36d82018-07-31 09:50:16 +0300316.. function:: blake2b(data=b'', *, digest_size=64, key=b'', salt=b'', \
INADA Naokie2f9e772017-01-13 19:29:58 +0900317 person=b'', fanout=1, depth=1, leaf_size=0, node_offset=0, \
Christian Heimes7cad53e2019-09-13 02:30:00 +0200318 node_depth=0, inner_size=0, last_node=False, \
319 usedforsecurity=True)
INADA Naokie2f9e772017-01-13 19:29:58 +0900320
Serhiy Storchakaf1d36d82018-07-31 09:50:16 +0300321.. function:: blake2s(data=b'', *, digest_size=32, key=b'', salt=b'', \
INADA Naokie2f9e772017-01-13 19:29:58 +0900322 person=b'', fanout=1, depth=1, leaf_size=0, node_offset=0, \
Christian Heimes7cad53e2019-09-13 02:30:00 +0200323 node_depth=0, inner_size=0, last_node=False, \
324 usedforsecurity=True)
INADA Naokie2f9e772017-01-13 19:29:58 +0900325
326
327These functions return the corresponding hash objects for calculating
328BLAKE2b or BLAKE2s. They optionally take these general parameters:
329
Serhiy Storchakaf1d36d82018-07-31 09:50:16 +0300330* *data*: initial chunk of data to hash, which must be
331 :term:`bytes-like object`. It can be passed only as positional argument.
INADA Naokie2f9e772017-01-13 19:29:58 +0900332
333* *digest_size*: size of output digest in bytes.
334
335* *key*: key for keyed hashing (up to 64 bytes for BLAKE2b, up to 32 bytes for
336 BLAKE2s).
337
338* *salt*: salt for randomized hashing (up to 16 bytes for BLAKE2b, up to 8
339 bytes for BLAKE2s).
340
341* *person*: personalization string (up to 16 bytes for BLAKE2b, up to 8 bytes
342 for BLAKE2s).
343
344The following table shows limits for general parameters (in bytes):
345
346======= =========== ======== ========= ===========
347Hash digest_size len(key) len(salt) len(person)
348======= =========== ======== ========= ===========
349BLAKE2b 64 64 16 16
350BLAKE2s 32 32 8 8
351======= =========== ======== ========= ===========
352
353.. note::
354
355 BLAKE2 specification defines constant lengths for salt and personalization
356 parameters, however, for convenience, this implementation accepts byte
357 strings of any size up to the specified length. If the length of the
358 parameter is less than specified, it is padded with zeros, thus, for
359 example, ``b'salt'`` and ``b'salt\x00'`` is the same value. (This is not
360 the case for *key*.)
361
362These sizes are available as module `constants`_ described below.
363
364Constructor functions also accept the following tree hashing parameters:
365
366* *fanout*: fanout (0 to 255, 0 if unlimited, 1 in sequential mode).
367
368* *depth*: maximal depth of tree (1 to 255, 255 if unlimited, 1 in
369 sequential mode).
370
371* *leaf_size*: maximal byte length of leaf (0 to 2**32-1, 0 if unlimited or in
372 sequential mode).
373
374* *node_offset*: node offset (0 to 2**64-1 for BLAKE2b, 0 to 2**48-1 for
375 BLAKE2s, 0 for the first, leftmost, leaf, or in sequential mode).
376
377* *node_depth*: node depth (0 to 255, 0 for leaves, or in sequential mode).
378
379* *inner_size*: inner digest size (0 to 64 for BLAKE2b, 0 to 32 for
380 BLAKE2s, 0 in sequential mode).
381
382* *last_node*: boolean indicating whether the processed node is the last
383 one (`False` for sequential mode).
384
385.. figure:: hashlib-blake2-tree.png
386 :alt: Explanation of tree mode parameters.
387
388See section 2.10 in `BLAKE2 specification
389<https://blake2.net/blake2_20130129.pdf>`_ for comprehensive review of tree
390hashing.
391
392
393Constants
394^^^^^^^^^
395
396.. data:: blake2b.SALT_SIZE
397.. data:: blake2s.SALT_SIZE
398
399Salt length (maximum length accepted by constructors).
400
401
402.. data:: blake2b.PERSON_SIZE
403.. data:: blake2s.PERSON_SIZE
404
405Personalization string length (maximum length accepted by constructors).
406
407
408.. data:: blake2b.MAX_KEY_SIZE
409.. data:: blake2s.MAX_KEY_SIZE
410
411Maximum key size.
412
413
414.. data:: blake2b.MAX_DIGEST_SIZE
415.. data:: blake2s.MAX_DIGEST_SIZE
416
417Maximum digest size that the hash function can output.
418
419
420Examples
421^^^^^^^^
422
423Simple hashing
424""""""""""""""
425
426To calculate hash of some data, you should first construct a hash object by
427calling the appropriate constructor function (:func:`blake2b` or
428:func:`blake2s`), then update it with the data by calling :meth:`update` on the
429object, and, finally, get the digest out of the object by calling
430:meth:`digest` (or :meth:`hexdigest` for hex-encoded string).
431
432 >>> from hashlib import blake2b
433 >>> h = blake2b()
434 >>> h.update(b'Hello world')
435 >>> h.hexdigest()
436 '6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'
437
438
439As a shortcut, you can pass the first chunk of data to update directly to the
Serhiy Storchakaf1d36d82018-07-31 09:50:16 +0300440constructor as the positional argument:
INADA Naokie2f9e772017-01-13 19:29:58 +0900441
442 >>> from hashlib import blake2b
443 >>> blake2b(b'Hello world').hexdigest()
444 '6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'
445
446You can call :meth:`hash.update` as many times as you need to iteratively
447update the hash:
448
449 >>> from hashlib import blake2b
450 >>> items = [b'Hello', b' ', b'world']
451 >>> h = blake2b()
452 >>> for item in items:
453 ... h.update(item)
454 >>> h.hexdigest()
455 '6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'
456
457
458Using different digest sizes
459""""""""""""""""""""""""""""
460
461BLAKE2 has configurable size of digests up to 64 bytes for BLAKE2b and up to 32
462bytes for BLAKE2s. For example, to replace SHA-1 with BLAKE2b without changing
463the size of output, we can tell BLAKE2b to produce 20-byte digests:
464
465 >>> from hashlib import blake2b
466 >>> h = blake2b(digest_size=20)
467 >>> h.update(b'Replacing SHA1 with the more secure function')
468 >>> h.hexdigest()
469 'd24f26cf8de66472d58d4e1b1774b4c9158b1f4c'
470 >>> h.digest_size
471 20
472 >>> len(h.digest())
473 20
474
475Hash objects with different digest sizes have completely different outputs
476(shorter hashes are *not* prefixes of longer hashes); BLAKE2b and BLAKE2s
477produce different outputs even if the output length is the same:
478
479 >>> from hashlib import blake2b, blake2s
480 >>> blake2b(digest_size=10).hexdigest()
481 '6fa1d8fcfd719046d762'
482 >>> blake2b(digest_size=11).hexdigest()
483 'eb6ec15daf9546254f0809'
484 >>> blake2s(digest_size=10).hexdigest()
485 '1bf21a98c78a1c376ae9'
486 >>> blake2s(digest_size=11).hexdigest()
487 '567004bf96e4a25773ebf4'
488
489
490Keyed hashing
491"""""""""""""
492
493Keyed hashing can be used for authentication as a faster and simpler
494replacement for `Hash-based message authentication code
Sanyam Khurana1b4587a2017-12-06 22:09:33 +0530495<https://en.wikipedia.org/wiki/Hash-based_message_authentication_code>`_ (HMAC).
INADA Naokie2f9e772017-01-13 19:29:58 +0900496BLAKE2 can be securely used in prefix-MAC mode thanks to the
497indifferentiability property inherited from BLAKE.
498
499This example shows how to get a (hex-encoded) 128-bit authentication code for
500message ``b'message data'`` with key ``b'pseudorandom key'``::
501
502 >>> from hashlib import blake2b
503 >>> h = blake2b(key=b'pseudorandom key', digest_size=16)
504 >>> h.update(b'message data')
505 >>> h.hexdigest()
506 '3d363ff7401e02026f4a4687d4863ced'
507
508
509As a practical example, a web application can symmetrically sign cookies sent
510to users and later verify them to make sure they weren't tampered with::
511
512 >>> from hashlib import blake2b
513 >>> from hmac import compare_digest
514 >>>
515 >>> SECRET_KEY = b'pseudorandomly generated server secret key'
516 >>> AUTH_SIZE = 16
517 >>>
518 >>> def sign(cookie):
sww312ffea2017-09-13 23:24:36 -0700519 ... h = blake2b(digest_size=AUTH_SIZE, key=SECRET_KEY)
520 ... h.update(cookie)
521 ... return h.hexdigest().encode('utf-8')
INADA Naokie2f9e772017-01-13 19:29:58 +0900522 >>>
Dmitry Chestnykhaecc08a2017-09-23 19:18:40 +0200523 >>> def verify(cookie, sig):
524 ... good_sig = sign(cookie)
525 ... return compare_digest(good_sig, sig)
526 >>>
527 >>> cookie = b'user-alice'
INADA Naokie2f9e772017-01-13 19:29:58 +0900528 >>> sig = sign(cookie)
529 >>> print("{0},{1}".format(cookie.decode('utf-8'), sig))
Dmitry Chestnykhaecc08a2017-09-23 19:18:40 +0200530 user-alice,b'43b3c982cf697e0c5ab22172d1ca7421'
531 >>> verify(cookie, sig)
INADA Naokie2f9e772017-01-13 19:29:58 +0900532 True
Dmitry Chestnykhaecc08a2017-09-23 19:18:40 +0200533 >>> verify(b'user-bob', sig)
INADA Naokie2f9e772017-01-13 19:29:58 +0900534 False
Dmitry Chestnykhaecc08a2017-09-23 19:18:40 +0200535 >>> verify(cookie, b'0102030405060708090a0b0c0d0e0f00')
INADA Naokie2f9e772017-01-13 19:29:58 +0900536 False
537
538Even though there's a native keyed hashing mode, BLAKE2 can, of course, be used
539in HMAC construction with :mod:`hmac` module::
540
541 >>> import hmac, hashlib
542 >>> m = hmac.new(b'secret key', digestmod=hashlib.blake2s)
543 >>> m.update(b'message')
544 >>> m.hexdigest()
545 'e3c8102868d28b5ff85fc35dda07329970d1a01e273c37481326fe0c861c8142'
546
547
548Randomized hashing
549""""""""""""""""""
550
551By setting *salt* parameter users can introduce randomization to the hash
552function. Randomized hashing is useful for protecting against collision attacks
553on the hash function used in digital signatures.
554
555 Randomized hashing is designed for situations where one party, the message
556 preparer, generates all or part of a message to be signed by a second
557 party, the message signer. If the message preparer is able to find
558 cryptographic hash function collisions (i.e., two messages producing the
Andrés Delfino50924392018-06-18 01:34:30 -0300559 same hash value), then they might prepare meaningful versions of the message
INADA Naokie2f9e772017-01-13 19:29:58 +0900560 that would produce the same hash value and digital signature, but with
561 different results (e.g., transferring $1,000,000 to an account, rather than
562 $10). Cryptographic hash functions have been designed with collision
563 resistance as a major goal, but the current concentration on attacking
564 cryptographic hash functions may result in a given cryptographic hash
565 function providing less collision resistance than expected. Randomized
566 hashing offers the signer additional protection by reducing the likelihood
567 that a preparer can generate two or more messages that ultimately yield the
568 same hash value during the digital signature generation process --- even if
569 it is practical to find collisions for the hash function. However, the use
570 of randomized hashing may reduce the amount of security provided by a
571 digital signature when all portions of the message are prepared
572 by the signer.
573
574 (`NIST SP-800-106 "Randomized Hashing for Digital Signatures"
Sanyam Khurana338cd832018-01-20 05:55:37 +0530575 <https://csrc.nist.gov/publications/detail/sp/800-106/final>`_)
INADA Naokie2f9e772017-01-13 19:29:58 +0900576
577In BLAKE2 the salt is processed as a one-time input to the hash function during
578initialization, rather than as an input to each compression function.
579
580.. warning::
581
582 *Salted hashing* (or just hashing) with BLAKE2 or any other general-purpose
583 cryptographic hash function, such as SHA-256, is not suitable for hashing
584 passwords. See `BLAKE2 FAQ <https://blake2.net/#qa>`_ for more
585 information.
586..
587
588 >>> import os
589 >>> from hashlib import blake2b
590 >>> msg = b'some message'
591 >>> # Calculate the first hash with a random salt.
592 >>> salt1 = os.urandom(blake2b.SALT_SIZE)
593 >>> h1 = blake2b(salt=salt1)
594 >>> h1.update(msg)
595 >>> # Calculate the second hash with a different random salt.
596 >>> salt2 = os.urandom(blake2b.SALT_SIZE)
597 >>> h2 = blake2b(salt=salt2)
598 >>> h2.update(msg)
599 >>> # The digests are different.
600 >>> h1.digest() != h2.digest()
601 True
602
603
604Personalization
605"""""""""""""""
606
607Sometimes it is useful to force hash function to produce different digests for
608the same input for different purposes. Quoting the authors of the Skein hash
609function:
610
611 We recommend that all application designers seriously consider doing this;
612 we have seen many protocols where a hash that is computed in one part of
613 the protocol can be used in an entirely different part because two hash
614 computations were done on similar or related data, and the attacker can
615 force the application to make the hash inputs the same. Personalizing each
616 hash function used in the protocol summarily stops this type of attack.
617
618 (`The Skein Hash Function Family
619 <http://www.skein-hash.info/sites/default/files/skein1.3.pdf>`_,
620 p. 21)
621
622BLAKE2 can be personalized by passing bytes to the *person* argument::
623
624 >>> from hashlib import blake2b
625 >>> FILES_HASH_PERSON = b'MyApp Files Hash'
626 >>> BLOCK_HASH_PERSON = b'MyApp Block Hash'
627 >>> h = blake2b(digest_size=32, person=FILES_HASH_PERSON)
628 >>> h.update(b'the same content')
629 >>> h.hexdigest()
630 '20d9cd024d4fb086aae819a1432dd2466de12947831b75c5a30cf2676095d3b4'
631 >>> h = blake2b(digest_size=32, person=BLOCK_HASH_PERSON)
632 >>> h.update(b'the same content')
633 >>> h.hexdigest()
634 'cf68fb5761b9c44e7878bfb2c4c9aea52264a80b75005e65619778de59f383a3'
635
636Personalization together with the keyed mode can also be used to derive different
637keys from a single one.
638
639 >>> from hashlib import blake2s
640 >>> from base64 import b64decode, b64encode
641 >>> orig_key = b64decode(b'Rm5EPJai72qcK3RGBpW3vPNfZy5OZothY+kHY6h21KM=')
642 >>> enc_key = blake2s(key=orig_key, person=b'kEncrypt').digest()
643 >>> mac_key = blake2s(key=orig_key, person=b'kMAC').digest()
644 >>> print(b64encode(enc_key).decode('utf-8'))
645 rbPb15S/Z9t+agffno5wuhB77VbRi6F9Iv2qIxU7WHw=
646 >>> print(b64encode(mac_key).decode('utf-8'))
647 G9GtHFE1YluXY1zWPlYk1e/nWfu0WSEb0KRcjhDeP/o=
648
649Tree mode
650"""""""""
651
652Here's an example of hashing a minimal tree with two leaf nodes::
653
654 10
655 / \
656 00 01
657
658This example uses 64-byte internal digests, and returns the 32-byte final
659digest::
660
661 >>> from hashlib import blake2b
662 >>>
663 >>> FANOUT = 2
664 >>> DEPTH = 2
665 >>> LEAF_SIZE = 4096
666 >>> INNER_SIZE = 64
667 >>>
668 >>> buf = bytearray(6000)
669 >>>
670 >>> # Left leaf
671 ... h00 = blake2b(buf[0:LEAF_SIZE], fanout=FANOUT, depth=DEPTH,
672 ... leaf_size=LEAF_SIZE, inner_size=INNER_SIZE,
673 ... node_offset=0, node_depth=0, last_node=False)
674 >>> # Right leaf
675 ... h01 = blake2b(buf[LEAF_SIZE:], fanout=FANOUT, depth=DEPTH,
676 ... leaf_size=LEAF_SIZE, inner_size=INNER_SIZE,
677 ... node_offset=1, node_depth=0, last_node=True)
678 >>> # Root node
679 ... h10 = blake2b(digest_size=32, fanout=FANOUT, depth=DEPTH,
680 ... leaf_size=LEAF_SIZE, inner_size=INNER_SIZE,
681 ... node_offset=0, node_depth=1, last_node=True)
682 >>> h10.update(h00.digest())
683 >>> h10.update(h01.digest())
684 >>> h10.hexdigest()
685 '3ad2a9b37c6070e374c7a8c508fe20ca86b6ed54e286e93a0318e95e881db5aa'
686
687Credits
688^^^^^^^
689
690BLAKE2_ was designed by *Jean-Philippe Aumasson*, *Samuel Neves*, *Zooko
691Wilcox-O'Hearn*, and *Christian Winnerlein* based on SHA-3_ finalist BLAKE_
692created by *Jean-Philippe Aumasson*, *Luca Henzen*, *Willi Meier*, and
693*Raphael C.-W. Phan*.
694
695It uses core algorithm from ChaCha_ cipher designed by *Daniel J. Bernstein*.
696
697The stdlib implementation is based on pyblake2_ module. It was written by
698*Dmitry Chestnykh* based on C implementation written by *Samuel Neves*. The
699documentation was copied from pyblake2_ and written by *Dmitry Chestnykh*.
700
701The C code was partly rewritten for Python by *Christian Heimes*.
702
703The following public domain dedication applies for both C hash function
704implementation, extension code, and this documentation:
705
706 To the extent possible under law, the author(s) have dedicated all copyright
707 and related and neighboring rights to this software to the public domain
708 worldwide. This software is distributed without any warranty.
709
710 You should have received a copy of the CC0 Public Domain Dedication along
711 with this software. If not, see
Sanyam Khurana1b4587a2017-12-06 22:09:33 +0530712 https://creativecommons.org/publicdomain/zero/1.0/.
INADA Naokie2f9e772017-01-13 19:29:58 +0900713
714The following people have helped with development or contributed their changes
715to the project and the public domain according to the Creative Commons Public
716Domain Dedication 1.0 Universal:
717
718* *Alexandr Sokolovskiy*
719
INADA Naokie2f9e772017-01-13 19:29:58 +0900720.. _BLAKE2: https://blake2.net
721.. _HMAC: https://en.wikipedia.org/wiki/Hash-based_message_authentication_code
722.. _BLAKE: https://131002.net/blake/
723.. _SHA-3: https://en.wikipedia.org/wiki/NIST_hash_function_competition
724.. _ChaCha: https://cr.yp.to/chacha.html
725.. _pyblake2: https://pythonhosted.org/pyblake2/
726
Christian Heimes121b9482016-09-06 22:03:25 +0200727
728
Georg Brandl116aa622007-08-15 14:28:22 +0000729.. seealso::
730
731 Module :mod:`hmac`
732 A module to generate message authentication codes using hashes.
733
734 Module :mod:`base64`
735 Another way to encode binary hashes for non-binary environments.
736
INADA Naokie2f9e772017-01-13 19:29:58 +0900737 https://blake2.net
738 Official BLAKE2 website.
Christian Heimes121b9482016-09-06 22:03:25 +0200739
Sanyam Khurana338cd832018-01-20 05:55:37 +0530740 https://csrc.nist.gov/csrc/media/publications/fips/180/2/archive/2002-08-01/documents/fips180-2.pdf
Georg Brandl116aa622007-08-15 14:28:22 +0000741 The FIPS 180-2 publication on Secure Hash Algorithms.
742
Benjamin Peterson1dd72e62015-09-27 02:05:01 -0700743 https://en.wikipedia.org/wiki/Cryptographic_hash_function#Cryptographic_hash_algorithms
Georg Brandlfd0eb3f2010-05-21 20:28:13 +0000744 Wikipedia article with information on which algorithms have known issues and
Georg Brandl116aa622007-08-15 14:28:22 +0000745 what that means regarding their use.
746
Serhiy Storchaka6dff0202016-05-07 10:49:07 +0300747 https://www.ietf.org/rfc/rfc2898.txt
Christian Heimese92ef132013-10-13 00:52:43 +0200748 PKCS #5: Password-Based Cryptography Specification Version 2.0