Module: Antrapol::ToolRack::PasswordUtils

Includes:
ConditionUtils
Defined in:
lib/toolrack/password_utils.rb

Defined Under Namespace

Classes: PasswordUtilsError

Instance Method Summary collapse

Instance Method Details

#all_alpha?(str) ⇒ Boolean

Returns:

  • (Boolean)


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/toolrack/password_utils.rb', line 149

def all_alpha?(str)
  return false if is_empty?(str) or not str.is_a?(String)

  s = str.split("")
  lalpha = ('a'..'z').to_a
  ualpha = ('A'..'Z').to_a
  num = ('0'..'9').to_a
  sym = ('!'..'?').to_a
  
  alpha = [lalpha, ualpha].flatten

  t1 = (alpha-s).length == 0
  t2 = (alpha-s).length == 0

  t3 = (s & num).length > 0
  t4 = (s & sym).length > 0
  
  [!(t1 or t2 or t3 or t4), res = { has_lower_alpha: t1, has_upper_alpha: t2, has_num: t3, has_symbol: t4 }]
end

#all_alpha_numeric?(str) ⇒ Boolean

Returns:

  • (Boolean)


224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/toolrack/password_utils.rb', line 224

def all_alpha_numeric?(str)
  return false if is_empty?(str)

  s = str.split("")
  lalpha = ('a'..'z').to_a
  ualpha = ('A'..'Z').to_a
  num = ('0'..'9').to_a
  sym = ('!'..'?').to_a
  sym = sym-num

  t1 = ((s & lalpha).length > 0)
  t2 = ((s & ualpha).length > 0)
  t3 = ((s & num).length > 0)

  t4 = ((s & sym).length > 0)

  [(t1 and t2 and t3 and !t4), res = { has_lower_alpha: t1, has_upper_alpha: t2, has_num: t3, has_symbol: t4 }]
end

#all_alpha_numeric_and_symbol?(str) ⇒ Boolean

Returns:

  • (Boolean)


256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/toolrack/password_utils.rb', line 256

def all_alpha_numeric_and_symbol?(str)
  return false if is_empty?(str)

  s = str.split("")
  lalpha = ('a'..'z').to_a
  ualpha = ('A'..'Z').to_a
  num = ('0'..'9').to_a
  sym = ('!'..'?').to_a
  sym = sym-num

  t1 = ((s & lalpha).length > 0)
  t2 = ((s & ualpha).length > 0)
  t3 = ((s & num).length > 0)
  t4 = ((s & sym).length > 0)

  [(t1 and t2 and t3 and t4), res = { has_lower_alpha: t1, has_upper_alpha: t2, has_num: t3, has_symbol: t4 }]
end

#all_lowercase_alpha?(str) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
116
117
118
119
120
# File 'lib/toolrack/password_utils.rb', line 113

def all_lowercase_alpha?(str)
  return false if is_empty?(str) or not str.is_a?(String)

  s = str.split("")
  lalpha = ('a'..'z').to_a

  (s-lalpha).length == 0
end

#all_number?(str) ⇒ Boolean

Returns:

  • (Boolean)


192
193
194
195
196
197
198
199
200
201
202
# File 'lib/toolrack/password_utils.rb', line 192

def all_number?(str)
  return false if is_empty?(str)

  str = str.to_s if not str.is_a?(String)

  s = str.split("")
  num = ('0'..'9').to_a

  !((s-num).length > 0)
  
end

#all_symbol?(str) ⇒ Boolean

Returns:

  • (Boolean)


204
205
206
207
208
209
210
211
# File 'lib/toolrack/password_utils.rb', line 204

def all_symbol?(str)
  return false if is_empty?(str)

  s = str.split("")
  sym = ('!'..'?').to_a
  !((s-sym).length > 0)
  
end

#all_uppercase_alpha?(str) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
134
135
136
137
138
# File 'lib/toolrack/password_utils.rb', line 131

def all_uppercase_alpha?(str)
  return false if is_empty?(str) or not str.is_a?(String)

  s = str.split("")
  ualpha = ('A'..'Z').to_a

  (s-ualpha).length == 0
end

#generate_random_password(length = 12, opts = { complexity: 4, enforce_quality: true }) ⇒ Object Also known as: gen_rand_pass, gen_pass

complexity 1 : lowercase alphabet 2 : lower + upper alphabet 3 : lower + upper + number 4 : lower + upper + number + symbol



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
# File 'lib/toolrack/password_utils.rb', line 16

def generate_random_password(length = 12, opts = { complexity: 4, enforce_quality: true })
 

  length = 12 if is_empty?(length)
  length = length.to_i
  length = 12 if length <= 0

  logger.debug "Length : #{length}"

  if not_empty?(opts)
    complexity = opts[:complexity] || 4
    
    raise PasswordUtilsError, "Password length too short but complexity too high. Not able generate the password with required complexity." if length <= complexity
    
    enforce_quality = opts[:enforce_quality] || true
  end

  complexity = 4 if is_empty?(complexity)
  complexity = 4 if complexity > 4
  complexity = 1 if complexity < 1

  logger.debug "Complexity : #{complexity}"

  enforce_quality = true if is_empty?(enforce_quality) or not is_bool?(enforce_quality)

  res = []
  antropy = [("a".."z"),("A".."Z"),(0..9),("!".."?")]
  selection = antropy[0..complexity-1].map { |s| s.to_a }.flatten

  fres = nil
  loop do

    len = length
    processed = 0
    loop do
      #res += selection.sample(length)
      res += selection.sort_by { SecureRandom.random_number }
      break if res.length >= length
    end

    fres = res[0...length].join

    if enforce_quality
      case complexity
      when 1
        if all_lowercase_alpha?(fres)
          break
        else
          logger.debug "Failed lowercase alpha quality check. #{fres} - Regenerating..."
          res.clear
        end
      when 2
        st, dst = all_alpha?(fres)
        if st
          break
        else
          logger.debug "Failed all alpha quality check. #{dst} - Regenerating..."
          res.clear
        end
      when 3
        st, dst = all_alpha_numeric?(fres)
        if st
          break
        else
          logger.debug "Failed alpha numeric quality check. #{dst} - Regenerating"
          res.clear
        end
      when 4
        st, dst = all_alpha_numeric_and_symbol?(fres)
        if st
          break
        else
          logger.debug "Failed alpha numeric + symbol quality check. #{fres} / #{dst} - Regenerating"
          res.clear
        end
      else
        logger.debug "Unknown complexity?? #{complexity}"
        break
      end
    else
      break
    end

  end

  fres

  #length = 12 if is_empty?(length)
  #length = length.to_i
  #length = 12 if length <= 0

  #antropy = ('!'..'~').to_a
  #antropy.sort_by { SecureRandom.random_number }.join[0...length]
end

#has_alpha?(str) ⇒ Boolean

Returns:

  • (Boolean)


169
170
171
172
173
174
175
176
177
178
# File 'lib/toolrack/password_utils.rb', line 169

def has_alpha?(str)
  return false if is_empty?(str) or not str.is_a?(String)

  s = str.split("")
  lalpha = ('a'..'z').to_a
  ualpha = ('A'..'Z').to_a
  alpha = [lalpha, ualpha].flatten

  (alpha & s).length > 0
end

#has_alpha_numeric?(str) ⇒ Boolean

Returns:

  • (Boolean)


243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/toolrack/password_utils.rb', line 243

def has_alpha_numeric?(str)
  return false if is_empty?(str)
  
  s = str.split("")
  lalpha = ('a'..'z').to_a
  ualpha = ('A'..'Z').to_a
  num = ('0'..'9').to_a
  alphanum = [lalpha, ualpha, num].flatten

  (s & alphanum).length > 0
  
end

#has_alpha_numeric_or_symbol?(str) ⇒ Boolean

Returns:

  • (Boolean)


274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/toolrack/password_utils.rb', line 274

def has_alpha_numeric_or_symbol?(str)
  return false if is_empty?(str)
  
  s = str.split("")
  lalpha = ('a'..'z').to_a
  ualpha = ('A'..'Z').to_a
  num = ('0'..'9').to_a
  sym = ('!'..'?').to_a
  sym = sym-num

  t1 = ((s & lalpha).length > 0)
  t2 = ((s & ualpha).length > 0)
  t3 = ((s & num).length > 0)
  t4 = ((s & sym).length > 0)
  
  [(t1 or t2 or t3 or t4), res = { has_lower_alpha: t1, has_upper_alpha: t2, has_num: t3, has_symbol: t4 }]
end

#has_lowercase_alpha?(str) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
125
126
127
128
129
# File 'lib/toolrack/password_utils.rb', line 122

def has_lowercase_alpha?(str)
  return false if is_empty?(str) or not str.is_a?(String)

  s = str.split("")
  lalpha = ('a'..'z').to_a

  (s & lalpha).length > 0
end

#has_number?(str) ⇒ Boolean

Returns:

  • (Boolean)


180
181
182
183
184
185
186
187
188
189
190
# File 'lib/toolrack/password_utils.rb', line 180

def has_number?(str)
  return false if is_empty?(str)

  str = str.to_s if not str.is_a?(String)

  s = str.split("")
  num = ('0'..'9').to_a

  (s & num).length > 0
  
end

#has_symbol?(str) ⇒ Boolean

Returns:

  • (Boolean)


213
214
215
216
217
218
219
220
221
# File 'lib/toolrack/password_utils.rb', line 213

def has_symbol?(str)
  return false if is_empty?(str)

  s = str.split("")
  sym = ('!'..'?').to_a
 
  p (s & sym)
  (s & sym).length > 0
end

#has_uppercase_alpha?(str) ⇒ Boolean

Returns:

  • (Boolean)


140
141
142
143
144
145
146
147
# File 'lib/toolrack/password_utils.rb', line 140

def has_uppercase_alpha?(str)
  return false if is_empty?(str) or not str.is_a?(String)

  s = str.split("")
  ualpha = ('A'..'Z').to_a

  (s & ualpha).length > 0
end