-
Notifications
You must be signed in to change notification settings - Fork 31
/
pwrank.rb
112 lines (102 loc) · 2.27 KB
/
pwrank.rb
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/ruby
common = Array.new
wordsFile = File.new("./4000-most-common-english-words-csv.csv", "r")
for word in wordsFile
common = common << word
end
wordsFile.close
class ComplexString
@string = nil
@score = nil
def initialize(password)
@string = password
@score = 0
end
# Takes a string S and returns an integer 0-3
def checkLen
strLen = @string.length
if strLen > 8
@score += 3
elsif strLen > 5 && strLen <= 8
@score += 2
elsif strLen > 3 && strLen <= 5
@score += 1
end
end
# Evaluates to false if the length of the string is less than 6
def tooShort?
@string.length <= 5
end
def getScore
@score
end
def getString
@string
end
# adds 1 point to SCORE if the ComplexString does not contain a word in COMMON
def checkIfCommon(common)
hasMatch = false
# does not work in the case that the 'common' word is uppercase
for word in common
word = word.strip
if /#{word}/ =~ @string
puts "passowrd matches \'" + word + "\'"
hasMatch = true # true if it finds a match in the list of common words
break
end
end
if !hasMatch
@score += 1
end
end
# Increments SCORE by the addinonal levels of complexity, if the passowrd
# is complex.
def checkComplexity
hasSpecial = false
hasUpper = false
hasLower = false
hasNums = false
lower = /[a-z]/ # matches all lower case characters
upper = /[A-Z]/ # matches all upper case characters
special = /\W/ # matches special characters, i.e. !@#$%^&*
nums = /[0-9]/ # matches all numbers
if (lower =~ @string) && !hasLower
@score += 1
hasLower = true
end
if (upper =~ @string) && !hasUpper
@score += 2
hasUpper = true
end
if (nums =~ @string) && !hasNums
@score += 1
hasNums = true
end
if (special =~ @string) && !hasSpecial
@score += 2
hasSpecial = true
end
end
end
# main
if ARGV.length == 1
password = ARGV[0]
else
puts "Enter password: "
password = gets # if empty, password defaluts to a newline character"\n"
end
while (password == "\n")
puts "you must enter a password"
puts "Enter password: "
password = gets
end
password = ComplexString.new(password)
if !password.tooShort?
password.checkLen
password.checkIfCommon(common)
password.checkComplexity
puts password.getScore.to_s
else
puts "your password is too short"
exit
end