module Qtpay module Sign def self.generate(params, options = {}) params = Utils.stringify_keys(params) key = options[:server_key] || Qtpay.server_key string = params_to_string(params) sign(key, string) end def self.verify?(params, options = {}) params = Utils.stringify_keys(params) sign_type = params.delete('sign_type') sign = params.delete('sign') string = params_to_string(params) key = options[:server_key] || Qtpay.server_key sign == sign(key, string) end def self.sign(key, string) Digest::MD5.hexdigest("#{string}#{key}") end def self.params_to_string(params) params.sort.map { |item| item.join('=') }.join('&') end end end