toolrack 0.8.2 → 0.9.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +54 -4
- data/lib/toolrack/password_utils.rb +301 -0
- data/lib/toolrack/version.rb +1 -1
- data/lib/toolrack.rb +16 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 42fd35a5442e9aaae8d559536346c0de0228db4d93aa40d1d4720eeacf5c7c31
|
4
|
+
data.tar.gz: 02efc3e79b36480a037174d7005cbcbfddc217688e71b2ebc7b41e0d66bfe12b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 54354902f0f97ca0c7c0295ec8ca9b7e25912fcb69954f4d290793ed5a4732e7ec5806ef35c4a5a08e8cc6e68552ce9744023680c5d14d960bfcef09931b0f62
|
7
|
+
data.tar.gz: 861b791fa8f1396d111f13f21cb91a4375ef9a5c565bcd7c842dca1cae1550d6919a14daf6d8d393009ff3bf44502d8d4b64bd678d813412f89d9e4167b2f686
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Toolrack
|
2
2
|
|
3
|
-
Toolrack just the collection of utilities that helps in my code clarity
|
3
|
+
Toolrack just the collection of utilities that helps in my code clarity and assistance since those utils too small to be a gem on its own.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -40,10 +40,21 @@ class EditController
|
|
40
40
|
# enable instance method to access those uitilities
|
41
41
|
include ToolRack::ConditionUtils
|
42
42
|
include ToolRack::ExceptionUtils
|
43
|
+
include ToolRack::PasswordUtils
|
44
|
+
include ToolRack::RuntimeUtils
|
45
|
+
include ToolRack::DataConversionUtils
|
46
|
+
|
47
|
+
# there is also shortcut since I write it so often
|
48
|
+
include TR::CondUtils
|
49
|
+
include TR::ExpUtils
|
50
|
+
include TR::PassUtils
|
51
|
+
include TR::RTUtils
|
52
|
+
include TR::DataConvUtils
|
43
53
|
end
|
44
54
|
```
|
45
55
|
|
46
|
-
Currently it has
|
56
|
+
Currently it has 5 modules:
|
57
|
+
|
47
58
|
* Condition Utilities
|
48
59
|
* is\_empty?(obj) - I found that I've type the condition if not (x.nil? and x.empty?) too frequent that I think this is very best to turn this into a function. The empty test shall take the following test in sequence:
|
49
60
|
* First test is x.nil?. If it is return true
|
@@ -77,6 +88,45 @@ Currently it has 3 modules:
|
|
77
88
|
* raise\_if\_false(obj, message, error) - As the name implied
|
78
89
|
* raise\_if\_true(obj, message, error) - As the name implied
|
79
90
|
|
80
|
-
|
81
|
-
|
91
|
+
* Runtime utils - Tired rewriting this in other project. Detect if the running system is on which operating system / runtime. The function is pretty self descriptive
|
92
|
+
* RuntimeUtils.on\_windows?
|
93
|
+
* RuntimeUtils.on\_mac?
|
94
|
+
* RuntimeUtils.on\_linux?
|
95
|
+
* RuntimeUtils.on\_ruby?
|
96
|
+
* RuntimeUtils.on\_jruby?
|
97
|
+
|
98
|
+
* Password utils - Generate random password with designated complexity
|
99
|
+
* generate\_random\_password(length, options = { complexity: \<value\>, enforce\_quality: \<true/false\> })
|
100
|
+
* length - length of the required generated password
|
101
|
+
* options[:complexity]:
|
102
|
+
* 1 - lowercase alphabet
|
103
|
+
* 2 - lower + upper case alphabet [alpha]
|
104
|
+
* 3 - lower + upper + number [alpha numeric]
|
105
|
+
* 4 - lower + upper + number + symbol
|
106
|
+
* options[:enforce\_quality]
|
107
|
+
* true - generated password is guaranteed to contain the required complexity. E.g. if complexity 4 is required, the password MUST contain lower, upper, number and symbol as its output or a new password that comply to the complexity shall be regenerated. This process is repeated until the required complexity is achieved
|
108
|
+
* false - The rules of the complexity is relexed. E.g. if complexity 4 is requested, the output might not have symbol in it.
|
109
|
+
* gen\_rand\_pass(length, options = { }) & gen\_pass(length, options = { })
|
110
|
+
* Alias for method generate\_random\_password
|
111
|
+
|
112
|
+
* all\_lowercase\_alpha?(str)
|
113
|
+
* all\_uppercase\_alpha?(str)
|
114
|
+
* all\_alpha?(str)
|
115
|
+
* all\_number?(str)
|
116
|
+
* all\_symbol?(str)
|
117
|
+
* all\_alpha\_numeric?(str)
|
118
|
+
* all\_alpha\_numeric\_and\_symbol?(str)
|
119
|
+
* All methods above starts with all_\* is to check for strict compliance to the required output. E.g. if all\_alpha?(str) is used, the input str MUST be all alpha to get a true status
|
120
|
+
|
121
|
+
* has\_lowercase\_alpha?(str)
|
122
|
+
* has\_uppercase\_alpha?(str)
|
123
|
+
* has\_alpha?(str)
|
124
|
+
* has\_number?(str)
|
125
|
+
* has\_symbol?(str)
|
126
|
+
* has\_alpha\_numeric?(str)
|
127
|
+
* has\_alpha\_numeric\_or\_symbol?(str)
|
128
|
+
* All methods above start with has_\* is just to check if the given str contains the required specification. E.g string 'abcdE' will still get true status if pass to has\_lowercase\_alpha?(str). But value 1234 or '1234' or '$%^&' pass to has\_lowercase\_alpha?(str) shall return false.
|
129
|
+
|
130
|
+
|
131
|
+
Check out the rspec folder for usage
|
82
132
|
|
@@ -0,0 +1,301 @@
|
|
1
|
+
|
2
|
+
require 'securerandom'
|
3
|
+
|
4
|
+
module Antrapol
|
5
|
+
module ToolRack
|
6
|
+
module PasswordUtils
|
7
|
+
include Antrapol::ToolRack::ConditionUtils
|
8
|
+
|
9
|
+
class PasswordUtilsError < StandardError; end
|
10
|
+
|
11
|
+
# complexity
|
12
|
+
# 1 : lowercase alphabet
|
13
|
+
# 2 : lower + upper alphabet
|
14
|
+
# 3 : lower + upper + number
|
15
|
+
# 4 : lower + upper + number + symbol
|
16
|
+
def generate_random_password(length = 12, opts = { complexity: 4, enforce_quality: true })
|
17
|
+
|
18
|
+
|
19
|
+
length = 12 if is_empty?(length)
|
20
|
+
length = length.to_i
|
21
|
+
length = 12 if length <= 0
|
22
|
+
|
23
|
+
logger.debug "Length : #{length}"
|
24
|
+
|
25
|
+
if not_empty?(opts)
|
26
|
+
complexity = opts[:complexity] || 4
|
27
|
+
|
28
|
+
raise PasswordUtilsError, "Password length too short but complexity too high. Not able generate the password with required complexity." if length <= complexity
|
29
|
+
|
30
|
+
enforce_quality = opts[:enforce_quality] || true
|
31
|
+
end
|
32
|
+
|
33
|
+
complexity = 4 if is_empty?(complexity)
|
34
|
+
complexity = 4 if complexity > 4
|
35
|
+
complexity = 1 if complexity < 1
|
36
|
+
|
37
|
+
logger.debug "Complexity : #{complexity}"
|
38
|
+
|
39
|
+
enforce_quality = true if is_empty?(enforce_quality) or not is_bool?(enforce_quality)
|
40
|
+
|
41
|
+
res = []
|
42
|
+
antropy = [("a".."z"),("A".."Z"),(0..9),("!".."?")]
|
43
|
+
selection = antropy[0..complexity-1].map { |s| s.to_a }.flatten
|
44
|
+
|
45
|
+
fres = nil
|
46
|
+
loop do
|
47
|
+
|
48
|
+
len = length
|
49
|
+
processed = 0
|
50
|
+
loop do
|
51
|
+
#res += selection.sample(length)
|
52
|
+
res += selection.sort_by { SecureRandom.random_number }
|
53
|
+
break if res.length >= length
|
54
|
+
end
|
55
|
+
|
56
|
+
fres = res[0...length].join
|
57
|
+
|
58
|
+
if enforce_quality
|
59
|
+
case complexity
|
60
|
+
when 1
|
61
|
+
if all_lowercase_alpha?(fres)
|
62
|
+
break
|
63
|
+
else
|
64
|
+
logger.debug "Failed lowercase alpha quality check. #{fres} - Regenerating..."
|
65
|
+
res.clear
|
66
|
+
end
|
67
|
+
when 2
|
68
|
+
st, dst = all_alpha?(fres)
|
69
|
+
if st
|
70
|
+
break
|
71
|
+
else
|
72
|
+
logger.debug "Failed all alpha quality check. #{dst} - Regenerating..."
|
73
|
+
res.clear
|
74
|
+
end
|
75
|
+
when 3
|
76
|
+
st, dst = all_alpha_numeric?(fres)
|
77
|
+
if st
|
78
|
+
break
|
79
|
+
else
|
80
|
+
logger.debug "Failed alpha numeric quality check. #{dst} - Regenerating"
|
81
|
+
res.clear
|
82
|
+
end
|
83
|
+
when 4
|
84
|
+
st, dst = all_alpha_numeric_and_symbol?(fres)
|
85
|
+
if st
|
86
|
+
break
|
87
|
+
else
|
88
|
+
logger.debug "Failed alpha numeric + symbol quality check. #{fres} / #{dst} - Regenerating"
|
89
|
+
res.clear
|
90
|
+
end
|
91
|
+
else
|
92
|
+
logger.debug "Unknown complexity?? #{complexity}"
|
93
|
+
break
|
94
|
+
end
|
95
|
+
else
|
96
|
+
break
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
fres
|
102
|
+
|
103
|
+
#length = 12 if is_empty?(length)
|
104
|
+
#length = length.to_i
|
105
|
+
#length = 12 if length <= 0
|
106
|
+
|
107
|
+
#antropy = ('!'..'~').to_a
|
108
|
+
#antropy.sort_by { SecureRandom.random_number }.join[0...length]
|
109
|
+
end
|
110
|
+
alias_method :gen_rand_pass, :generate_random_password
|
111
|
+
alias_method :gen_pass, :generate_random_password
|
112
|
+
|
113
|
+
def all_lowercase_alpha?(str)
|
114
|
+
return false if is_empty?(str) or not str.is_a?(String)
|
115
|
+
|
116
|
+
s = str.split("")
|
117
|
+
lalpha = ('a'..'z').to_a
|
118
|
+
|
119
|
+
(s.difference(lalpha).length == 0)
|
120
|
+
end
|
121
|
+
|
122
|
+
def has_lowercase_alpha?(str)
|
123
|
+
return false if is_empty?(str) or not str.is_a?(String)
|
124
|
+
|
125
|
+
s = str.split("")
|
126
|
+
lalpha = ('a'..'z').to_a
|
127
|
+
|
128
|
+
(s & lalpha).length > 0
|
129
|
+
end
|
130
|
+
|
131
|
+
def all_uppercase_alpha?(str)
|
132
|
+
return false if is_empty?(str) or not str.is_a?(String)
|
133
|
+
|
134
|
+
s = str.split("")
|
135
|
+
ualpha = ('A'..'Z').to_a
|
136
|
+
|
137
|
+
s.difference(ualpha).length == 0
|
138
|
+
end
|
139
|
+
|
140
|
+
def has_uppercase_alpha?(str)
|
141
|
+
return false if is_empty?(str) or not str.is_a?(String)
|
142
|
+
|
143
|
+
s = str.split("")
|
144
|
+
ualpha = ('A'..'Z').to_a
|
145
|
+
|
146
|
+
(s & ualpha).length > 0
|
147
|
+
end
|
148
|
+
|
149
|
+
def all_alpha?(str)
|
150
|
+
return false if is_empty?(str) or not str.is_a?(String)
|
151
|
+
|
152
|
+
s = str.split("")
|
153
|
+
lalpha = ('a'..'z').to_a
|
154
|
+
ualpha = ('A'..'Z').to_a
|
155
|
+
num = ('0'..'9').to_a
|
156
|
+
sym = ('!'..'?').to_a
|
157
|
+
|
158
|
+
alpha = [lalpha, ualpha].flatten
|
159
|
+
|
160
|
+
t1 = (alpha.difference(s).length == 0)
|
161
|
+
t2 = (alpha.difference(s).length == 0)
|
162
|
+
|
163
|
+
t3 = (s & num).length > 0
|
164
|
+
t4 = (s & sym).length > 0
|
165
|
+
|
166
|
+
[!(t1 or t2 or t3 or t4), res = { has_lower_alpha: t1, has_upper_alpha: t2, has_num: t3, has_symbol: t4 }]
|
167
|
+
end
|
168
|
+
|
169
|
+
def has_alpha?(str)
|
170
|
+
return false if is_empty?(str) or not str.is_a?(String)
|
171
|
+
|
172
|
+
s = str.split("")
|
173
|
+
lalpha = ('a'..'z').to_a
|
174
|
+
ualpha = ('A'..'Z').to_a
|
175
|
+
alpha = [lalpha, ualpha].flatten
|
176
|
+
|
177
|
+
(alpha & s).length > 0
|
178
|
+
end
|
179
|
+
|
180
|
+
def has_number?(str)
|
181
|
+
return false if is_empty?(str)
|
182
|
+
|
183
|
+
str = str.to_s if not str.is_a?(String)
|
184
|
+
|
185
|
+
s = str.split("")
|
186
|
+
num = ('0'..'9').to_a
|
187
|
+
|
188
|
+
(s & num).length > 0
|
189
|
+
|
190
|
+
end
|
191
|
+
|
192
|
+
def all_number?(str)
|
193
|
+
return false if is_empty?(str)
|
194
|
+
|
195
|
+
str = str.to_s if not str.is_a?(String)
|
196
|
+
|
197
|
+
s = str.split("")
|
198
|
+
num = ('0'..'9').to_a
|
199
|
+
|
200
|
+
!(s.difference(num).length > 0)
|
201
|
+
|
202
|
+
end
|
203
|
+
|
204
|
+
def all_symbol?(str)
|
205
|
+
return false if is_empty?(str)
|
206
|
+
|
207
|
+
s = str.split("")
|
208
|
+
sym = ('!'..'?').to_a
|
209
|
+
!(s.difference(sym).length > 0)
|
210
|
+
|
211
|
+
end
|
212
|
+
|
213
|
+
def has_symbol?(str)
|
214
|
+
return false if is_empty?(str)
|
215
|
+
|
216
|
+
s = str.split("")
|
217
|
+
sym = ('!'..'?').to_a
|
218
|
+
|
219
|
+
p (s & sym)
|
220
|
+
(s & sym).length > 0
|
221
|
+
end
|
222
|
+
|
223
|
+
|
224
|
+
def all_alpha_numeric?(str)
|
225
|
+
return false if is_empty?(str)
|
226
|
+
|
227
|
+
s = str.split("")
|
228
|
+
lalpha = ('a'..'z').to_a
|
229
|
+
ualpha = ('A'..'Z').to_a
|
230
|
+
num = ('0'..'9').to_a
|
231
|
+
sym = ('!'..'?').to_a
|
232
|
+
sym = sym.difference(num)
|
233
|
+
|
234
|
+
t1 = ((s & lalpha).length > 0)
|
235
|
+
t2 = ((s & ualpha).length > 0)
|
236
|
+
t3 = ((s & num).length > 0)
|
237
|
+
|
238
|
+
t4 = ((s & sym).length > 0)
|
239
|
+
|
240
|
+
[(t1 and t2 and t3 and !t4), res = { has_lower_alpha: t1, has_upper_alpha: t2, has_num: t3, has_symbol: t4 }]
|
241
|
+
end
|
242
|
+
|
243
|
+
def has_alpha_numeric?(str)
|
244
|
+
return false if is_empty?(str)
|
245
|
+
|
246
|
+
s = str.split("")
|
247
|
+
lalpha = ('a'..'z').to_a
|
248
|
+
ualpha = ('A'..'Z').to_a
|
249
|
+
num = ('0'..'9').to_a
|
250
|
+
alphanum = [lalpha, ualpha, num].flatten
|
251
|
+
|
252
|
+
(s & alphanum).length > 0
|
253
|
+
|
254
|
+
end
|
255
|
+
|
256
|
+
def all_alpha_numeric_and_symbol?(str)
|
257
|
+
return false if is_empty?(str)
|
258
|
+
|
259
|
+
s = str.split("")
|
260
|
+
lalpha = ('a'..'z').to_a
|
261
|
+
ualpha = ('A'..'Z').to_a
|
262
|
+
num = ('0'..'9').to_a
|
263
|
+
sym = ('!'..'?').to_a
|
264
|
+
sym = sym.difference(num)
|
265
|
+
|
266
|
+
t1 = ((s & lalpha).length > 0)
|
267
|
+
t2 = ((s & ualpha).length > 0)
|
268
|
+
t3 = ((s & num).length > 0)
|
269
|
+
t4 = ((s & sym).length > 0)
|
270
|
+
|
271
|
+
[(t1 and t2 and t3 and t4), res = { has_lower_alpha: t1, has_upper_alpha: t2, has_num: t3, has_symbol: t4 }]
|
272
|
+
end
|
273
|
+
|
274
|
+
def has_alpha_numeric_or_symbol?(str)
|
275
|
+
return false if is_empty?(str)
|
276
|
+
|
277
|
+
s = str.split("")
|
278
|
+
lalpha = ('a'..'z').to_a
|
279
|
+
ualpha = ('A'..'Z').to_a
|
280
|
+
num = ('0'..'9').to_a
|
281
|
+
sym = ('!'..'?').to_a
|
282
|
+
sym = sym.difference(num)
|
283
|
+
|
284
|
+
t1 = ((s & lalpha).length > 0)
|
285
|
+
t2 = ((s & ualpha).length > 0)
|
286
|
+
t3 = ((s & num).length > 0)
|
287
|
+
t4 = ((s & sym).length > 0)
|
288
|
+
|
289
|
+
[(t1 or t2 or t3 or t4), res = { has_lower_alpha: t1, has_upper_alpha: t2, has_num: t3, has_symbol: t4 }]
|
290
|
+
end
|
291
|
+
|
292
|
+
private
|
293
|
+
def logger
|
294
|
+
logger = Antrapol::ToolRack::Logger.instance.glogger
|
295
|
+
logger.tag = :pass_utils
|
296
|
+
logger
|
297
|
+
end
|
298
|
+
|
299
|
+
end
|
300
|
+
end
|
301
|
+
end
|
data/lib/toolrack/version.rb
CHANGED
data/lib/toolrack.rb
CHANGED
@@ -11,6 +11,7 @@ require_relative 'toolrack/condition_utils'
|
|
11
11
|
require_relative 'toolrack/process_utils'
|
12
12
|
require_relative 'toolrack/runtime_utils'
|
13
13
|
require_relative 'toolrack/data_conversion_utils'
|
14
|
+
require_relative 'toolrack/password_utils'
|
14
15
|
|
15
16
|
module Antrapol
|
16
17
|
module ToolRack
|
@@ -21,6 +22,20 @@ module Antrapol
|
|
21
22
|
end
|
22
23
|
|
23
24
|
ToolRack = Antrapol::ToolRack
|
24
|
-
|
25
|
+
TR = ToolRack
|
25
26
|
|
27
|
+
ToolRack::DataConvUtils = Antrapol::ToolRack::DataConversionUtils
|
28
|
+
TR::DataConvUtils = ToolRack::DataConvUtils
|
29
|
+
|
30
|
+
ToolRack::CondUtils = ToolRack::ConditionUtils
|
31
|
+
TR::CondUtils = ToolRack::ConditionUtils
|
32
|
+
|
33
|
+
ToolRack::PassUtils = ToolRack::PasswordUtils
|
34
|
+
TR::PassUtils = ToolRack::PasswordUtils
|
35
|
+
|
36
|
+
ToolRack::ExpUtils = ToolRack::ExceptionUtils
|
37
|
+
TR::ExpUtils = ToolRack::ExpUtils
|
38
|
+
|
39
|
+
ToolRack::RTUtils = ToolRack::RuntimeUtils
|
40
|
+
TR::RTUtils = ToolRack::RTUtils
|
26
41
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: toolrack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.9.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-09-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: tlogger
|
@@ -73,6 +73,7 @@ files:
|
|
73
73
|
- lib/toolrack/data_conversion_utils.rb
|
74
74
|
- lib/toolrack/exception_utils.rb
|
75
75
|
- lib/toolrack/global.rb
|
76
|
+
- lib/toolrack/password_utils.rb
|
76
77
|
- lib/toolrack/process_utils.rb
|
77
78
|
- lib/toolrack/runtime_utils.rb
|
78
79
|
- lib/toolrack/utils.rb
|