1:mod:`hashlib` --- Secure hashes and message digests
2====================================================
3
4.. module:: hashlib
5   :synopsis: Secure hash and message digest algorithms.
6
7.. moduleauthor:: Gregory P. Smith <[email protected]>
8.. sectionauthor:: Gregory P. Smith <[email protected]>
9
10**Source code:** :source:`Lib/hashlib.py`
11
12.. index::
13   single: message digest, MD5
14   single: secure hash algorithm, SHA1, SHA224, SHA256, SHA384, SHA512
15
16.. testsetup::
17
18   import hashlib
19
20
21--------------
22
23This 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
26algorithm (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.
29
30.. note::
31
32   If you want the adler32 or crc32 hash functions, they are available in
33   the :mod:`zlib` module.
34
35.. warning::
36
37   Some algorithms have known hash collision weaknesses, refer to the "See
38   also" section at the end.
39
40
41.. _hash-algorithms:
42
43Hash algorithms
44---------------
45
46There is one constructor method named for each type of :dfn:`hash`.  All return
47a 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
49objects <bytes-like object>` (normally :class:`bytes`) using the :meth:`update` method.
50At any point you can ask it for the :dfn:`digest` of the
51concatenation of the data fed to it so far using the :meth:`digest` or
52:meth:`hexdigest` methods.
53
54.. note::
55
56   For better multithreading performance, the Python :term:`GIL` is released for
57   data larger than 2047 bytes at object creation or on update.
58
59.. note::
60
61   Feeding string objects into :meth:`update` is not supported, as hashes work
62   on bytes, not on characters.
63
64.. index:: single: OpenSSL; (use in module hashlib)
65
66Constructors for hash algorithms that are always present in this module are
67:func:`sha1`, :func:`sha224`, :func:`sha256`, :func:`sha384`,
68:func:`sha512`, :func:`blake2b`, and :func:`blake2s`.
69:func:`md5` is normally available as well, though it
70may be missing or blocked if you are using a rare "FIPS compliant" build of Python.
71Additional algorithms may also be available depending upon the OpenSSL
72library 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`.
79
80.. versionadded:: 3.6
81   :func:`blake2b` and :func:`blake2s` were added.
82
83.. _hashlib-usedforsecurity:
84
85.. versionchanged:: 3.9
86   All hashlib constructors take a keyword-only argument *usedforsecurity*
87   with default value ``True``. A false value allows the use of insecure and
88   blocked hashing algorithms in restricted environments. ``False`` indicates
89   that the hashing algorithm is not used in a security context, e.g. as a
90   non-cryptographic one-way compression function.
91
92   Hashlib now uses SHA3 and SHAKE from OpenSSL 1.1.1 and newer.
93
94For example, to obtain the digest of the byte string ``b"Nobody inspects the
95spammish repetition"``::
96
97   >>> import hashlib
98   >>> m = hashlib.sha256()
99   >>> m.update(b"Nobody inspects")
100   >>> m.update(b" the spammish repetition")
101   >>> m.digest()
102   b'\x03\x1e\xdd}Ae\x15\x93\xc5\xfe\\\x00o\xa5u+7\xfd\xdf\xf7\xbcN\x84:\xa6\xaf\x0c\x95\x0fK\x94\x06'
103   >>> m.hexdigest()
104   '031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406'
105
106More condensed:
107
108   >>> hashlib.sha256(b"Nobody inspects the spammish repetition").hexdigest()
109   '031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406'
110
111.. function:: new(name[, data], *, usedforsecurity=True)
112
113   Is a generic constructor that takes the string *name* of the desired
114   algorithm as its first parameter.  It also exists to allow access to the
115   above listed hashes as well as any other algorithms that your OpenSSL
116   library may offer.  The named constructors are much faster than :func:`new`
117   and should be preferred.
118
119Using :func:`new` with an algorithm provided by OpenSSL:
120
121   >>> h = hashlib.new('sha256')
122   >>> h.update(b"Nobody inspects the spammish repetition")
123   >>> h.hexdigest()
124   '031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406'
125
126Hashlib provides the following constant attributes:
127
128.. data:: algorithms_guaranteed
129
130   A set containing the names of the hash algorithms guaranteed to be supported
131   by this module on all platforms.  Note that 'md5' is in this list despite
132   some upstream vendors offering an odd "FIPS compliant" Python build that
133   excludes it.
134
135   .. versionadded:: 3.2
136
137.. data:: algorithms_available
138
139   A set containing the names of the hash algorithms that are available in the
140   running Python interpreter.  These names will be recognized when passed to
141   :func:`new`.  :attr:`algorithms_guaranteed` will always be a subset.  The
142   same algorithm may appear multiple times in this set under different names
143   (thanks to OpenSSL).
144
145   .. versionadded:: 3.2
146
147The following values are provided as constant attributes of the hash objects
148returned by the constructors:
149
150
151.. data:: hash.digest_size
152
153   The size of the resulting hash in bytes.
154
155.. data:: hash.block_size
156
157   The internal block size of the hash algorithm in bytes.
158
159A hash object has the following attributes:
160
161.. attribute:: hash.name
162
163   The canonical name of this hash, always lowercase and always suitable as a
164   parameter to :func:`new` to create another hash of this type.
165
166   .. versionchanged:: 3.4
167      The name attribute has been present in CPython since its inception, but
168      until Python 3.4 was not formally specified, so may not exist on some
169      platforms.
170
171A hash object has the following methods:
172
173
174.. method:: hash.update(data)
175
176   Update the hash object with the :term:`bytes-like object`.
177   Repeated calls are equivalent to a single call with the
178   concatenation of all the arguments: ``m.update(a); m.update(b)`` is
179   equivalent to ``m.update(a+b)``.
180
181   .. versionchanged:: 3.1
182      The Python GIL is released to allow other threads to run while hash
183      updates on data larger than 2047 bytes is taking place when using hash
184      algorithms supplied by OpenSSL.
185
186
187.. method:: hash.digest()
188
189   Return the digest of the data passed to the :meth:`update` method so far.
190   This is a bytes object of size :attr:`digest_size` which may contain bytes in
191   the whole range from 0 to 255.
192
193
194.. method:: hash.hexdigest()
195
196   Like :meth:`digest` except the digest is returned as a string object of
197   double length, containing only hexadecimal digits.  This may be used to
198   exchange the value safely in email or other non-binary environments.
199
200
201.. method:: hash.copy()
202
203   Return a copy ("clone") of the hash object.  This can be used to efficiently
204   compute the digests of data sharing a common initial substring.
205
206
207SHAKE variable length digests
208-----------------------------
209
210The :func:`shake_128` and :func:`shake_256` algorithms provide variable
211length digests with length_in_bits//2 up to 128 or 256 bits of security.
212As such, their digest methods require a length. Maximum length is not limited
213by the SHAKE algorithm.
214
215.. method:: shake.digest(length)
216
217   Return the digest of the data passed to the :meth:`update` method so far.
218   This is a bytes object of size *length* which may contain bytes in
219   the whole range from 0 to 255.
220
221
222.. method:: shake.hexdigest(length)
223
224   Like :meth:`digest` except the digest is returned as a string object of
225   double length, containing only hexadecimal digits.  This may be used to
226   exchange the value safely in email or other non-binary environments.
227
228
229File hashing
230------------
231
232The hashlib module provides a helper function for efficient hashing of
233a file or file-like object.
234
235.. function:: file_digest(fileobj, digest, /)
236
237   Return a digest object that has been updated with contents of file object.
238
239   *fileobj* must be a file-like object opened for reading in binary mode.
240   It accepts file objects from  builtin :func:`open`, :class:`~io.BytesIO`
241   instances, SocketIO objects from :meth:`socket.socket.makefile`, and
242   similar. The function may bypass Python's I/O and use the file descriptor
243   from :meth:`~io.IOBase.fileno` directly. *fileobj* must be assumed to be
244   in an unknown state after this function returns or raises. It is up to
245   the caller to close *fileobj*.
246
247   *digest* must either be a hash algorithm name as a *str*, a hash
248   constructor, or a callable that returns a hash object.
249
250   Example:
251
252      >>> import io, hashlib, hmac
253      >>> with open(hashlib.__file__, "rb") as f:
254      ...     digest = hashlib.file_digest(f, "sha256")
255      ...
256      >>> digest.hexdigest()  # doctest: +ELLIPSIS
257      '...'
258
259      >>> buf = io.BytesIO(b"somedata")
260      >>> mac1 = hmac.HMAC(b"key", digestmod=hashlib.sha512)
261      >>> digest = hashlib.file_digest(buf, lambda: mac1)
262
263      >>> digest is mac1
264      True
265      >>> mac2 = hmac.HMAC(b"key", b"somedata", digestmod=hashlib.sha512)
266      >>> mac1.digest() == mac2.digest()
267      True
268
269   .. versionadded:: 3.11
270
271
272Key derivation
273--------------
274
275Key derivation and key stretching algorithms are designed for secure password
276hashing. Naive algorithms such as ``sha1(password)`` are not resistant against
277brute-force attacks. A good password hashing function must be tunable, slow, and
278include a `salt <https://en.wikipedia.org/wiki/Salt_%28cryptography%29>`_.
279
280
281.. function:: pbkdf2_hmac(hash_name, password, salt, iterations, dklen=None)
282
283   The function provides PKCS#5 password-based key derivation function 2. It
284   uses HMAC as pseudorandom function.
285
286   The string *hash_name* is the desired name of the hash digest algorithm for
287   HMAC, e.g. 'sha1' or 'sha256'. *password* and *salt* are interpreted as
288   buffers of bytes. Applications and libraries should limit *password* to
289   a sensible length (e.g. 1024). *salt* should be about 16 or more bytes from
290   a proper source, e.g. :func:`os.urandom`.
291
292   The number of *iterations* should be chosen based on the hash algorithm and
293   computing power. As of 2022, hundreds of thousands of iterations of SHA-256
294   are suggested. For rationale as to why and how to choose what is best for
295   your application, read *Appendix A.2.2* of NIST-SP-800-132_. The answers
296   on the `stackexchange pbkdf2 iterations question`_ explain in detail.
297
298   *dklen* is the length of the derived key. If *dklen* is ``None`` then the
299   digest size of the hash algorithm *hash_name* is used, e.g. 64 for SHA-512.
300
301   >>> from hashlib import pbkdf2_hmac
302   >>> our_app_iters = 500_000  # Application specific, read above.
303   >>> dk = pbkdf2_hmac('sha256', b'password', b'bad salt'*2, our_app_iters)
304   >>> dk.hex()
305   '15530bba69924174860db778f2c6f8104d3aaf9d26241840c8c4a641c8d000a9'
306
307   .. versionadded:: 3.4
308
309   .. note::
310
311      A fast implementation of *pbkdf2_hmac* is available with OpenSSL.  The
312      Python implementation uses an inline version of :mod:`hmac`. It is about
313      three times slower and doesn't release the GIL.
314
315   .. deprecated:: 3.10
316
317      Slow Python implementation of *pbkdf2_hmac* is deprecated. In the
318      future the function will only be available when Python is compiled
319      with OpenSSL.
320
321.. function:: scrypt(password, *, salt, n, r, p, maxmem=0, dklen=64)
322
323   The function provides scrypt password-based key derivation function as
324   defined in :rfc:`7914`.
325
326   *password* and *salt* must be :term:`bytes-like objects
327   <bytes-like object>`.  Applications and libraries should limit *password*
328   to a sensible length (e.g. 1024).  *salt* should be about 16 or more
329   bytes from a proper source, e.g. :func:`os.urandom`.
330
331   *n* is the CPU/Memory cost factor, *r* the block size, *p* parallelization
332   factor and *maxmem* limits memory (OpenSSL 1.1.0 defaults to 32 MiB).
333   *dklen* is the length of the derived key.
334
335   .. versionadded:: 3.6
336
337
338BLAKE2
339------
340
341.. sectionauthor:: Dmitry Chestnykh
342
343.. index::
344   single: blake2b, blake2s
345
346BLAKE2_ is a cryptographic hash function defined in :rfc:`7693` that comes in two
347flavors:
348
349* **BLAKE2b**, optimized for 64-bit platforms and produces digests of any size
350  between 1 and 64 bytes,
351
352* **BLAKE2s**, optimized for 8- to 32-bit platforms and produces digests of any
353  size between 1 and 32 bytes.
354
355BLAKE2 supports **keyed mode** (a faster and simpler replacement for HMAC_),
356**salted hashing**, **personalization**, and **tree hashing**.
357
358Hash objects from this module follow the API of standard library's
359:mod:`hashlib` objects.
360
361
362Creating hash objects
363^^^^^^^^^^^^^^^^^^^^^
364
365New hash objects are created by calling constructor functions:
366
367
368.. function:: blake2b(data=b'', *, digest_size=64, key=b'', salt=b'', \
369                person=b'', fanout=1, depth=1, leaf_size=0, node_offset=0,  \
370                node_depth=0, inner_size=0, last_node=False, \
371                usedforsecurity=True)
372
373.. function:: blake2s(data=b'', *, digest_size=32, key=b'', salt=b'', \
374                person=b'', fanout=1, depth=1, leaf_size=0, node_offset=0,  \
375                node_depth=0, inner_size=0, last_node=False, \
376                usedforsecurity=True)
377
378
379These functions return the corresponding hash objects for calculating
380BLAKE2b or BLAKE2s. They optionally take these general parameters:
381
382* *data*: initial chunk of data to hash, which must be
383  :term:`bytes-like object`.  It can be passed only as positional argument.
384
385* *digest_size*: size of output digest in bytes.
386
387* *key*: key for keyed hashing (up to 64 bytes for BLAKE2b, up to 32 bytes for
388  BLAKE2s).
389
390* *salt*: salt for randomized hashing (up to 16 bytes for BLAKE2b, up to 8
391  bytes for BLAKE2s).
392
393* *person*: personalization string (up to 16 bytes for BLAKE2b, up to 8 bytes
394  for BLAKE2s).
395
396The following table shows limits for general parameters (in bytes):
397
398======= =========== ======== ========= ===========
399Hash    digest_size len(key) len(salt) len(person)
400======= =========== ======== ========= ===========
401BLAKE2b     64         64       16        16
402BLAKE2s     32         32       8         8
403======= =========== ======== ========= ===========
404
405.. note::
406
407    BLAKE2 specification defines constant lengths for salt and personalization
408    parameters, however, for convenience, this implementation accepts byte
409    strings of any size up to the specified length. If the length of the
410    parameter is less than specified, it is padded with zeros, thus, for
411    example, ``b'salt'`` and ``b'salt\x00'`` is the same value. (This is not
412    the case for *key*.)
413
414These sizes are available as module `constants`_ described below.
415
416Constructor functions also accept the following tree hashing parameters:
417
418* *fanout*: fanout (0 to 255, 0 if unlimited, 1 in sequential mode).
419
420* *depth*: maximal depth of tree (1 to 255, 255 if unlimited, 1 in
421  sequential mode).
422
423* *leaf_size*: maximal byte length of leaf (0 to ``2**32-1``, 0 if unlimited or in
424  sequential mode).
425
426* *node_offset*: node offset (0 to ``2**64-1`` for BLAKE2b, 0 to ``2**48-1`` for
427  BLAKE2s, 0 for the first, leftmost, leaf, or in sequential mode).
428
429* *node_depth*: node depth (0 to 255, 0 for leaves, or in sequential mode).
430
431* *inner_size*: inner digest size (0 to 64 for BLAKE2b, 0 to 32 for
432  BLAKE2s, 0 in sequential mode).
433
434* *last_node*: boolean indicating whether the processed node is the last
435  one (``False`` for sequential mode).
436
437.. figure:: hashlib-blake2-tree.png
438   :alt: Explanation of tree mode parameters.
439   :class: invert-in-dark-mode
440
441See section 2.10 in `BLAKE2 specification
442<https://www.blake2.net/blake2_20130129.pdf>`_ for comprehensive review of tree
443hashing.
444
445
446Constants
447^^^^^^^^^
448
449.. data:: blake2b.SALT_SIZE
450.. data:: blake2s.SALT_SIZE
451
452Salt length (maximum length accepted by constructors).
453
454
455.. data:: blake2b.PERSON_SIZE
456.. data:: blake2s.PERSON_SIZE
457
458Personalization string length (maximum length accepted by constructors).
459
460
461.. data:: blake2b.MAX_KEY_SIZE
462.. data:: blake2s.MAX_KEY_SIZE
463
464Maximum key size.
465
466
467.. data:: blake2b.MAX_DIGEST_SIZE
468.. data:: blake2s.MAX_DIGEST_SIZE
469
470Maximum digest size that the hash function can output.
471
472
473Examples
474^^^^^^^^
475
476Simple hashing
477""""""""""""""
478
479To calculate hash of some data, you should first construct a hash object by
480calling the appropriate constructor function (:func:`blake2b` or
481:func:`blake2s`), then update it with the data by calling :meth:`update` on the
482object, and, finally, get the digest out of the object by calling
483:meth:`digest` (or :meth:`hexdigest` for hex-encoded string).
484
485    >>> from hashlib import blake2b
486    >>> h = blake2b()
487    >>> h.update(b'Hello world')
488    >>> h.hexdigest()
489    '6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'
490
491
492As a shortcut, you can pass the first chunk of data to update directly to the
493constructor as the positional argument:
494
495    >>> from hashlib import blake2b
496    >>> blake2b(b'Hello world').hexdigest()
497    '6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'
498
499You can call :meth:`hash.update` as many times as you need to iteratively
500update the hash:
501
502    >>> from hashlib import blake2b
503    >>> items = [b'Hello', b' ', b'world']
504    >>> h = blake2b()
505    >>> for item in items:
506    ...     h.update(item)
507    >>> h.hexdigest()
508    '6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'
509
510
511Using different digest sizes
512""""""""""""""""""""""""""""
513
514BLAKE2 has configurable size of digests up to 64 bytes for BLAKE2b and up to 32
515bytes for BLAKE2s. For example, to replace SHA-1 with BLAKE2b without changing
516the size of output, we can tell BLAKE2b to produce 20-byte digests:
517
518    >>> from hashlib import blake2b
519    >>> h = blake2b(digest_size=20)
520    >>> h.update(b'Replacing SHA1 with the more secure function')
521    >>> h.hexdigest()
522    'd24f26cf8de66472d58d4e1b1774b4c9158b1f4c'
523    >>> h.digest_size
524    20
525    >>> len(h.digest())
526    20
527
528Hash objects with different digest sizes have completely different outputs
529(shorter hashes are *not* prefixes of longer hashes); BLAKE2b and BLAKE2s
530produce different outputs even if the output length is the same:
531
532    >>> from hashlib import blake2b, blake2s
533    >>> blake2b(digest_size=10).hexdigest()
534    '6fa1d8fcfd719046d762'
535    >>> blake2b(digest_size=11).hexdigest()
536    'eb6ec15daf9546254f0809'
537    >>> blake2s(digest_size=10).hexdigest()
538    '1bf21a98c78a1c376ae9'
539    >>> blake2s(digest_size=11).hexdigest()
540    '567004bf96e4a25773ebf4'
541
542
543Keyed hashing
544"""""""""""""
545
546Keyed hashing can be used for authentication as a faster and simpler
547replacement for `Hash-based message authentication code
548<https://en.wikipedia.org/wiki/HMAC>`_ (HMAC).
549BLAKE2 can be securely used in prefix-MAC mode thanks to the
550indifferentiability property inherited from BLAKE.
551
552This example shows how to get a (hex-encoded) 128-bit authentication code for
553message ``b'message data'`` with key ``b'pseudorandom key'``::
554
555    >>> from hashlib import blake2b
556    >>> h = blake2b(key=b'pseudorandom key', digest_size=16)
557    >>> h.update(b'message data')
558    >>> h.hexdigest()
559    '3d363ff7401e02026f4a4687d4863ced'
560
561
562As a practical example, a web application can symmetrically sign cookies sent
563to users and later verify them to make sure they weren't tampered with::
564
565    >>> from hashlib import blake2b
566    >>> from hmac import compare_digest
567    >>>
568    >>> SECRET_KEY = b'pseudorandomly generated server secret key'
569    >>> AUTH_SIZE = 16
570    >>>
571    >>> def sign(cookie):
572    ...     h = blake2b(digest_size=AUTH_SIZE, key=SECRET_KEY)
573    ...     h.update(cookie)
574    ...     return h.hexdigest().encode('utf-8')
575    >>>
576    >>> def verify(cookie, sig):
577    ...     good_sig = sign(cookie)
578    ...     return compare_digest(good_sig, sig)
579    >>>
580    >>> cookie = b'user-alice'
581    >>> sig = sign(cookie)
582    >>> print("{0},{1}".format(cookie.decode('utf-8'), sig))
583    user-alice,b'43b3c982cf697e0c5ab22172d1ca7421'
584    >>> verify(cookie, sig)
585    True
586    >>> verify(b'user-bob', sig)
587    False
588    >>> verify(cookie, b'0102030405060708090a0b0c0d0e0f00')
589    False
590
591Even though there's a native keyed hashing mode, BLAKE2 can, of course, be used
592in HMAC construction with :mod:`hmac` module::
593
594    >>> import hmac, hashlib
595    >>> m = hmac.new(b'secret key', digestmod=hashlib.blake2s)
596    >>> m.update(b'message')
597    >>> m.hexdigest()
598    'e3c8102868d28b5ff85fc35dda07329970d1a01e273c37481326fe0c861c8142'
599
600
601Randomized hashing
602""""""""""""""""""
603
604By setting *salt* parameter users can introduce randomization to the hash
605function. Randomized hashing is useful for protecting against collision attacks
606on the hash function used in digital signatures.
607
608    Randomized hashing is designed for situations where one party, the message
609    preparer, generates all or part of a message to be signed by a second
610    party, the message signer. If the message preparer is able to find
611    cryptographic hash function collisions (i.e., two messages producing the
612    same hash value), then they might prepare meaningful versions of the message
613    that would produce the same hash value and digital signature, but with
614    different results (e.g., transferring $1,000,000 to an account, rather than
615    $10). Cryptographic hash functions have been designed with collision
616    resistance as a major goal, but the current concentration on attacking
617    cryptographic hash functions may result in a given cryptographic hash
618    function providing less collision resistance than expected. Randomized
619    hashing offers the signer additional protection by reducing the likelihood
620    that a preparer can generate two or more messages that ultimately yield the
621    same hash value during the digital signature generation process --- even if
622    it is practical to find collisions for the hash function. However, the use
623    of randomized hashing may reduce the amount of security provided by a
624    digital signature when all portions of the message are prepared
625    by the signer.
626
627    (`NIST SP-800-106 "Randomized Hashing for Digital Signatures"
628    <https://csrc.nist.gov/publications/detail/sp/800-106/archive/2009-02-25>`_)
629
630In BLAKE2 the salt is processed as a one-time input to the hash function during
631initialization, rather than as an input to each compression function.
632
633.. warning::
634
635    *Salted hashing* (or just hashing) with BLAKE2 or any other general-purpose
636    cryptographic hash function, such as SHA-256, is not suitable for hashing
637    passwords.  See `BLAKE2 FAQ <https://www.blake2.net/#qa>`_ for more
638    information.
639..
640
641    >>> import os
642    >>> from hashlib import blake2b
643    >>> msg = b'some message'
644    >>> # Calculate the first hash with a random salt.
645    >>> salt1 = os.urandom(blake2b.SALT_SIZE)
646    >>> h1 = blake2b(salt=salt1)
647    >>> h1.update(msg)
648    >>> # Calculate the second hash with a different random salt.
649    >>> salt2 = os.urandom(blake2b.SALT_SIZE)
650    >>> h2 = blake2b(salt=salt2)
651    >>> h2.update(msg)
652    >>> # The digests are different.
653    >>> h1.digest() != h2.digest()
654    True
655
656
657Personalization
658"""""""""""""""
659
660Sometimes it is useful to force hash function to produce different digests for
661the same input for different purposes. Quoting the authors of the Skein hash
662function:
663
664    We recommend that all application designers seriously consider doing this;
665    we have seen many protocols where a hash that is computed in one part of
666    the protocol can be used in an entirely different part because two hash
667    computations were done on similar or related data, and the attacker can
668    force the application to make the hash inputs the same. Personalizing each
669    hash function used in the protocol summarily stops this type of attack.
670
671    (`The Skein Hash Function Family
672    <https://www.schneier.com/wp-content/uploads/2016/02/skein.pdf>`_,
673    p. 21)
674
675BLAKE2 can be personalized by passing bytes to the *person* argument::
676
677    >>> from hashlib import blake2b
678    >>> FILES_HASH_PERSON = b'MyApp Files Hash'
679    >>> BLOCK_HASH_PERSON = b'MyApp Block Hash'
680    >>> h = blake2b(digest_size=32, person=FILES_HASH_PERSON)
681    >>> h.update(b'the same content')
682    >>> h.hexdigest()
683    '20d9cd024d4fb086aae819a1432dd2466de12947831b75c5a30cf2676095d3b4'
684    >>> h = blake2b(digest_size=32, person=BLOCK_HASH_PERSON)
685    >>> h.update(b'the same content')
686    >>> h.hexdigest()
687    'cf68fb5761b9c44e7878bfb2c4c9aea52264a80b75005e65619778de59f383a3'
688
689Personalization together with the keyed mode can also be used to derive different
690keys from a single one.
691
692    >>> from hashlib import blake2s
693    >>> from base64 import b64decode, b64encode
694    >>> orig_key = b64decode(b'Rm5EPJai72qcK3RGBpW3vPNfZy5OZothY+kHY6h21KM=')
695    >>> enc_key = blake2s(key=orig_key, person=b'kEncrypt').digest()
696    >>> mac_key = blake2s(key=orig_key, person=b'kMAC').digest()
697    >>> print(b64encode(enc_key).decode('utf-8'))
698    rbPb15S/Z9t+agffno5wuhB77VbRi6F9Iv2qIxU7WHw=
699    >>> print(b64encode(mac_key).decode('utf-8'))
700    G9GtHFE1YluXY1zWPlYk1e/nWfu0WSEb0KRcjhDeP/o=
701
702Tree mode
703"""""""""
704
705Here's an example of hashing a minimal tree with two leaf nodes::
706
707       10
708      /  \
709     00  01
710
711This example uses 64-byte internal digests, and returns the 32-byte final
712digest::
713
714    >>> from hashlib import blake2b
715    >>>
716    >>> FANOUT = 2
717    >>> DEPTH = 2
718    >>> LEAF_SIZE = 4096
719    >>> INNER_SIZE = 64
720    >>>
721    >>> buf = bytearray(6000)
722    >>>
723    >>> # Left leaf
724    ... h00 = blake2b(buf[0:LEAF_SIZE], fanout=FANOUT, depth=DEPTH,
725    ...               leaf_size=LEAF_SIZE, inner_size=INNER_SIZE,
726    ...               node_offset=0, node_depth=0, last_node=False)
727    >>> # Right leaf
728    ... h01 = blake2b(buf[LEAF_SIZE:], fanout=FANOUT, depth=DEPTH,
729    ...               leaf_size=LEAF_SIZE, inner_size=INNER_SIZE,
730    ...               node_offset=1, node_depth=0, last_node=True)
731    >>> # Root node
732    ... h10 = blake2b(digest_size=32, fanout=FANOUT, depth=DEPTH,
733    ...               leaf_size=LEAF_SIZE, inner_size=INNER_SIZE,
734    ...               node_offset=0, node_depth=1, last_node=True)
735    >>> h10.update(h00.digest())
736    >>> h10.update(h01.digest())
737    >>> h10.hexdigest()
738    '3ad2a9b37c6070e374c7a8c508fe20ca86b6ed54e286e93a0318e95e881db5aa'
739
740Credits
741^^^^^^^
742
743BLAKE2_ was designed by *Jean-Philippe Aumasson*, *Samuel Neves*, *Zooko
744Wilcox-O'Hearn*, and *Christian Winnerlein* based on SHA-3_ finalist BLAKE_
745created by *Jean-Philippe Aumasson*, *Luca Henzen*, *Willi Meier*, and
746*Raphael C.-W. Phan*.
747
748It uses core algorithm from ChaCha_ cipher designed by *Daniel J.  Bernstein*.
749
750The stdlib implementation is based on pyblake2_ module. It was written by
751*Dmitry Chestnykh* based on C implementation written by *Samuel Neves*. The
752documentation was copied from pyblake2_ and written by *Dmitry Chestnykh*.
753
754The C code was partly rewritten for Python by *Christian Heimes*.
755
756The following public domain dedication applies for both C hash function
757implementation, extension code, and this documentation:
758
759   To the extent possible under law, the author(s) have dedicated all copyright
760   and related and neighboring rights to this software to the public domain
761   worldwide. This software is distributed without any warranty.
762
763   You should have received a copy of the CC0 Public Domain Dedication along
764   with this software. If not, see
765   https://creativecommons.org/publicdomain/zero/1.0/.
766
767The following people have helped with development or contributed their changes
768to the project and the public domain according to the Creative Commons Public
769Domain Dedication 1.0 Universal:
770
771* *Alexandr Sokolovskiy*
772
773.. _BLAKE2: https://www.blake2.net
774.. _HMAC: https://en.wikipedia.org/wiki/Hash-based_message_authentication_code
775.. _BLAKE: https://web.archive.org/web/20200918190133/https://131002.net/blake/
776.. _SHA-3: https://en.wikipedia.org/wiki/NIST_hash_function_competition
777.. _ChaCha: https://cr.yp.to/chacha.html
778.. _pyblake2: https://pythonhosted.org/pyblake2/
779.. _NIST-SP-800-132: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf
780.. _stackexchange pbkdf2 iterations question: https://security.stackexchange.com/questions/3959/recommended-of-iterations-when-using-pbkdf2-sha256/
781
782
783.. seealso::
784
785   Module :mod:`hmac`
786      A module to generate message authentication codes using hashes.
787
788   Module :mod:`base64`
789      Another way to encode binary hashes for non-binary environments.
790
791   https://www.blake2.net
792      Official BLAKE2 website.
793
794   https://csrc.nist.gov/csrc/media/publications/fips/180/2/archive/2002-08-01/documents/fips180-2.pdf
795      The FIPS 180-2 publication on Secure Hash Algorithms.
796
797   https://en.wikipedia.org/wiki/Cryptographic_hash_function#Cryptographic_hash_algorithms
798      Wikipedia article with information on which algorithms have known issues and
799      what that means regarding their use.
800
801   https://www.ietf.org/rfc/rfc8018.txt
802      PKCS #5: Password-Based Cryptography Specification Version 2.1
803
804   https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf
805      NIST Recommendation for Password-Based Key Derivation.
806