Skip to content

Commit cf8abcb

Browse files
dchimenostevendaprano
authored andcommitted
import secrets module in secrets recipes (#6705)
1 parent f65e31f commit cf8abcb

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

Doc/library/secrets.rst

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,9 @@ Generate an eight-character alphanumeric password:
145145
.. testcode::
146146

147147
import string
148+
import secrets
148149
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))
150151

151152

152153
.. note::
@@ -164,9 +165,10 @@ three digits:
164165
.. testcode::
165166

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

178180
.. testcode::
179181

182+
import secrets
180183
# On standard Linux systems, use a convenient dictionary file.
181184
# Other platforms may need to provide their own word-list.
182185
with open('/usr/share/dict/words') as f:
183186
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))
185188

186189

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

190193
.. testcode::
191194

192-
url = 'https://mydomain.com/reset=' + token_urlsafe()
195+
import secrets
196+
url = 'https://mydomain.com/reset=' + secrets.token_urlsafe()
193197

194198

195199

0 commit comments

Comments
 (0)