module Newebpay::NewebpayHelper

Public Class Methods

add_padding(data, block_size = 32) click to toggle source
# File lib/newebpay/newebpay_helper.rb, line 31
def self.add_padding(data, block_size = 32)
  pad = block_size - (data.length % block_size)
  data + (pad.chr * pad)
end
create_check_code(check_string) click to toggle source
# File lib/newebpay/newebpay_helper.rb, line 71
def self.create_check_code(check_string)
  # query_trade
  config = Newebpay.config
  encode_string = "HashIV=#{config.hash_iv}&#{check_string}&HashKey=#{config.hash_key}"
  Digest::SHA256.hexdigest(encode_string).upcase
end
create_check_value(check_string) click to toggle source

to do 加密方式混亂,待整合與重新命名

# File lib/newebpay/newebpay_helper.rb, line 64
def self.create_check_value(check_string)
  # query_trade
  config = Newebpay.config
  encode_string = "IV=#{config.hash_iv}&#{check_string}&Key=#{config.hash_key}"
  Digest::SHA256.hexdigest(encode_string).upcase
end
decrypt(key, iv, encrypted_data) click to toggle source
# File lib/newebpay/newebpay_helper.rb, line 46
def self.decrypt(key, iv, encrypted_data)
  encrypted_data = [encrypted_data].pack('H*')
  decipher = OpenSSL::Cipher::AES256.new(:CBC)
  decipher.decrypt
  decipher.padding = 0
  decipher.key = key
  decipher.iv = iv
  data = decipher.update(encrypted_data) + decipher.final
  strippadding data
end
decrypt_data(encrypted_data) click to toggle source
# File lib/newebpay/newebpay_helper.rb, line 41
def self.decrypt_data(encrypted_data)
  config = Newebpay.config
  decrypt config.hash_key, config.hash_iv, encrypted_data
end
decrypt_merchant_data(encrypted_data) click to toggle source
# File lib/newebpay/newebpay_helper.rb, line 36
def self.decrypt_merchant_data(encrypted_data)
  config = Newebpay.merchant_config
  decrypt config.hash_key, config.hash_iv, encrypted_data
end
encrypt(key, iv, data) click to toggle source
# File lib/newebpay/newebpay_helper.rb, line 20
def self.encrypt(key, iv, data)
  cipher = OpenSSL::Cipher::AES256.new(:CBC)
  cipher.encrypt
  cipher.padding = 0
  cipher.key = key
  cipher.iv = iv
  padding_data = add_padding(data)
  encrypted = cipher.update(padding_data) + cipher.final
  encrypted.unpack('H*').first
end
encrypt_data(data) click to toggle source

for mpg

# File lib/newebpay/newebpay_helper.rb, line 10
def self.encrypt_data(data)
  config = Newebpay.config
  encrypt config.hash_key, config.hash_iv, data
end
encrypt_merchant_data(data) click to toggle source
# File lib/newebpay/newebpay_helper.rb, line 15
def self.encrypt_merchant_data(data)
  config = Newebpay.merchant_config
  encrypt config.hash_key, config.hash_iv, data
end
sha256_encode(key, iv, trade_info, hash_iv_first = false) click to toggle source
# File lib/newebpay/newebpay_helper.rb, line 78
def self.sha256_encode(key, iv, trade_info, hash_iv_first = false)
  encode_string = if hash_iv_first
                    "HashIV=#{iv}&#{trade_info}&HashKey=#{key}"
                  else
                    "HashKey=#{key}&#{trade_info}&HashIV=#{iv}"
                  end
  Digest::SHA256.hexdigest(encode_string).upcase
end
sha256_encode_trade_info(trade_info) click to toggle source

for mpg

# File lib/newebpay/newebpay_helper.rb, line 58
def self.sha256_encode_trade_info(trade_info)
  config = Newebpay.config
  sha256_encode config.hash_key, config.hash_iv, trade_info
end
strippadding(data) click to toggle source
# File lib/newebpay/newebpay_helper.rb, line 87
def self.strippadding(data)
  slast = data[-1].ord
  slastc = slast.chr
  padding_index = /#{slastc}{#{slast}}/ =~ data
  if !padding_index.nil?
    data[0, padding_index]
  else
    false
  end
end