Skip to content

Commit e2bbe62

Browse files
[3.12] Cleanup and clarify our hashlib docs. (GH-105624) (#105632)
Cleanup and clarify our hashlib docs. (GH-105624) Clarify and improve our hashlib docs. Now with 50% less mess! (cherry picked from commit 0d1d6ab) Co-authored-by: Gregory P. Smith <[email protected]>
1 parent 8c4cf96 commit e2bbe62

File tree

1 file changed

+90
-47
lines changed

1 file changed

+90
-47
lines changed

Doc/library/hashlib.rst

+90-47
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
.. index::
1313
single: message digest, MD5
14-
single: secure hash algorithm, SHA1, SHA224, SHA256, SHA384, SHA512
14+
single: secure hash algorithm, SHA1, SHA2, SHA224, SHA256, SHA384, SHA512, SHA3, Shake, Blake2
1515

1616
.. testsetup::
1717

@@ -22,7 +22,8 @@
2222

2323
This module implements a common interface to many different secure hash and
2424
message digest algorithms. Included are the FIPS secure hash algorithms SHA1,
25-
SHA224, SHA256, SHA384, and SHA512 (defined in FIPS 180-2) as well as RSA's MD5
25+
SHA224, SHA256, SHA384, SHA512, (defined in `the FIPS 180-4 standard`_),
26+
the SHA-3 series (defined in `the FIPS 202 standard`_) as well as RSA's MD5
2627
algorithm (defined in internet :rfc:`1321`). The terms "secure hash" and
2728
"message digest" are interchangeable. Older algorithms were called message
2829
digests. The modern term is secure hash.
@@ -32,50 +33,50 @@ digests. The modern term is secure hash.
3233
If you want the adler32 or crc32 hash functions, they are available in
3334
the :mod:`zlib` module.
3435

35-
.. warning::
36-
37-
Some algorithms have known hash collision weaknesses, refer to the "See
38-
also" section at the end.
39-
4036

4137
.. _hash-algorithms:
4238

4339
Hash algorithms
4440
---------------
4541

4642
There is one constructor method named for each type of :dfn:`hash`. All return
47-
a hash object with the same simple interface. For example: use :func:`sha256` to
48-
create a SHA-256 hash object. You can now feed this object with :term:`bytes-like
49-
objects <bytes-like object>` (normally :class:`bytes`) using the :meth:`update` method.
50-
At any point you can ask it for the :dfn:`digest` of the
51-
concatenation 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.
43+
a hash object with the same simple interface. For example: use :func:`sha256`
44+
to 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
46+
the :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.
5849

59-
.. note::
50+
To allow multithreading, the Python :term:`GIL` is released while computing a
51+
hash supplied more than 2047 bytes of data at once in its constructor or
52+
:meth:`.update<hash.update>` method.
6053

61-
Feeding string objects into :meth:`update` is not supported, as hashes work
62-
on bytes, not on characters.
6354

6455
.. index:: single: OpenSSL; (use in module hashlib)
6556

6657
Constructors 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
70-
may be missing or blocked if you are using a rare "FIPS compliant" build of Python.
71-
Additional algorithms may also be available depending upon the OpenSSL
72-
library that Python uses on your platform. On most platforms the
58+
:func:`sha1`, :func:`sha224`, :func:`sha256`, :func:`sha384`, :func:`sha512`,
7359
:func:`sha3_224`, :func:`sha3_256`, :func:`sha3_384`, :func:`sha3_512`,
74-
:func:`shake_128`, :func:`shake_256` are also available.
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
62+
if you are using a rare "FIPS compliant" build of Python.
63+
These correspond to :data:`algorithms_guaranteed`.
64+
65+
Additional algorithms may also be available if your Python distribution's
66+
:mod:`hashlib` was linked against a build of OpenSSL that provides others.
67+
Others *are not guaranteed available* on all installations and will only be
68+
accessible 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.
7575

7676
.. versionadded:: 3.6
7777
SHA3 (Keccak) and SHAKE constructors :func:`sha3_224`, :func:`sha3_256`,
78-
:func:`sha3_384`, :func:`sha3_512`, :func:`shake_128`, :func:`shake_256`.
78+
:func:`sha3_384`, :func:`sha3_512`, :func:`shake_128`, :func:`shake_256`
79+
were added.
7980

8081
.. versionadded:: 3.6
8182
:func:`blake2b` and :func:`blake2s` were added.
@@ -89,10 +90,14 @@ library that Python uses on your platform. On most platforms the
8990
that the hashing algorithm is not used in a security context, e.g. as a
9091
non-cryptographic one-way compression function.
9192

92-
Hashlib now uses SHA3 and SHAKE from OpenSSL 1.1.1 and newer.
93+
.. versionchanged:: 3.9
94+
Hashlib now uses SHA3 and SHAKE from OpenSSL if it provides it.
9395

94-
For example, to obtain the digest of the byte string ``b"Nobody inspects the
95-
spammish repetition"``::
96+
Usage
97+
-----
98+
99+
To obtain the digest of the byte string ``b"Nobody inspects the spammish
100+
repetition"``::
96101

97102
>>> import hashlib
98103
>>> m = hashlib.sha256()
@@ -108,22 +113,42 @@ More condensed:
108113
>>> hashlib.sha256(b"Nobody inspects the spammish repetition").hexdigest()
109114
'031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406'
110115

111-
.. function:: new(name[, data], *, usedforsecurity=True)
116+
Constructors
117+
------------
118+
119+
.. function:: new(name[, data], \*, usedforsecurity=True)
112120

113121
Is a generic constructor that takes the string *name* of the desired
114122
algorithm as its first parameter. It also exists to allow access to the
115123
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.
124+
library may offer.
118125

119-
Using :func:`new` with an algorithm provided by OpenSSL:
126+
Using :func:`new` with an algorithm name:
120127

121128
>>> h = hashlib.new('sha256')
122129
>>> h.update(b"Nobody inspects the spammish repetition")
123130
>>> h.hexdigest()
124131
'031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406'
125132

126-
Hashlib provides the following constant attributes:
133+
134+
.. function:: md5([, data], \*, usedforsecurity=True)
135+
.. function:: sha1([, data], \*, usedforsecurity=True)
136+
.. function:: sha224([, data], \*, usedforsecurity=True)
137+
.. function:: sha256([, data], \*, usedforsecurity=True)
138+
.. function:: sha384([, data], \*, usedforsecurity=True)
139+
.. function:: sha512([, data], \*, usedforsecurity=True)
140+
.. function:: sha3_224([, data], \*, usedforsecurity=True)
141+
.. function:: sha3_256([, data], \*, usedforsecurity=True)
142+
.. function:: sha3_384([, data], \*, usedforsecurity=True)
143+
.. function:: sha3_512([, data], \*, usedforsecurity=True)
144+
145+
Named constructors such as these are faster than passing an algorithm name to
146+
:func:`new`.
147+
148+
Attributes
149+
----------
150+
151+
Hashlib provides the following constant module attributes:
127152

128153
.. data:: algorithms_guaranteed
129154

@@ -144,10 +169,12 @@ Hashlib provides the following constant attributes:
144169

145170
.. versionadded:: 3.2
146171

172+
Hash Objects
173+
------------
174+
147175
The following values are provided as constant attributes of the hash objects
148176
returned by the constructors:
149177

150-
151178
.. data:: hash.digest_size
152179

153180
The size of the resulting hash in bytes.
@@ -207,6 +234,9 @@ A hash object has the following methods:
207234
SHAKE variable length digests
208235
-----------------------------
209236

237+
.. function:: shake_128([, data], \*, usedforsecurity=True)
238+
.. function:: shake_256([, data], \*, usedforsecurity=True)
239+
210240
The :func:`shake_128` and :func:`shake_256` algorithms provide variable
211241
length digests with length_in_bits//2 up to 128 or 256 bits of security.
212242
As such, their digest methods require a length. Maximum length is not limited
@@ -223,8 +253,13 @@ by the SHAKE algorithm.
223253

224254
Like :meth:`digest` except the digest is returned as a string object of
225255
double length, containing only hexadecimal digits. This may be used to
226-
exchange the value safely in email or other non-binary environments.
256+
exchange the value in email or other non-binary environments.
227257

258+
Example use:
259+
260+
>>> h = hashlib.shake_256(b'Nobody inspects the spammish repetition')
261+
>>> h.hexdigest(20)
262+
'44709d6fcb83d92a76dcb0b668c98e1b1d3dafe7'
228263

229264
File hashing
230265
------------
@@ -768,12 +803,17 @@ Domain Dedication 1.0 Universal:
768803
.. _BLAKE2: https://www.blake2.net
769804
.. _HMAC: https://en.wikipedia.org/wiki/Hash-based_message_authentication_code
770805
.. _BLAKE: https://web.archive.org/web/20200918190133/https://131002.net/blake/
771-
.. _SHA-3: https://en.wikipedia.org/wiki/NIST_hash_function_competition
806+
.. _SHA-3: https://en.wikipedia.org/wiki/Secure_Hash_Algorithms
772807
.. _ChaCha: https://cr.yp.to/chacha.html
773808
.. _pyblake2: https://pythonhosted.org/pyblake2/
774809
.. _NIST-SP-800-132: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf
775810
.. _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/publications/detail/fips/180/4/final
813+
.. _the FIPS 202 standard: https://csrc.nist.gov/publications/detail/fips/202/final
814+
776815

816+
.. _hashlib-seealso:
777817

778818
.. seealso::
779819

@@ -783,15 +823,18 @@ Domain Dedication 1.0 Universal:
783823
Module :mod:`base64`
784824
Another way to encode binary hashes for non-binary environments.
785825

786-
https://www.blake2.net
787-
Official BLAKE2 website.
826+
https://nvlpubs.nist.gov/nistpubs/fips/nist.fips.180-4.pdf
827+
The FIPS 180-4 publication on Secure Hash Algorithms.
828+
829+
https://csrc.nist.gov/publications/detail/fips/202/final
830+
The FIPS 202 publication on the SHA-3 Standard.
788831

789-
https://csrc.nist.gov/csrc/media/publications/fips/180/2/archive/2002-08-01/documents/fips180-2.pdf
790-
The FIPS 180-2 publication on Secure Hash Algorithms.
832+
https://www.blake2.net/
833+
Official BLAKE2 website.
791834

792-
https://en.wikipedia.org/wiki/Cryptographic_hash_function#Cryptographic_hash_algorithms
793-
Wikipedia article with information on which algorithms have known issues and
794-
what that means regarding their use.
835+
https://en.wikipedia.org/wiki/Cryptographic_hash_function
836+
Wikipedia article with information on which algorithms have known issues
837+
and what that means regarding their use.
795838

796839
https://www.ietf.org/rfc/rfc8018.txt
797840
PKCS #5: Password-Based Cryptography Specification Version 2.1

0 commit comments

Comments
 (0)