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