Module: Newebpay::Cipher
- Defined in:
- lib/newebpay/cipher.rb
Overview
The module for encrypt and decrypt trade info
Class Method Summary collapse
-
.decrypt(data) ⇒ String
Decrypt data.
-
.encrypt(data) ⇒ String
Encrypt data.
-
.strip_padding(data) ⇒ String
Remove Padding from NewebPay.
Class Method Details
.decrypt(data) ⇒ String
Decrypt data
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
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
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 |