Class: ResponseEncryption::AsymmetricEncrypter

Inherits:
ActiveModelService show all
Defined in:
lib/response_encryption/asymmetric_encrypter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ActiveModelService

#valid?

Constructor Details

#initialize(options = {}) ⇒ AsymmetricEncrypter

Returns a new instance of AsymmetricEncrypter.



5
6
7
8
# File 'lib/response_encryption/asymmetric_encrypter.rb', line 5

def initialize(options={})
  validate(options)
  @public_key = OpenSSL::PKey::RSA.new(options[:public_key])
end

Instance Attribute Details

#encrypted_dataObject (readonly)

Returns the value of attribute encrypted_data.



3
4
5
# File 'lib/response_encryption/asymmetric_encrypter.rb', line 3

def encrypted_data
  @encrypted_data
end

#public_keyObject (readonly)

Returns the value of attribute public_key.



3
4
5
# File 'lib/response_encryption/asymmetric_encrypter.rb', line 3

def public_key
  @public_key
end

Instance Method Details

#encrypt(data, encode_data = true) ⇒ String

Returns with the encrypted and encoded information.

Parameters:

  • data (Object)

    which should respond to #to_s

  • encode_data (Boolean) (defaults to: true)

Returns:

  • (String)

    with the encrypted and encoded information



13
14
15
16
17
# File 'lib/response_encryption/asymmetric_encrypter.rb', line 13

def encrypt(data, encode_data = true)
  return data if data.blank?
  encrypted = public_key.public_encrypt(data.to_s)
  @encrypted_data = encode_data ? Base64.encode64(encrypted) : encrypted
end

#validate(options) ⇒ Object



19
20
21
# File 'lib/response_encryption/asymmetric_encrypter.rb', line 19

def validate(options)
  errors.add(:param_missing, 'You must to provide public_key option') if options[:public_key].blank?
end