Class: DynarexPassword

Inherits:
Object
  • Object
show all
Defined in:
lib/dynarex-password.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDynarexPassword

Returns a new instance of DynarexPassword.



11
12
13
# File 'lib/dynarex-password.rb', line 11

def initialize()
  super()
end

Instance Attribute Details

#dxObject (readonly)

Returns the value of attribute dx.



9
10
11
# File 'lib/dynarex-password.rb', line 9

def dx
  @dx
end

Instance Method Details

#generate_lookup(fixed_size: nil) ⇒ Object

if a fized size of 2 is passed in then each character will generate

exactly 2 random alphanumeric characters


18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/dynarex-password.rb', line 18

def generate_lookup(fixed_size: nil)
  
  @fixed_size = fixed_size
  @temp_list = []
  @dx = Dynarex.new('codes/code(index,value)')

  chars =  (0..9).to_a  + Array.new(7) + ('A'..'Z').to_a \
                                             + Array.new(6) + ('a'..'z').to_a
  @chars = (0..9).to_a  + ('A'..'Z').to_a + ('a'..'z').to_a 

  chars.each do |char|
    @dx.create index: char, value: get_random_chars(2) if char
  end    
end

#lookup(weak_password, file = nil) ⇒ Object Also known as: encrypt



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dynarex-password.rb', line 33

def lookup(weak_password, file=nil)
  
  dx = if file then
    Dynarex.new file
  elsif @dx
    @dx
  else
    raise 'dynarex-password: please supply a lookup file'
  end
  
  chars = weak_password.split(//).map do |char|
    char[/[0-9A-Za-z]/] ? dx.records[char][:body][:value] : char
  end
  
  chars.join  
end

#reverse_lookup(password, file = nil) ⇒ Object Also known as: decrypt

reverse_lookup can only be used with a lookup file which was generated

with a fixed char length of 2


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dynarex-password.rb', line 55

def reverse_lookup(password, file=nil)
  
  dx = if file then
    Dynarex.new file
  elsif @dx
    @dx
  else
    raise 'dynarex-password: please supply a lookup file'
  end
  
  h = dx.to_h.inject({}){|r, x| r.merge({x[:value] => x[:index]})}

  password.split('-').map do |linex| 
    linex.split('_').map do |x|
      x.split(//).each_slice(2).map {|chars| h[chars.join]}.join 
    end.join '_'
  end.join '-'

end

#save(filepath) ⇒ Object



77
# File 'lib/dynarex-password.rb', line 77

def save(filepath)   @dx.save filepath end