Class: CcavenuePayment::Crypto
- Inherits:
-
Object
- Object
- CcavenuePayment::Crypto
- Defined in:
- lib/ccavenue_payment/crypto.rb
Constant Summary collapse
- INIT_VECTOR =
(0..15).to_a.pack("C*")
Instance Attribute Summary collapse
-
#working_key ⇒ Object
readonly
Returns the value of attribute working_key.
Instance Method Summary collapse
- #decrypt(cipher_text) ⇒ Object
- #encrypt(plain_text) ⇒ Object
-
#initialize(working_key:) ⇒ Crypto
constructor
A new instance of Crypto.
Constructor Details
#initialize(working_key:) ⇒ Crypto
Returns a new instance of Crypto.
7 8 9 |
# File 'lib/ccavenue_payment/crypto.rb', line 7 def initialize(working_key:) @working_key = working_key end |
Instance Attribute Details
#working_key ⇒ Object (readonly)
Returns the value of attribute working_key.
3 4 5 |
# File 'lib/ccavenue_payment/crypto.rb', line 3 def working_key @working_key end |
Instance Method Details
#decrypt(cipher_text) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/ccavenue_payment/crypto.rb', line 21 def decrypt(cipher_text) secret_key = [Digest::MD5.hexdigest(working_key)].pack("H*") encrypted_text = [cipher_text].pack("H*") decipher = OpenSSL::Cipher.new('aes-128-cbc') decipher.decrypt decipher.key = secret_key decipher.iv = INIT_VECTOR (decipher.update(encrypted_text) + decipher.final).gsub(/\0+$/, '') end |
#encrypt(plain_text) ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/ccavenue_payment/crypto.rb', line 11 def encrypt(plain_text) secret_key = [Digest::MD5.hexdigest(working_key)].pack("H*") cipher = OpenSSL::Cipher.new('aes-128-cbc') cipher.encrypt cipher.key = secret_key cipher.iv = INIT_VECTOR encrypted_text = cipher.update(plain_text) + cipher.final (encrypted_text.unpack("H*")).first end |