@@ -145,8 +145,9 @@ Generate an eight-character alphanumeric password:
145
145
.. testcode ::
146
146
147
147
import string
148
+ import secrets
148
149
alphabet = string.ascii_letters + string.digits
149
- password = ''.join(choice(alphabet) for i in range(8))
150
+ password = ''.join(secrets. choice(alphabet) for i in range(8))
150
151
151
152
152
153
.. note ::
@@ -164,9 +165,10 @@ three digits:
164
165
.. testcode ::
165
166
166
167
import string
168
+ import secrets
167
169
alphabet = string.ascii_letters + string.digits
168
170
while True:
169
- password = ''.join(choice(alphabet) for i in range(10))
171
+ password = ''.join(secrets. choice(alphabet) for i in range(10))
170
172
if (any(c.islower() for c in password)
171
173
and any(c.isupper() for c in password)
172
174
and sum(c.isdigit() for c in password) >= 3):
@@ -177,19 +179,21 @@ Generate an `XKCD-style passphrase <https://xkcd.com/936/>`_:
177
179
178
180
.. testcode ::
179
181
182
+ import secrets
180
183
# On standard Linux systems, use a convenient dictionary file.
181
184
# Other platforms may need to provide their own word-list.
182
185
with open('/usr/share/dict/words') as f:
183
186
words = [word.strip() for word in f]
184
- password = ' '.join(choice(words) for i in range(4))
187
+ password = ' '.join(secrets. choice(words) for i in range(4))
185
188
186
189
187
190
Generate a hard-to-guess temporary URL containing a security token
188
191
suitable for password recovery applications:
189
192
190
193
.. testcode ::
191
194
192
- url = 'https://mydomain.com/reset=' + token_urlsafe()
195
+ import secrets
196
+ url = 'https://mydomain.com/reset=' + secrets.token_urlsafe()
193
197
194
198
195
199
0 commit comments