Class: ResponseEncryption::AsymmetricEncrypter
- Inherits:
-
ActiveModelService
- Object
- ActiveModelService
- ResponseEncryption::AsymmetricEncrypter
- Defined in:
- lib/response_encryption/asymmetric_encrypter.rb
Instance Attribute Summary collapse
-
#encrypted_data ⇒ Object
readonly
Returns the value of attribute encrypted_data.
-
#public_key ⇒ Object
readonly
Returns the value of attribute public_key.
Instance Method Summary collapse
-
#encrypt(data, encode_data = true) ⇒ String
With the encrypted and encoded information.
-
#initialize(options = {}) ⇒ AsymmetricEncrypter
constructor
A new instance of AsymmetricEncrypter.
- #validate(options) ⇒ Object
Methods inherited from ActiveModelService
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(={}) validate() @public_key = OpenSSL::PKey::RSA.new([:public_key]) end |
Instance Attribute Details
#encrypted_data ⇒ Object (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_key ⇒ Object (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.
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() errors.add(:param_missing, 'You must to provide public_key option') if [:public_key].blank? end |