-
Notifications
You must be signed in to change notification settings - Fork 31
/
pwrank.py
96 lines (86 loc) · 2.49 KB
/
pwrank.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Application to rank a password. Outputs a score 0-10
import sys, re, md5
lower = re.compile("[a-z]")
upper = re.compile("[A-Z]")
nums = re.compile("[0-9]")
special = re.compile("\W")
wordsFile = open("./4000-most-common-english-words-csv.csv", "r")
common = wordsFile.readlines()
wordsFile.close()
class ComplexString:
string = None
score = 0
def __init__(self, string):
self.string = string
# Returns False if the length of the string is less than 6
def tooShort(self):
return len(self.string) <= 5
# Takes a string S and returns an integer 0-3
def checkLen(self):
strLen = len(self.string)
if strLen > 8:
self.score += 3
elif strLen > 5 & strLen <= 8:
self.score += 2
elif strLen > 3 & strLen <= 5:
self.score += 1
# Increments SCORE by the addinonal levels of complexity, if the passowrd
# is complex.
def checkComplexity(self):
hasSpecial = False
hasUpper = False
hasLower = False
hasNums = False
for c in self.string:
if nums.match(c) and not hasNums:
self.score += 1
hasNums = True
if lower.match(c) and not hasLower:
self.score += 1
hasLower = True
if upper.match(c) and not hasUpper:
self.score += 2
hasUpper = True
if special.match(c) and not hasSpecial:
self.score += 2
hasSpecial = True
# adds 1 point to SCORE if the ComplexString does not contain a common word
def checkIfCommon(self):
notInCommon = True
for word in common:
checkWord = re.compile(word.strip())
if checkWord.match(self.string):
notInCommon = False
if notInCommon:
self.score += 1
# Removes questionable input from the password
def sanitize(self):
self.string = self.string.replace("\\","")
self.string = self.string.replace(".","")
# add functionality to remove whitespace
print self.string
# Returns the md5 hash of the ComplexString
def hashMD5(self):
h = md5.new()
h.update(self.string)
return h.digest()
# Returns the complexity score for this ComplexString
def getScore(self):
return self.score
if len(sys.argv) == 2:
password = sys.argv[1]
else:
password = raw_input("Enter password: ")
if password != "":
password = ComplexString(password)
if not password.tooShort():
password.checkLen()
password.checkComplexity()
password.checkIfCommon()
print "your password score is " + str(password.getScore())
else:
print "your password is too short"
sys.exit(0)
else:
print "you must enter a password"
sys.exit(0)