File tree Expand file tree Collapse file tree 1 file changed +17
-3
lines changed Expand file tree Collapse file tree 1 file changed +17
-3
lines changed Original file line number Diff line number Diff line change 7
7
# Keeper Commander
8
8
9
9
#
10
-
10
+ import logging
11
11
import os
12
12
import random
13
13
import string
16
16
def randomSample (sampleLength = 0 , sampleString = '' ):
17
17
sample = ''
18
18
19
+ use_secrets = False
20
+
21
+ try :
22
+ # Older version of Python (before 3.6) don't have this module.
23
+ # If not installed, fall back to the original version of the code
24
+ import secrets
25
+ logging .debug ("module 'secrets' is installed" )
26
+ use_secrets = True
27
+ except ModuleNotFoundError :
28
+ logging .warning ("module 'secrets' is not installed" )
29
+
19
30
for i in range (sampleLength ):
20
- pos = int .from_bytes (os .urandom (2 ), 'big' ) % len (sampleString )
21
- sample += sampleString [pos ]
31
+ if use_secrets :
32
+ sample += secrets .choice (sampleString )
33
+ else :
34
+ pos = int .from_bytes (os .urandom (2 ), 'big' ) % len (sampleString )
35
+ sample += sampleString [pos ]
22
36
23
37
return sample
24
38
You can’t perform that action at this time.
0 commit comments