Skip to content

Commit 696b70e

Browse files
Pull in main
2 parents 8c28ddb + d4fa529 commit 696b70e

29 files changed

+321
-104
lines changed

Doc/library/array.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Notes:
5757
``Py_UNICODE``. This change doesn't affect its behavior because
5858
``Py_UNICODE`` is alias of ``wchar_t`` since Python 3.3.
5959

60-
.. deprecated-removed:: 3.3 4.0
60+
.. deprecated-removed:: 3.3 3.16
6161
Please migrate to ``'w'`` typecode.
6262

6363

Doc/library/hashlib.rst

Lines changed: 96 additions & 52 deletions
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,19 @@ 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.
95+
96+
.. versionchanged:: 3.12
97+
For any of the MD5, SHA1, SHA2, or SHA3 algorithms that the linked
98+
OpenSSL does not provide we fall back to a verified implementation from
99+
the `HACL\* project`_.
100+
101+
Usage
102+
-----
93103

94-
For example, to obtain the digest of the byte string ``b"Nobody inspects the
95-
spammish repetition"``::
104+
To obtain the digest of the byte string ``b"Nobody inspects the spammish
105+
repetition"``::
96106

97107
>>> import hashlib
98108
>>> m = hashlib.sha256()
@@ -108,22 +118,42 @@ More condensed:
108118
>>> hashlib.sha256(b"Nobody inspects the spammish repetition").hexdigest()
109119
'031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406'
110120

111-
.. function:: new(name[, data], *, usedforsecurity=True)
121+
Constructors
122+
------------
123+
124+
.. function:: new(name[, data], \*, usedforsecurity=True)
112125

113126
Is a generic constructor that takes the string *name* of the desired
114127
algorithm as its first parameter. It also exists to allow access to the
115128
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.
129+
library may offer.
118130

119-
Using :func:`new` with an algorithm provided by OpenSSL:
131+
Using :func:`new` with an algorithm name:
120132

121133
>>> h = hashlib.new('sha256')
122134
>>> h.update(b"Nobody inspects the spammish repetition")
123135
>>> h.hexdigest()
124136
'031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406'
125137

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

128158
.. data:: algorithms_guaranteed
129159

@@ -144,10 +174,12 @@ Hashlib provides the following constant attributes:
144174

145175
.. versionadded:: 3.2
146176

177+
Hash Objects
178+
------------
179+
147180
The following values are provided as constant attributes of the hash objects
148181
returned by the constructors:
149182

150-
151183
.. data:: hash.digest_size
152184

153185
The size of the resulting hash in bytes.
@@ -178,11 +210,6 @@ A hash object has the following methods:
178210
concatenation of all the arguments: ``m.update(a); m.update(b)`` is
179211
equivalent to ``m.update(a+b)``.
180212

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-
186213

187214
.. method:: hash.digest()
188215

@@ -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,18 @@ 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+
.. _HACL\* project: https://github.com/hacl-star/hacl-star
815+
776816

817+
.. _hashlib-seealso:
777818

778819
.. seealso::
779820

@@ -783,15 +824,18 @@ Domain Dedication 1.0 Universal:
783824
Module :mod:`base64`
784825
Another way to encode binary hashes for non-binary environments.
785826

786-
https://www.blake2.net
787-
Official BLAKE2 website.
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/publications/detail/fips/202/final
831+
The FIPS 202 publication on the SHA-3 Standard.
788832

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.
833+
https://www.blake2.net/
834+
Official BLAKE2 website.
791835

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.
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.
795839

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

Doc/whatsnew/3.13.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ Deprecated
134134
They will be removed in Python 3.15.
135135
(Contributed by Victor Stinner in :gh:`105096`.)
136136

137+
* :mod:`array`'s ``'u'`` format code, deprecated in docs since Python 3.3,
138+
emits :exc:`DeprecationWarning` since 3.13
139+
and will be removed in Python 3.16.
140+
Use the ``'w'`` format code instead.
141+
(contributed by Hugo van Kemenade in :gh:`80480`)
137142

138143
Removed
139144
=======

Lib/test/test_array.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313
import operator
1414
import struct
1515
import sys
16+
import warnings
1617

1718
import array
1819
from array import _array_reconstructor as array_reconstructor
1920

20-
sizeof_wchar = array.array('u').itemsize
21+
with warnings.catch_warnings():
22+
warnings.simplefilter('ignore', DeprecationWarning)
23+
sizeof_wchar = array.array('u').itemsize
2124

2225

2326
class ArraySubclass(array.array):
@@ -93,8 +96,16 @@ def test_empty(self):
9396
UTF32_LE = 20
9497
UTF32_BE = 21
9598

99+
96100
class ArrayReconstructorTest(unittest.TestCase):
97101

102+
def setUp(self):
103+
warnings.filterwarnings(
104+
"ignore",
105+
message="The 'u' type code is deprecated and "
106+
"will be removed in Python 3.16",
107+
category=DeprecationWarning)
108+
98109
def test_error(self):
99110
self.assertRaises(TypeError, array_reconstructor,
100111
"", "b", 0, b"")
@@ -1208,10 +1219,16 @@ def test_issue17223(self):
12081219
self.assertRaises(ValueError, a.tounicode)
12091220
self.assertRaises(ValueError, str, a)
12101221

1222+
def test_typecode_u_deprecation(self):
1223+
with self.assertWarns(DeprecationWarning):
1224+
array.array("u")
1225+
1226+
12111227
class UCS4Test(UnicodeTest):
12121228
typecode = 'w'
12131229
minitemsize = 4
12141230

1231+
12151232
class NumberTest(BaseTest):
12161233

12171234
def test_extslice(self):

Lib/test/test_buffer.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import sys, array, io, os
2525
from decimal import Decimal
2626
from fractions import Fraction
27+
from test.support import warnings_helper
2728

2829
try:
2930
from _testbuffer import *
@@ -3217,12 +3218,6 @@ def test_memoryview_compare_special_cases(self):
32173218
nd[0] = (-1, float('nan'))
32183219
self.assertNotEqual(memoryview(nd), nd)
32193220

3220-
# Depends on issue #15625: the struct module does not understand 'u'.
3221-
a = array.array('u', 'xyz')
3222-
v = memoryview(a)
3223-
self.assertNotEqual(a, v)
3224-
self.assertNotEqual(v, a)
3225-
32263221
# Some ctypes format strings are unknown to the struct module.
32273222
if ctypes:
32283223
# format: "T{>l:x:>l:y:}"
@@ -3236,6 +3231,15 @@ class BEPoint(ctypes.BigEndianStructure):
32363231
self.assertNotEqual(point, a)
32373232
self.assertRaises(NotImplementedError, a.tolist)
32383233

3234+
@warnings_helper.ignore_warnings(category=DeprecationWarning) # gh-80480 array('u')
3235+
def test_memoryview_compare_special_cases_deprecated_u_type_code(self):
3236+
3237+
# Depends on issue #15625: the struct module does not understand 'u'.
3238+
a = array.array('u', 'xyz')
3239+
v = memoryview(a)
3240+
self.assertNotEqual(a, v)
3241+
self.assertNotEqual(v, a)
3242+
32393243
def test_memoryview_compare_ndim_zero(self):
32403244

32413245
nd1 = ndarray(1729, shape=[], format='@L')

Lib/test/test_re.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from test.support import (gc_collect, bigmemtest, _2G,
22
cpython_only, captured_stdout,
33
check_disallow_instantiation, is_emscripten, is_wasi,
4-
SHORT_TIMEOUT)
4+
warnings_helper, SHORT_TIMEOUT)
55
import locale
66
import re
77
import string
@@ -1522,10 +1522,11 @@ def test_bug_6561(self):
15221522
for x in not_decimal_digits:
15231523
self.assertIsNone(re.match(r'^\d$', x))
15241524

1525+
@warnings_helper.ignore_warnings(category=DeprecationWarning) # gh-80480 array('u')
15251526
def test_empty_array(self):
15261527
# SF buf 1647541
15271528
import array
1528-
for typecode in 'bBuhHiIlLfd':
1529+
for typecode in 'bBhuwHiIlLfd':
15291530
a = array.array(typecode)
15301531
self.assertIsNone(re.compile(b"bla").match(a))
15311532
self.assertEqual(re.compile(b"").match(a).groups(), ())

0 commit comments

Comments
 (0)