Module: EvoleapLicensing::EncryptionHandler

Defined in:
lib/evoleap_licensing/encryption_handler.rb

Class Method Summary collapse

Class Method Details

.decrypt_response(body, aes_key, aes_iv) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/evoleap_licensing/encryption_handler.rb', line 29

def self.decrypt_response(body, aes_key, aes_iv)
  aes = OpenSSL::Cipher::AES.new(128, :CBC)
  aes.decrypt
  aes.key = aes_key
  aes.iv = aes_iv
  bytes = Base64.decode64(body)
  aes.update(bytes) + aes.final
end

.encrypt_payload(params, rsa_cipher) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/evoleap_licensing/encryption_handler.rb', line 9

def self.encrypt_payload(params, rsa_cipher)
  aes = OpenSSL::Cipher::AES.new(128, :CBC)
  aes.encrypt
  aes_key = aes.random_key
  iv = aes.random_iv

  encrypted_aes_key = rsa_cipher.public_encrypt(aes_key)
  encrypted_params = aes.update(params.to_json) + aes.final

  final = []
  final += encrypted_aes_key.bytes
  final.push(0) # padding
  final += iv.bytes
  final.push(0) # padding
  final += encrypted_params.bytes

  encoded = Base64.strict_encode64(final.pack("C*"))
  [aes_key, iv, encoded]
end

.rsa_key(public_key) ⇒ Object



38
39
40
# File 'lib/evoleap_licensing/encryption_handler.rb', line 38

def self.rsa_key(public_key)
  OpenSSL::PKey::RSA.new(public_key.strip)
end