Class: TonglianRubySdk::Signer

Inherits:
Object
  • Object
show all
Defined in:
lib/tonglian-ruby-sdk.rb

Overview

To sign client request message and verify tonglian’s response message

Instance Method Summary collapse

Constructor Details

#initialize(private_path, private_passwd, public_path) ⇒ Signer

Returns a new instance of Signer.



62
63
64
65
66
# File 'lib/tonglian-ruby-sdk.rb', line 62

def initialize(private_path, private_passwd, public_path)
  @private_path = private_path
  @private_passwd = private_passwd
  @public_path = public_path
end

Instance Method Details

#sign(params) ⇒ Object



68
69
70
71
72
# File 'lib/tonglian-ruby-sdk.rb', line 68

def sign(params)
  message = make_sign_message(params)
  rsa = OpenSSL::PKey::RSA.new private_key
  Base64.strict_encode64(rsa.sign(OpenSSL::Digest.new('SHA256'), message))
end

#verify?(params, signature = nil) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
# File 'lib/tonglian-ruby-sdk.rb', line 74

def verify?(params, signature = nil)
  signature = params['sign'] if signature.nil? || signature.to_s.empty?
  params.delete('sign')
  message = make_verify_message(params)

  public_file = File.open(@public_path)
  public_key = OpenSSL::X509::Certificate.new(public_file).public_key.export
  rsa = OpenSSL::PKey::RSA.new(public_key)
  rsa.verify(OpenSSL::Digest.new('SHA256'), Base64.decode64(signature), message)
end