Class: CcavenuePayment::Crypto

Inherits:
Object
  • Object
show all
Defined in:
lib/ccavenue_payment/crypto.rb

Constant Summary collapse

INIT_VECTOR =
(0..15).to_a.pack("C*")

Instance Attribute Summary collapse

Instance Method Summary collapse

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_keyObject (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