toolrack 0.9.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7a6d6ade6a80e5efb3499f23135144ee8c0f58b53aa98b453981e74cb279237c
4
- data.tar.gz: f665002e707b8c53aa855bedefa8c50a18fa9b4a3162cc987cdd0d000f33bd37
3
+ metadata.gz: 571e36485e6a124ee83052bd40d53b1612973e3f7636bf8432ae5d4d4f641f35
4
+ data.tar.gz: 7042d576dec084cc3eccbbb49e74c2ff8e7175d4e69c3ddb2fe9027b7a65d0cd
5
5
  SHA512:
6
- metadata.gz: eab1c799dc27d82fb55006baba5943bada9f9234d4e78266f8564638b86286129f5fd3938d42be729a62044411bcb686fc75bc59d127e106a6245d9fb85e11a8
7
- data.tar.gz: bf908f5e105b9e096b779de8c1e63d66057e8034c9a7eb996c6a01c1aca7e1e941f5cad68d6dedf329d26b49a6ec45fb121e33d2d452342c511ddc96f35b06bc
6
+ metadata.gz: 32a5ebec97858317a3a500db4e2bd315a80e67bee888b302fc45ff0aacd46749b77c25b9485d3faf6588fe0efbb1a72041a731366809dd8471fe73c1aa16437e
7
+ data.tar.gz: e76ec4875a11dbf84634e527a665492ec9665f55747ba6aaf5007b28cb92bd1ba682072c3186b218b87a8954f4623d64908ce0b4a406573b521fb62f36128b25
@@ -1,3 +1,18 @@
1
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2
+ # Version 2, December 2004
3
+ #
4
+ # Copyright (C) 2021 Chris Liaw <chrisliaw@antrapol.com>
5
+ # Author: Chris Liaw <chrisliaw@antrapol.com>
6
+ #
7
+ # Everyone is permitted to copy and distribute verbatim or modified
8
+ # copies of this license document, and changing it is allowed as long
9
+ # as the name is changed.
10
+ #
11
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
12
+ # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
13
+ #
14
+ # 0. You just DO WHAT THE FUCK YOU WANT TO.
15
+
1
16
 
2
17
  require 'securerandom'
3
18
 
@@ -6,17 +21,290 @@ module Antrapol
6
21
  module PasswordUtils
7
22
  include Antrapol::ToolRack::ConditionUtils
8
23
 
9
- def generate_random_password(length = 12)
24
+ # complexity
25
+ # 1 : lowercase alphabet
26
+ # 2 : lower + upper alphabet
27
+ # 3 : lower + upper + number
28
+ # 4 : lower + upper + number + symbol
29
+ def generate_random_password(length = 12, opts = { complexity: 4, enforce_quality: true })
30
+
10
31
  length = 12 if is_empty?(length)
11
32
  length = length.to_i
12
33
  length = 12 if length <= 0
13
34
 
14
- antropy = ('!'..'~').to_a
15
- antropy.sort_by { SecureRandom.random_number }.join[0...length]
35
+ logger.debug "Length : #{length}"
36
+
37
+ if not_empty?(opts)
38
+ complexity = opts[:complexity] || 4
39
+ enforce_quality = opts[:enforce_quality] || true
40
+ end
41
+
42
+ complexity = 4 if is_empty?(complexity)
43
+ complexity = 4 if complexity > 4
44
+ complexity = 1 if complexity < 1
45
+
46
+ logger.debug "Complexity : #{complexity}"
47
+
48
+ enforce_quality = true if is_empty?(enforce_quality) or not is_bool?(enforce_quality)
49
+
50
+ res = []
51
+ antropy = [("a".."z"),("A".."Z"),(0..9),("!".."?")]
52
+ selection = antropy[0..complexity-1].map { |s| s.to_a }.flatten
53
+
54
+ fres = nil
55
+ loop do
56
+
57
+ len = length
58
+ processed = 0
59
+ loop do
60
+ #res += selection.sample(length)
61
+ res += selection.sort_by { SecureRandom.random_number }
62
+ break if res.length >= length
63
+ end
64
+
65
+ fres = res[0...length].join
66
+
67
+ if enforce_quality
68
+ case complexity
69
+ when 1
70
+ if all_lowercase_alpha?(fres)
71
+ break
72
+ else
73
+ logger.debug "Failed lowercase alpha quality check. #{fres} - Regenerating..."
74
+ res.clear
75
+ end
76
+ when 2
77
+ st, dst = all_alpha?(fres)
78
+ if st
79
+ break
80
+ else
81
+ logger.debug "Failed all alpha quality check. #{dst} - Regenerating..."
82
+ res.clear
83
+ end
84
+ when 3
85
+ st, dst = all_alpha_numeric?(fres)
86
+ if st
87
+ break
88
+ else
89
+ logger.debug "Failed alpha numeric quality check. #{dst} - Regenerating"
90
+ res.clear
91
+ end
92
+ when 4
93
+ st, dst = all_alpha_numeric_and_symbol?(fres)
94
+ if st
95
+ break
96
+ else
97
+ logger.debug "Failed alpha numeric + symbol quality check. #{fres} / #{dst} - Regenerating"
98
+ res.clear
99
+ end
100
+ else
101
+ logger.debug "Unknown complexity?? #{complexity}"
102
+ break
103
+ end
104
+ else
105
+ break
106
+ end
107
+
108
+ end
109
+
110
+ fres
111
+
112
+ #length = 12 if is_empty?(length)
113
+ #length = length.to_i
114
+ #length = 12 if length <= 0
115
+
116
+ #antropy = ('!'..'~').to_a
117
+ #antropy.sort_by { SecureRandom.random_number }.join[0...length]
16
118
  end
17
119
  alias_method :gen_rand_pass, :generate_random_password
18
120
  alias_method :gen_pass, :generate_random_password
19
121
 
122
+ def all_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.difference(lalpha).length == 0)
129
+ end
130
+
131
+ def has_lowercase_alpha?(str)
132
+ return false if is_empty?(str) or not str.is_a?(String)
133
+
134
+ s = str.split("")
135
+ lalpha = ('a'..'z').to_a
136
+
137
+ (s & lalpha).length > 0
138
+ end
139
+
140
+ def all_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.difference(ualpha).length == 0
147
+ end
148
+
149
+ def has_uppercase_alpha?(str)
150
+ return false if is_empty?(str) or not str.is_a?(String)
151
+
152
+ s = str.split("")
153
+ ualpha = ('A'..'Z').to_a
154
+
155
+ (s & ualpha).length > 0
156
+ end
157
+
158
+ def all_alpha?(str)
159
+ return false if is_empty?(str) or not str.is_a?(String)
160
+
161
+ s = str.split("")
162
+ lalpha = ('a'..'z').to_a
163
+ ualpha = ('A'..'Z').to_a
164
+ num = ('0'..'9').to_a
165
+ sym = ('!'..'?').to_a
166
+
167
+ alpha = [lalpha, ualpha].flatten
168
+
169
+ t1 = (alpha.difference(s).length == 0)
170
+ t2 = (alpha.difference(s).length == 0)
171
+
172
+ t3 = (s & num).length > 0
173
+ t4 = (s & sym).length > 0
174
+
175
+ [!(t1 or t2 or t3 or t4), res = { has_lower_alpha: t1, has_upper_alpha: t2, has_num: t3, has_symbol: t4 }]
176
+ end
177
+
178
+ def has_alpha?(str)
179
+ return false if is_empty?(str) or not str.is_a?(String)
180
+
181
+ s = str.split("")
182
+ lalpha = ('a'..'z').to_a
183
+ ualpha = ('A'..'Z').to_a
184
+ alpha = [lalpha, ualpha].flatten
185
+
186
+ (alpha & s).length > 0
187
+ end
188
+
189
+ def has_number?(str)
190
+ return false if is_empty?(str)
191
+
192
+ str = str.to_s if not str.is_a?(String)
193
+
194
+ s = str.split("")
195
+ num = ('0'..'9').to_a
196
+
197
+ (s & num).length > 0
198
+
199
+ end
200
+
201
+ def all_number?(str)
202
+ return false if is_empty?(str)
203
+
204
+ str = str.to_s if not str.is_a?(String)
205
+
206
+ s = str.split("")
207
+ num = ('0'..'9').to_a
208
+
209
+ !(s.difference(num).length > 0)
210
+
211
+ end
212
+
213
+ def all_symbol?(str)
214
+ return false if is_empty?(str)
215
+
216
+ s = str.split("")
217
+ sym = ('!'..'?').to_a
218
+ !(s.difference(sym).length > 0)
219
+
220
+ end
221
+
222
+ def has_symbol?(str)
223
+ return false if is_empty?(str)
224
+
225
+ s = str.split("")
226
+ sym = ('!'..'?').to_a
227
+
228
+ p (s & sym)
229
+ (s & sym).length > 0
230
+ end
231
+
232
+
233
+ def all_alpha_numeric?(str)
234
+ return false if is_empty?(str)
235
+
236
+ s = str.split("")
237
+ lalpha = ('a'..'z').to_a
238
+ ualpha = ('A'..'Z').to_a
239
+ num = ('0'..'9').to_a
240
+ sym = ('!'..'?').to_a
241
+ sym = sym.difference(num)
242
+
243
+ t1 = ((s & lalpha).length > 0)
244
+ t2 = ((s & ualpha).length > 0)
245
+ t3 = ((s & num).length > 0)
246
+
247
+ t4 = ((s & sym).length > 0)
248
+
249
+ [(t1 and t2 and t3 and !t4), res = { has_lower_alpha: t1, has_upper_alpha: t2, has_num: t3, has_symbol: t4 }]
250
+ end
251
+
252
+ def has_alpha_numeric?(str)
253
+ return false if is_empty?(str)
254
+
255
+ s = str.split("")
256
+ lalpha = ('a'..'z').to_a
257
+ ualpha = ('A'..'Z').to_a
258
+ num = ('0'..'9').to_a
259
+ alphanum = [lalpha, ualpha, num].flatten
260
+
261
+ (s & alphanum).length > 0
262
+
263
+ end
264
+
265
+ def all_alpha_numeric_and_symbol?(str)
266
+ return false if is_empty?(str)
267
+
268
+ s = str.split("")
269
+ lalpha = ('a'..'z').to_a
270
+ ualpha = ('A'..'Z').to_a
271
+ num = ('0'..'9').to_a
272
+ sym = ('!'..'?').to_a
273
+ sym = sym.difference(num)
274
+
275
+ t1 = ((s & lalpha).length > 0)
276
+ t2 = ((s & ualpha).length > 0)
277
+ t3 = ((s & num).length > 0)
278
+ t4 = ((s & sym).length > 0)
279
+
280
+ [(t1 and t2 and t3 and t4), res = { has_lower_alpha: t1, has_upper_alpha: t2, has_num: t3, has_symbol: t4 }]
281
+ end
282
+
283
+ def has_alpha_numeric_or_symbol?(str)
284
+ return false if is_empty?(str)
285
+
286
+ s = str.split("")
287
+ lalpha = ('a'..'z').to_a
288
+ ualpha = ('A'..'Z').to_a
289
+ num = ('0'..'9').to_a
290
+ sym = ('!'..'?').to_a
291
+ sym = sym.difference(num)
292
+
293
+ t1 = ((s & lalpha).length > 0)
294
+ t2 = ((s & ualpha).length > 0)
295
+ t3 = ((s & num).length > 0)
296
+ t4 = ((s & sym).length > 0)
297
+
298
+ [(t1 or t2 or t3 or t4), res = { has_lower_alpha: t1, has_upper_alpha: t2, has_num: t3, has_symbol: t4 }]
299
+ end
300
+
301
+ private
302
+ def logger
303
+ logger = Antrapol::ToolRack::Logger.instance.glogger
304
+ logger.tag = :pass_utils
305
+ logger
306
+ end
307
+
20
308
  end
21
309
  end
22
310
  end
@@ -1,6 +1,6 @@
1
1
  module Antrapol
2
2
  module ToolRack
3
- VERSION = "0.9.0"
3
+ VERSION = "0.9.1"
4
4
  end
5
5
  end
6
6
 
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.9.0
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-09-08 00:00:00.000000000 Z
11
+ date: 2021-09-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tlogger