-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomPWD.py
74 lines (60 loc) · 2.17 KB
/
RandomPWD.py
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
##Generate random password##
# description
# Generate random password with following rules:
# 1. 6-20 characters
# 2. at least one uppercase character
# 3. at least one lowercase character
# 4. at least one digit
# 5. at least one special character (!, @, #, $, %, ^, &, *)
# 6. no more than 2 characters repeating consecutively
# code !
import random
from pip._vendor.msgpack.fallback import xrange
LOWERCASE_CHARS = tuple(map(chr, xrange(ord('a'), ord('z') + 1)))
UPPERCASE_CHARS = tuple(map(chr, xrange(ord('A'), ord('Z') + 1)))
DIGITS = tuple(map(str, range(0, 10)))
SPECIALS = ('!', '@', '#', '$', '%', '^', '&', '*')
SEQUENCE = (LOWERCASE_CHARS,
UPPERCASE_CHARS,
DIGITS,
SPECIALS,
)
def generate_random_password(total, sequences):
r = _generate_random_number_for_each_sequence(total, len(sequences))
password = []
for (population, k) in zip(sequences, r):
n = 0
while n < k:
position = random.randint(0, len(population) - 1)
password += population[position]
n += 1
random.shuffle(password)
while _is_repeating(password):
random.shuffle(password)
return ''.join(password)
def _generate_random_number_for_each_sequence(total, sequence_number):
""" Generate random sequence with numbers (greater than 0).
The number of items equals to 'sequence_number' and
the total number of items equals to 'total'
"""
current_total = 0
r = []
for n in range(sequence_number - 1, 0, -1):
current = random.randint(1, total - current_total - n)
current_total += current
r.append(current)
r.append(total - sum(r))
random.shuffle(r)
return r
def _is_repeating(password):
""" Check if there is any 2 characters repeating consecutively """
n = 1
while n < len(password):
if password[n] == password[n - 1]:
return True
n += 1
return False
if __name__ == '__main__':
print(generate_random_password(random.randint(6, 30), SEQUENCE))
prompt = input("\n ** Hit return to exit plz **")
print(prompt)