Class: Kojn::Crypto

Inherits:
Object
  • Object
show all
Defined in:
lib/kojn/crypto.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCrypto



9
10
11
12
13
14
# File 'lib/kojn/crypto.rb', line 9

def initialize
  self.access_token = Kojn.api_key
  self.crypt_method = 'AES-256-CFB'

  self.aes = OpenSSL::Cipher.new(self.crypt_method)
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



7
8
9
# File 'lib/kojn/crypto.rb', line 7

def access_token
  @access_token
end

#aesObject

Returns the value of attribute aes.



7
8
9
# File 'lib/kojn/crypto.rb', line 7

def aes
  @aes
end

#crypt_methodObject

Returns the value of attribute crypt_method.



7
8
9
# File 'lib/kojn/crypto.rb', line 7

def crypt_method
  @crypt_method
end

Class Method Details

.decrypt(key, data) ⇒ Object



46
47
48
# File 'lib/kojn/crypto.rb', line 46

def self.decrypt(key, data)
  Kojn::Crypto.new(key).decrypt_params(data)
end

.encrypt(key, data) ⇒ Object



42
43
44
# File 'lib/kojn/crypto.rb', line 42

def self.encrypt(key, data)
  Kojn::Crypto.new(key).encrypt_params(data)
end

Instance Method Details

#decrypt(data, iv) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/kojn/crypto.rb', line 20

def decrypt(data, iv)
  self.aes.decrypt
  self.aes.key = @access_token
  self.aes.iv = iv

  JSON.parse(aes.update(data) + aes.final)
end

#decrypt_params(params) ⇒ Object



16
17
18
# File 'lib/kojn/crypto.rb', line 16

def decrypt_params(params)
  return self.decrypt(Base64.decode64(params['payload']), params['iv'])
end

#encrypt(data, iv, key) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/kojn/crypto.rb', line 34

def encrypt(data, iv, key)
  @aes.encrypt
  @aes.key = key
  @aes.iv = iv

  @aes.update(data.to_json) + @aes.final
end

#encrypt_params(params, key) ⇒ Object



28
29
30
31
32
# File 'lib/kojn/crypto.rb', line 28

def encrypt_params(params, key)
  iv = rand.to_s[0..15]

  return {iv: iv, payload: Base64.encode64(encrypt(params, iv, key))}
end