dynarex-password 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/dynarex-password.rb +32 -4
- metadata +1 -1
data/lib/dynarex-password.rb
CHANGED
@@ -12,7 +12,12 @@ class DynarexPassword
|
|
12
12
|
super()
|
13
13
|
end
|
14
14
|
|
15
|
-
|
15
|
+
# if a fized size of 2 is passed in then each character will generate
|
16
|
+
# exactly 2 random alphanumeric characters
|
17
|
+
#
|
18
|
+
def generate_lookup(fixed_size=nil)
|
19
|
+
|
20
|
+
@fixed_size = fixed_size
|
16
21
|
@temp_list = []
|
17
22
|
@dynarex = Dynarex.new('codes/code(index,value)')
|
18
23
|
|
@@ -40,15 +45,38 @@ class DynarexPassword
|
|
40
45
|
|
41
46
|
string.join
|
42
47
|
end
|
48
|
+
|
49
|
+
# reverse_lookup can only be used with a lookup file which was generated
|
50
|
+
# with a fixed char length of 2
|
51
|
+
#
|
52
|
+
def reverse_lookup(password, file=nil)
|
53
|
+
|
54
|
+
if file then
|
55
|
+
dynarex = Dynarex.new file
|
56
|
+
elsif @dynarex then
|
57
|
+
dynarex = @dynarex
|
58
|
+
else
|
59
|
+
return 'please supply a lookup file'
|
60
|
+
end
|
61
|
+
|
62
|
+
h = dynarex.to_h.inject({}){|r, x| r.merge({x[:value] => x[:index]})}
|
63
|
+
|
64
|
+
string = password.split(//).each_slice(2).map do |chars|
|
65
|
+
h[chars.join]
|
66
|
+
end
|
67
|
+
|
68
|
+
string.join
|
69
|
+
end
|
43
70
|
|
44
71
|
def save(filepath) @dynarex.save filepath, pretty: true end
|
45
72
|
|
46
73
|
private
|
47
74
|
|
48
|
-
def get_random_chars(
|
49
|
-
|
50
|
-
newpass = Array.new(rand(size)+1, '').map{@chars[rand(@chars.size)]}.join
|
75
|
+
def get_random_chars(upper_size)
|
51
76
|
|
77
|
+
size = @fixed_size ? @fixed_size : rand(upper_size)+1
|
78
|
+
newpass = Array.new(size, '').map{@chars[rand(@chars.size)]}.join
|
79
|
+
puts 'newpass : ' + newpass.inspect
|
52
80
|
# return the encryption providing it doesn't already exist in the lookup table.
|
53
81
|
return !@temp_list.include?(newpass) ? newpass : get_random_chars(size)
|
54
82
|
|