dynarex-password 0.1.3 → 0.1.4

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.
Files changed (2) hide show
  1. data/lib/dynarex-password.rb +32 -4
  2. metadata +1 -1
@@ -12,7 +12,12 @@ class DynarexPassword
12
12
  super()
13
13
  end
14
14
 
15
- def generate_lookup()
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(size)
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
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: dynarex-password
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.3
5
+ version: 0.1.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - James Robertson