Kaydet (Commit) cf8abcbe authored tarafından Daniel Chimeno's avatar Daniel Chimeno Kaydeden (comit) stevendaprano

import secrets module in secrets recipes (#6705)

üst f65e31fe
...@@ -145,8 +145,9 @@ Generate an eight-character alphanumeric password: ...@@ -145,8 +145,9 @@ Generate an eight-character alphanumeric password:
.. testcode:: .. testcode::
import string import string
import secrets
alphabet = string.ascii_letters + string.digits 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:: .. note::
...@@ -164,9 +165,10 @@ three digits: ...@@ -164,9 +165,10 @@ three digits:
.. testcode:: .. testcode::
import string import string
import secrets
alphabet = string.ascii_letters + string.digits alphabet = string.ascii_letters + string.digits
while True: 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) if (any(c.islower() for c in password)
and any(c.isupper() for c in password) and any(c.isupper() for c in password)
and sum(c.isdigit() for c in password) >= 3): and sum(c.isdigit() for c in password) >= 3):
...@@ -177,11 +179,12 @@ Generate an `XKCD-style passphrase <https://xkcd.com/936/>`_: ...@@ -177,11 +179,12 @@ Generate an `XKCD-style passphrase <https://xkcd.com/936/>`_:
.. testcode:: .. testcode::
import secrets
# On standard Linux systems, use a convenient dictionary file. # On standard Linux systems, use a convenient dictionary file.
# Other platforms may need to provide their own word-list. # Other platforms may need to provide their own word-list.
with open('/usr/share/dict/words') as f: with open('/usr/share/dict/words') as f:
words = [word.strip() for word in 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 Generate a hard-to-guess temporary URL containing a security token
...@@ -189,7 +192,8 @@ suitable for password recovery applications: ...@@ -189,7 +192,8 @@ suitable for password recovery applications:
.. testcode:: .. testcode::
url = 'https://mydomain.com/reset=' + token_urlsafe() import secrets
url = 'https://mydomain.com/reset=' + secrets.token_urlsafe()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment