11
11
12
12
.. index ::
13
13
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
15
15
16
16
.. testsetup ::
17
17
22
22
23
23
This module implements a common interface to many different secure hash and
24
24
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
26
27
algorithm (defined in internet :rfc: `1321 `). The terms "secure hash" and
27
28
"message digest" are interchangeable. Older algorithms were called message
28
29
digests. The modern term is secure hash.
@@ -32,50 +33,50 @@ digests. The modern term is secure hash.
32
33
If you want the adler32 or crc32 hash functions, they are available in
33
34
the :mod: `zlib ` module.
34
35
35
- .. warning ::
36
-
37
- Some algorithms have known hash collision weaknesses, refer to the "See
38
- also" section at the end.
39
-
40
36
41
37
.. _hash-algorithms :
42
38
43
39
Hash algorithms
44
40
---------------
45
41
46
42
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.
58
49
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.
60
53
61
- Feeding string objects into :meth: `update ` is not supported, as hashes work
62
- on bytes, not on characters.
63
54
64
55
.. index :: single: OpenSSL; (use in module hashlib)
65
56
66
57
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 `,
73
59
: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.
75
75
76
76
.. versionadded :: 3.6
77
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 `.
78
+ :func: `sha3_384 `, :func: `sha3_512 `, :func: `shake_128 `, :func: `shake_256 `
79
+ were added.
79
80
80
81
.. versionadded :: 3.6
81
82
:func: `blake2b ` and :func: `blake2s ` were added.
@@ -89,10 +90,14 @@ library that Python uses on your platform. On most platforms the
89
90
that the hashing algorithm is not used in a security context, e.g. as a
90
91
non-cryptographic one-way compression function.
91
92
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.
93
95
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" ``::
96
101
97
102
>>> import hashlib
98
103
>>> m = hashlib.sha256()
@@ -108,22 +113,42 @@ More condensed:
108
113
>>> hashlib.sha256(b " Nobody inspects the spammish repetition" ).hexdigest()
109
114
'031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406'
110
115
111
- .. function :: new(name[, data], *, usedforsecurity=True)
116
+ Constructors
117
+ ------------
118
+
119
+ .. function :: new(name[, data], \*, usedforsecurity=True)
112
120
113
121
Is a generic constructor that takes the string *name * of the desired
114
122
algorithm as its first parameter. It also exists to allow access to the
115
123
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.
118
125
119
- Using :func: `new ` with an algorithm provided by OpenSSL :
126
+ Using :func: `new ` with an algorithm name :
120
127
121
128
>>> h = hashlib.new(' sha256' )
122
129
>>> h.update(b " Nobody inspects the spammish repetition" )
123
130
>>> h.hexdigest()
124
131
'031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406'
125
132
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:
127
152
128
153
.. data :: algorithms_guaranteed
129
154
@@ -144,10 +169,12 @@ Hashlib provides the following constant attributes:
144
169
145
170
.. versionadded :: 3.2
146
171
172
+ Hash Objects
173
+ ------------
174
+
147
175
The following values are provided as constant attributes of the hash objects
148
176
returned by the constructors:
149
177
150
-
151
178
.. data :: hash.digest_size
152
179
153
180
The size of the resulting hash in bytes.
@@ -207,6 +234,9 @@ A hash object has the following methods:
207
234
SHAKE variable length digests
208
235
-----------------------------
209
236
237
+ .. function :: shake_128([, data], \*, usedforsecurity=True)
238
+ .. function :: shake_256([, data], \*, usedforsecurity=True)
239
+
210
240
The :func: `shake_128 ` and :func: `shake_256 ` algorithms provide variable
211
241
length digests with length_in_bits//2 up to 128 or 256 bits of security.
212
242
As such, their digest methods require a length. Maximum length is not limited
@@ -223,8 +253,13 @@ by the SHAKE algorithm.
223
253
224
254
Like :meth: `digest ` except the digest is returned as a string object of
225
255
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.
227
257
258
+ Example use:
259
+
260
+ >>> h = hashlib.shake_256(b ' Nobody inspects the spammish repetition' )
261
+ >>> h.hexdigest(20 )
262
+ '44709d6fcb83d92a76dcb0b668c98e1b1d3dafe7'
228
263
229
264
File hashing
230
265
------------
@@ -768,12 +803,17 @@ Domain Dedication 1.0 Universal:
768
803
.. _BLAKE2 : https://www.blake2.net
769
804
.. _HMAC : https://en.wikipedia.org/wiki/Hash-based_message_authentication_code
770
805
.. _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
772
807
.. _ChaCha : https://cr.yp.to/chacha.html
773
808
.. _pyblake2 : https://pythonhosted.org/pyblake2/
774
809
.. _NIST-SP-800-132 : https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf
775
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/publications/detail/fips/180/4/final
813
+ .. _the FIPS 202 standard : https://csrc.nist.gov/publications/detail/fips/202/final
814
+
776
815
816
+ .. _hashlib-seealso :
777
817
778
818
.. seealso ::
779
819
@@ -783,15 +823,18 @@ Domain Dedication 1.0 Universal:
783
823
Module :mod: `base64 `
784
824
Another way to encode binary hashes for non-binary environments.
785
825
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.
788
831
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 .
791
834
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.
795
838
796
839
https://www.ietf.org/rfc/rfc8018.txt
797
840
PKCS #5: Password-Based Cryptography Specification Version 2.1
0 commit comments