class Kojn::Crypto

Attributes

access_token[RW]
aes[RW]
crypt_method[RW]

Public Class Methods

decrypt(key, data) click to toggle source
# File lib/kojn/crypto.rb, line 46
def self.decrypt(key, data)
  Kojn::Crypto.new(key).decrypt_params(data)
end
encrypt(key, data) click to toggle source
# File lib/kojn/crypto.rb, line 42
def self.encrypt(key, data)
  Kojn::Crypto.new(key).encrypt_params(data)
end
new() click to toggle source
# 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

Public Instance Methods

decrypt(data, iv) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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