Module: Newebpay::Cipher

Defined in:
lib/newebpay/cipher.rb

Overview

The module for encrypt and decrypt trade info

Since:

  • 0.1.0

Class Method Summary collapse

Class Method Details

.decrypt(data) ⇒ String

Decrypt data

Parameters:

  • data (String)

Returns:

  • (String)

    the decrypted data as string

Since:

  • 0.1.0



36
37
38
39
40
41
42
43
44
45
# File 'lib/newebpay/cipher.rb', line 36

def decrypt(data)
  cipher = OpenSSL::Cipher.new('aes-256-cbc').tap do |c|
    c.decrypt
    c.padding = 0
    c.key = Newebpay::Config.hash_key
    c.iv = Newebpay::Config.hash_iv
  end

  strip_padding(cipher.update([data].pack('H*')) + cipher.final)
end

.encrypt(data) ⇒ String

Encrypt data

Parameters:

  • data (String)

Returns:

  • (String)

    the encrypted data as hex

Since:

  • 0.1.0



19
20
21
22
23
24
25
26
27
# File 'lib/newebpay/cipher.rb', line 19

def encrypt(data)
  cipher = OpenSSL::Cipher.new('aes-256-cbc').tap do |c|
    c.encrypt
    c.key = Newebpay::Config.hash_key
    c.iv = Newebpay::Config.hash_iv
  end

  (cipher.update(data) + cipher.final).unpack1('H*')
end

.strip_padding(data) ⇒ String

Remove Padding from NewebPay

Parameters:

  • data (String)

Returns:

  • (String)

Since:

  • 0.1.0



54
55
56
57
58
# File 'lib/newebpay/cipher.rb', line 54

def strip_padding(data)
  padding = data[-1].ord
  padding_char = padding.chr
  data[/(.*)#{padding_char}{#{padding}}/, 1]
end