Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5562563

Browse files
author
Steven D'Aprano
committed
Automated merge with ssh://hg.python.org/cpython
2 parents d77133b + 4ad4654 commit 5562563

2 files changed

Lines changed: 39 additions & 86 deletions

File tree

Doc/library/secrets.rst

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ hard-to-guess URLs, and similar.
8888
.. function:: token_urlsafe([nbytes=None])
8989

9090
Return a random URL-safe text string, containing *nbytes* random
91-
bytes. The text is Base64 encoded, so on average, each byte results
91+
bytes. The text is Base64 encoded, so on average each byte results
9292
in approximately 1.3 characters. If *nbytes* is ``None`` or not
9393
supplied, a reasonable default is used.
9494

@@ -106,7 +106,7 @@ To be secure against
106106
tokens need to have sufficient randomness. Unfortunately, what is
107107
considered sufficient will necessarily increase as computers get more
108108
powerful and able to make more guesses in a shorter period. As of 2015,
109-
it is believed that 64 bytes (512 bits) of randomness is sufficient for
109+
it is believed that 32 bytes (256 bits) of randomness is sufficient for
110110
the typical use-case expected for the :mod:`secrets` module.
111111

112112
For those who want to manage their own token length, you can explicitly
@@ -129,8 +129,8 @@ Other functions
129129
.. function:: compare_digest(a, b)
130130

131131
Return ``True`` if strings *a* and *b* are equal, otherwise ``False``,
132-
in such a way as to redice the risk of
133-
`timing attacks <http://codahale.com/a-lesson-in-timing-attacks/>`_ .
132+
in such a way as to reduce the risk of
133+
`timing attacks <http://codahale.com/a-lesson-in-timing-attacks/>`_.
134134
See :func:`hmac.compare_digest` for additional details.
135135

136136

@@ -151,11 +151,10 @@ Generate an eight-character alphanumeric password:
151151

152152
.. note::
153153

154-
Applications should
155-
`not store passwords in a recoverable format <http://cwe.mitre.org/data/definitions/257.html>`_ ,
156-
whether plain text or encrypted. They should always be salted and
157-
hashed using a cryptographically-strong one-way (irreversible) hash
158-
function.
154+
Applications should not
155+
`store passwords in a recoverable format <http://cwe.mitre.org/data/definitions/257.html>`_,
156+
whether plain text or encrypted. They should be salted and hashed
157+
using a cryptographically-strong one-way (irreversible) hash function.
159158

160159

161160
Generate a ten-character alphanumeric password with at least one
@@ -174,7 +173,7 @@ three digits:
174173
break
175174

176175

177-
Generate an `XKCD-style passphrase <http://xkcd.com/936/>`_ :
176+
Generate an `XKCD-style passphrase <http://xkcd.com/936/>`_:
178177

179178
.. testcode::
180179

Lib/secrets.py

Lines changed: 30 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,9 @@
11
"""Generate cryptographically strong pseudo-random numbers suitable for
22
managing secrets such as account authentication, tokens, and similar.
3-
See PEP 506 for more information.
43
4+
See PEP 506 for more information.
55
https://www.python.org/dev/peps/pep-0506/
66
7-
8-
Random numbers
9-
==============
10-
11-
The ``secrets`` module provides the following pseudo-random functions, based
12-
on SystemRandom, which in turn uses the most secure source of randomness your
13-
operating system provides.
14-
15-
16-
choice(sequence)
17-
Choose a random element from a non-empty sequence.
18-
19-
randbelow(n)
20-
Return a random int in the range [0, n).
21-
22-
randbits(k)
23-
Generates an int with k random bits.
24-
25-
SystemRandom
26-
Class for generating random numbers using sources provided by
27-
the operating system. See the ``random`` module for documentation.
28-
29-
30-
Token functions
31-
===============
32-
33-
The ``secrets`` module provides a number of functions for generating secure
34-
tokens, suitable for applications such as password resets, hard-to-guess
35-
URLs, and similar. All the ``token_*`` functions take an optional single
36-
argument specifying the number of bytes of randomness to use. If that is
37-
not given, or is ``None``, a reasonable default is used. That default is
38-
subject to change at any time, including during maintenance releases.
39-
40-
41-
token_bytes(nbytes=None)
42-
Return a random byte-string containing ``nbytes`` number of bytes.
43-
44-
>>> secrets.token_bytes(16) #doctest:+SKIP
45-
b'\\xebr\\x17D*t\\xae\\xd4\\xe3S\\xb6\\xe2\\xebP1\\x8b'
46-
47-
48-
token_hex(nbytes=None)
49-
Return a random text-string, in hexadecimal. The string has ``nbytes``
50-
random bytes, each byte converted to two hex digits.
51-
52-
>>> secrets.token_hex(16) #doctest:+SKIP
53-
'f9bf78b9a18ce6d46a0cd2b0b86df9da'
54-
55-
token_urlsafe(nbytes=None)
56-
Return a random URL-safe text-string, containing ``nbytes`` random
57-
bytes. On average, each byte results in approximately 1.3 characters
58-
in the final result.
59-
60-
>>> secrets.token_urlsafe(16) #doctest:+SKIP
61-
'Drmhze6EPcv0fN_81Bj-nA'
62-
63-
64-
(The examples above assume Python 3. In Python 2, byte-strings will display
65-
using regular quotes ``''`` with no prefix, and text-strings will have a
66-
``u`` prefix.)
67-
68-
69-
Other functions
70-
===============
71-
72-
compare_digest(a, b)
73-
Return True if strings a and b are equal, otherwise False.
74-
Performs the equality comparison in such a way as to reduce the
75-
risk of timing attacks.
76-
77-
See http://codahale.com/a-lesson-in-timing-attacks/ for a
78-
discussion on how timing attacks against ``==`` can reveal
79-
secrets from your application.
80-
81-
827
"""
838

849
__all__ = ['choice', 'randbelow', 'randbits', 'SystemRandom',
@@ -100,18 +25,47 @@
10025
choice = _sysrand.choice
10126

10227
def randbelow(exclusive_upper_bound):
28+
"""Return a random int in the range [0, n)."""
10329
return _sysrand._randbelow(exclusive_upper_bound)
10430

10531
DEFAULT_ENTROPY = 32 # number of bytes to return by default
10632

10733
def token_bytes(nbytes=None):
34+
"""Return a random byte string containing *nbytes* bytes.
35+
36+
If *nbytes* is ``None`` or not supplied, a reasonable
37+
default is used.
38+
39+
>>> token_bytes(16) #doctest:+SKIP
40+
b'\\xebr\\x17D*t\\xae\\xd4\\xe3S\\xb6\\xe2\\xebP1\\x8b'
41+
42+
"""
10843
if nbytes is None:
10944
nbytes = DEFAULT_ENTROPY
11045
return os.urandom(nbytes)
11146

11247
def token_hex(nbytes=None):
48+
"""Return a random text string, in hexadecimal.
49+
50+
The string has *nbytes* random bytes, each byte converted to two
51+
hex digits. If *nbytes* is ``None`` or not supplied, a reasonable
52+
default is used.
53+
54+
>>> token_hex(16) #doctest:+SKIP
55+
'f9bf78b9a18ce6d46a0cd2b0b86df9da'
56+
57+
"""
11358
return binascii.hexlify(token_bytes(nbytes)).decode('ascii')
11459

11560
def token_urlsafe(nbytes=None):
61+
"""Return a random URL-safe text string, in Base64 encoding.
62+
63+
The string has *nbytes* random bytes. If *nbytes* is ``None``
64+
or not supplied, a reasonable default is used.
65+
66+
>>> token_urlsafe(16) #doctest:+SKIP
67+
'Drmhze6EPcv0fN_81Bj-nA'
68+
69+
"""
11670
tok = token_bytes(nbytes)
11771
return base64.urlsafe_b64encode(tok).rstrip(b'=').decode('ascii')

0 commit comments

Comments
 (0)