Skip to content

bpo-33430: import secrets module in secrets recipes #6705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions Doc/library/secrets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,9 @@ Generate an eight-character alphanumeric password:
.. testcode::

import string
import secrets
alphabet = string.ascii_letters + string.digits
password = ''.join(choice(alphabet) for i in range(8))
password = ''.join(secrets.choice(alphabet) for i in range(8))


.. note::
Expand All @@ -164,9 +165,10 @@ three digits:
.. testcode::

import string
import secrets
alphabet = string.ascii_letters + string.digits
while True:
password = ''.join(choice(alphabet) for i in range(10))
password = ''.join(secrets.choice(alphabet) for i in range(10))
if (any(c.islower() for c in password)
and any(c.isupper() for c in password)
and sum(c.isdigit() for c in password) >= 3):
Expand All @@ -177,19 +179,21 @@ Generate an `XKCD-style passphrase <https://xkcd.com/936/>`_:

.. testcode::

import secrets
# On standard Linux systems, use a convenient dictionary file.
# Other platforms may need to provide their own word-list.
with open('/usr/share/dict/words') as f:
words = [word.strip() for word in f]
password = ' '.join(choice(words) for i in range(4))
password = ' '.join(secrets.choice(words) for i in range(4))


Generate a hard-to-guess temporary URL containing a security token
suitable for password recovery applications:

.. testcode::

url = 'https://mydomain.com/reset=' + token_urlsafe()
import secrets
url = 'https://mydomain.com/reset=' + secrets.token_urlsafe()



Expand Down