Sha256: d2fd608a823fe5a7ec34813acd3f9c3eced27aa45d95f20d6c4a23795b44442b

Contents?: true

Size: 863 Bytes

Versions: 4

Compression:

Stored size: 863 Bytes

Contents

# coding: utf-8
module ChanPay
  module Sign
    module RSA

      def self.sign(key, hash)
        content = link_hash(hash)
        rsa = OpenSSL::PKey::RSA.new(key)
        Base64.strict_encode64(rsa.sign('sha1', content))
      end

      def self.verify?(key, hash, sign)
        content = link_hash(hash)
        rsa = OpenSSL::PKey::RSA.new(key)
        result = rsa.verify('sha1', Base64.strict_decode64(sign), content)
        puts "\nrsa verify:#{result}; 回过来的 sign 为:#{sign}\n" unless result
        result
      end

      private

      def self.link_hash(hash)
        hash.delete_if do |key, value|
          key == :Sign || key == :SignType || value.nil? || value == ''
        end
        values = []
        hash.sort.to_h.each{|k,v|
          values << "#{k}=#{v}"
        }

        values.join("&")
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
chan_pay-0.6.0 lib/chan_pay/sign/rsa.rb
chan_pay-0.4.0 lib/chan_pay/sign/rsa.rb
chan_pay-0.3.0 lib/chan_pay/sign/rsa.rb
chan_pay-0.2.0 lib/chan_pay/sign/rsa.rb