Class: Faceless::Authcode

Inherits:
Object
  • Object
show all
Defined in:
lib/faceless/authcode.rb

Constant Summary collapse

KEY_LENGTH =
4

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, operation, expiry) ⇒ Authcode

Returns a new instance of Authcode.



10
11
12
13
14
# File 'lib/faceless/authcode.rb', line 10

def initialize string, operation, expiry
  @string = string
  @operation = operation || ''
  @expiry = expiry || 0
end

Class Method Details

.decode(string, expiry = 0) ⇒ Object



20
21
22
# File 'lib/faceless/authcode.rb', line 20

def self.decode string, expiry = 0
  new(string, 'DECODE', expiry).authcode
end

.encode(string, expiry = 0) ⇒ Object



16
17
18
# File 'lib/faceless/authcode.rb', line 16

def self.encode string, expiry = 0
  new(string, 'ENCODE', expiry).authcode
end

Instance Method Details

#authcodeObject



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
# File 'lib/faceless/authcode.rb', line 24

def authcode
  key_length = generate_encrypt_key.size

  string = determine_string
  string_ords = ords(string)
  string_length = string_ords.size

  result = ''
  box = (0..255).to_a

  random_key = 0.upto(255).map do |i|
    (generate_encrypt_key[i % key_length]).ord
  end

  j = i = 0
  while i < 256 do
    j = (j + box[i] + random_key[i]) % 256
    box[i], box[j] = box[j], box[i]
    i += 1
  end

  a = j = i = 0
  while i < string_length
    a = (a + 1) % 256
    j = (j + box[a]) % 256
    box[a], box[j] = box[j], box[a]
    result += (string_ords[i] ^ (box[(box[a] + box[j]) % 256])).chr
    i += 1
  end

  return_result result
end