dev-random-passwords 0.0.6 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/dev-random-passwords.rb +108 -72
- data/lib/dev-random-passwords/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 88f5996ac8df46987fd4d86a9a9cf748bc62de00
|
4
|
+
data.tar.gz: 9278f36b51fde496e4dcdde56010c47091489cb0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 04c9fe5ac098ae363f9be7bb178cefb95438f7c3a25e2080214790c7a14cb0faf1895d4787a47b2c631f5f28ebd295254a6336df37c2272d4fb1fb2a84030ffb
|
7
|
+
data.tar.gz: eafbf4760d066d37f5df37ec0d173288d754539657081441748fed1504a0e8333740cc37fa5cfcccf41a8b2a1ddedeac0e7c7b05966809dc44c94492b2675609
|
data/lib/dev-random-passwords.rb
CHANGED
@@ -8,6 +8,8 @@ module DevRandomPasswords
|
|
8
8
|
UPPERCASE_CHARS = ('A'..'Z').to_a.join("")
|
9
9
|
DIGITS = ('0'..'9').to_a.join("")
|
10
10
|
SPECIAL_CHARS = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
|
11
|
+
NOT_READABLE_ERROR = "is not readable by the current user please change permissions on this file or switch users."
|
12
|
+
REQUIREMENT_CONFLICT_ERROR = "You required a character type that is not possible with your current password options. Set your options and try again."
|
11
13
|
|
12
14
|
def initialize
|
13
15
|
@charset = LOWERCASE_CHARS + UPPERCASE_CHARS + DIGITS + SPECIAL_CHARS
|
@@ -16,66 +18,6 @@ module DevRandomPasswords
|
|
16
18
|
@hardware_random = false
|
17
19
|
end
|
18
20
|
|
19
|
-
def get_byte
|
20
|
-
if @hardware_random == true
|
21
|
-
|
22
|
-
if File.exist?('/dev/hwrng')
|
23
|
-
if File.readable?('/dev/hwrng')
|
24
|
-
random_file = File.new('/dev/hwrng', 'r')
|
25
|
-
random_byte = random_file.read(1).ord
|
26
|
-
random_file.close
|
27
|
-
if random_byte
|
28
|
-
return random_byte
|
29
|
-
end
|
30
|
-
else
|
31
|
-
raise "/dev/hwrng is not readable by the current user please change permissions on this file"
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
if File.exist?('/dev/hwrandom')
|
36
|
-
if File.readable?('/dev/hwrandom')
|
37
|
-
random_file = File.new('/dev/hwrandom', 'r')
|
38
|
-
random_byte = random_file.read(1).ord
|
39
|
-
random_file.close
|
40
|
-
if random_byte
|
41
|
-
return random_byte
|
42
|
-
end
|
43
|
-
else
|
44
|
-
raise "/dev/hwrandom is not readable by the current user please change permissions on this file"
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
end
|
49
|
-
|
50
|
-
if File.exist?('/dev/random')
|
51
|
-
if File.readable?('/dev/random')
|
52
|
-
random_file = File.new('/dev/random', 'r')
|
53
|
-
random_byte = random_file.read(1).ord
|
54
|
-
random_file.close
|
55
|
-
if random_byte
|
56
|
-
return random_byte
|
57
|
-
end
|
58
|
-
else
|
59
|
-
raise "/dev/random is not readable by the current user please change permissions on this file"
|
60
|
-
end
|
61
|
-
|
62
|
-
elsif File.exist?('/dev/urandom')
|
63
|
-
if File.readable?('/dev/urandom')
|
64
|
-
random_file = File.new('/dev/urandom', 'r')
|
65
|
-
random_byte = random_file.read(1).ord
|
66
|
-
random_file.close
|
67
|
-
if random_byte
|
68
|
-
return random_byte
|
69
|
-
end
|
70
|
-
else
|
71
|
-
raise "/dev/urandom is not readable by the current user please change permissions on this file"
|
72
|
-
end
|
73
|
-
|
74
|
-
else
|
75
|
-
raise "Could not find a random number generator on your system"
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
21
|
def set_options(options={
|
80
22
|
'lowercase' => true,
|
81
23
|
'uppercase' => true,
|
@@ -150,6 +92,89 @@ module DevRandomPasswords
|
|
150
92
|
|
151
93
|
end
|
152
94
|
|
95
|
+
def generate
|
96
|
+
new_password = ""
|
97
|
+
|
98
|
+
@char_length.times do
|
99
|
+
rand_num = get_byte
|
100
|
+
while rand_num >= @charset.length
|
101
|
+
rand_num -= @charset.length
|
102
|
+
end
|
103
|
+
new_password += @charset[rand_num]
|
104
|
+
end
|
105
|
+
|
106
|
+
check_for_conflicts
|
107
|
+
|
108
|
+
if requirements_met? new_password
|
109
|
+
return new_password
|
110
|
+
else
|
111
|
+
return generate
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
115
|
+
|
116
|
+
private
|
117
|
+
|
118
|
+
def get_byte
|
119
|
+
if @hardware_random == true
|
120
|
+
|
121
|
+
if File.exist?('/dev/hwrng')
|
122
|
+
if File.readable?('/dev/hwrng')
|
123
|
+
random_file = File.new('/dev/hwrng', 'r')
|
124
|
+
random_byte = random_file.read(1).ord
|
125
|
+
random_file.close
|
126
|
+
if random_byte
|
127
|
+
return random_byte
|
128
|
+
end
|
129
|
+
else
|
130
|
+
raise "/dev/hwrng #{NOT_READABLE_ERROR}"
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
if File.exist?('/dev/hwrandom')
|
135
|
+
if File.readable?('/dev/hwrandom')
|
136
|
+
random_file = File.new('/dev/hwrandom', 'r')
|
137
|
+
random_byte = random_file.read(1).ord
|
138
|
+
random_file.close
|
139
|
+
if random_byte
|
140
|
+
return random_byte
|
141
|
+
end
|
142
|
+
else
|
143
|
+
raise "/dev/hwrandom #{NOT_READABLE_ERROR}"
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
end
|
148
|
+
|
149
|
+
if File.exist?('/dev/random')
|
150
|
+
if File.readable?('/dev/random')
|
151
|
+
random_file = File.new('/dev/random', 'r')
|
152
|
+
random_byte = random_file.read(1).ord
|
153
|
+
random_file.close
|
154
|
+
if random_byte
|
155
|
+
return random_byte
|
156
|
+
end
|
157
|
+
else
|
158
|
+
raise "/dev/random #{NOT_READABLE_ERROR}"
|
159
|
+
end
|
160
|
+
|
161
|
+
elsif File.exist?('/dev/urandom')
|
162
|
+
if File.readable?('/dev/urandom')
|
163
|
+
random_file = File.new('/dev/urandom', 'r')
|
164
|
+
random_byte = random_file.read(1).ord
|
165
|
+
random_file.close
|
166
|
+
if random_byte
|
167
|
+
return random_byte
|
168
|
+
end
|
169
|
+
else
|
170
|
+
raise "/dev/urandom #{NOT_READABLE_ERROR}"
|
171
|
+
end
|
172
|
+
|
173
|
+
else
|
174
|
+
raise "Could not find a random number generator on your system"
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
153
178
|
def requirements_met?(password)
|
154
179
|
|
155
180
|
if @requirements
|
@@ -182,21 +207,32 @@ module DevRandomPasswords
|
|
182
207
|
|
183
208
|
end
|
184
209
|
|
185
|
-
def
|
186
|
-
new_password = ""
|
210
|
+
def check_for_conflicts
|
187
211
|
|
188
|
-
@
|
189
|
-
|
190
|
-
|
191
|
-
|
212
|
+
if @requirements
|
213
|
+
if @requirements['uppercase']
|
214
|
+
if (@charset.split("") & UPPERCASE_CHARS.split("")).empty?
|
215
|
+
raise REQUIREMENT_CONFLICT_ERROR
|
216
|
+
end
|
192
217
|
end
|
193
|
-
new_password += @charset[rand_num]
|
194
|
-
end
|
195
218
|
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
219
|
+
if @requirements['lowercase']
|
220
|
+
if (@charset.split("") & LOWERCASE_CHARS.split("")).empty?
|
221
|
+
raise REQUIREMENT_CONFLICT_ERROR
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
if @requirements['digits']
|
226
|
+
if (@charset.split("") & DIGITS.split("")).empty?
|
227
|
+
raise REQUIREMENT_CONFLICT_ERROR
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
if @requirements['special']
|
232
|
+
if (@charset.split("") & SPECIAL_CHARS.split("")).empty?
|
233
|
+
raise REQUIREMENT_CONFLICT_ERROR
|
234
|
+
end
|
235
|
+
end
|
200
236
|
end
|
201
237
|
|
202
238
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dev-random-passwords
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joel Smith
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-10-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|