Module: Newebpay::NewebpayHelper

Defined in:
lib/newebpay/newebpay_helper.rb

Class Method Summary collapse

Class Method Details

.add_padding(data, block_size = 32) ⇒ Object



31
32
33
34
# 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) ⇒ Object



71
72
73
74
75
76
# 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) ⇒ Object

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



64
65
66
67
68
69
# 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) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# 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) ⇒ Object



41
42
43
44
# 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) ⇒ Object



36
37
38
39
# 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) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# 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) ⇒ Object

for mpg



10
11
12
13
# 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) ⇒ Object



15
16
17
18
# 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) ⇒ Object



78
79
80
81
82
83
84
85
# 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) ⇒ Object

for mpg



58
59
60
61
# 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) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# 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