-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathgenpasswd.py
More file actions
executable file
·56 lines (49 loc) · 1.28 KB
/
genpasswd.py
File metadata and controls
executable file
·56 lines (49 loc) · 1.28 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
#!/usr/bin/env python2
"""Generate random string as password.
Syntax:
genpasswd.py LENGTH
LENGTH is the required length of the random string.
Author: pigsboss@github
"""
import numpy as np
import sys
def is_valid(passwd):
has_digit = False
has_lower = False
has_upper = False
for i in range(ord('0'),ord('9')+1):
if chr(i) in passwd:
has_digit = True
break
for i in range(ord('a'),ord('z')+1):
if chr(i) in passwd:
has_lower = True
break
for i in range(ord('A'),ord('Z')+1):
if chr(i) in passwd:
has_upper = True
break
return has_digit & has_lower & has_upper
def randstr(length):
cdata = np.uint8(np.random.rand(length)*(10.0+26.0*2))
str = ''
for i in range(length):
if cdata[i]<10:
str+=chr(ord('0')+cdata[i])
elif cdata[i]<36:
str+=chr(ord('a')+cdata[i]-10)
else:
str+=chr(ord('A')+cdata[i]-36)
return str
def gen_valid_passwd(length,maxloops=1000):
t=0
while t<maxloops:
str = randstr(length)
if is_valid(str) is True:
break
t+=1
return str
if __name__ == '__main__':
length = eval(sys.argv[1])
passwd = gen_valid_passwd(length)
print passwd