-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateRandomStrings.py
More file actions
85 lines (64 loc) · 3 KB
/
createRandomStrings.py
File metadata and controls
85 lines (64 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
import random
import string
import secrets
import sys
import os.path
from os import path
from uuid import uuid4
n = 14
def build_string_with_secrets():
# Use secrets module for strong cross-platform entropy
# https://docs.python.org/3/library/secrets.html#recipes-and-best-practices
# Python 3.10 or above for 'secrets' support
if sys.version_info < (3, 10):
raise Exception("Use only with Python 3.10 or higher")
my_alphabet = string.ascii_lowercase + string.ascii_uppercase + string.digits + "_" + "-"
secret_string = ''.join(secrets.choice(my_alphabet) for i in range(n))
return secret_string
def build_string_with_srandom():
# Use random.SystemRandom().shuffle strong Linux/Unix entropy
# Shuffle has a limited life, ending in 3.11
# "Deprecated since version 3.9, will be removed in version 3.11"
# https://docs.python.org/3/library/random.html?highlight=systemrandom#random.SystemRandom
universe = ["a", "b", "c", "d", "e", "f", "g", "h",
"i", "j", "k", "l", "m", "n", "o", "p",
"q", "r", "s", "t", "u", "v", "w", "x",
"y", "z", "A", "B", "C", "D", "E", "F",
"G", "H", "I", "J", "K", "L", "M", "N",
"O", "P", "Q", "R", "S", "T", "U", "V",
"W", "X", "Y", "Z", "0", "1", "2", "3",
"4", "5", "6", "7", "8", "9", "_", "-"]
rand_string = ""
# shuffle all characters
# generate other characters
for i in range(n):
random.SystemRandom().shuffle(universe)
rand_string += random.choice(universe)
return rand_string
def build_string_with_srandom_sample():
my_alphabet = string.ascii_lowercase + string.ascii_uppercase + string.digits + "_" + "-"
srandom_list = []
srandom_string = ""
for i in range(n):
srandom_list += random.SystemRandom().sample(my_alphabet, k=1)
return srandom_string.join(srandom_list)
def build_string_with_srandom_samplev2():
my_alphabet = string.ascii_lowercase + string.ascii_uppercase + string.digits + "_" + "-"
srandom_string = ""
srandom_list = random.SystemRandom().sample(my_alphabet, k=n)
return srandom_string.join(srandom_list)
def build_string_with_uuid():
# This approach absolutely does NOT generate random strings
# Rather, it generates unique strings that can be used for a variety of purposes
# that some might think of as "random."
UUID = str(uuid4()), # Unique-enough identifier
return UUID
if __name__ == '__main__':
# Unfinished experimenting with different ways to
# create 'unique' strings, a common requirement
print(f"Using Secrets: {build_string_with_secrets()}")
print(f"Using random.SystemRandomShuffle: {build_string_with_srandom()}")
print(f"Using random.SystemRandomSample: {build_string_with_srandom_sample()}")
print(f"Using random.SystemRandomSamplev2: {build_string_with_srandom_samplev2()}")
print(f"NOT random, but unique using uuid.uuid4: {build_string_with_uuid()}")