Class: TonglianRubySdk::Signer
- Inherits:
-
Object
- Object
- TonglianRubySdk::Signer
- Defined in:
- lib/tonglian-ruby-sdk.rb
Overview
To sign client request message and verify tonglian’s response message
Instance Method Summary collapse
-
#initialize(private_path, private_passwd, public_path) ⇒ Signer
constructor
A new instance of Signer.
- #sign(params) ⇒ Object
- #verify?(params, signature = nil) ⇒ Boolean
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) = (params) rsa = OpenSSL::PKey::RSA.new private_key Base64.strict_encode64(rsa.sign(OpenSSL::Digest.new('SHA256'), )) end |
#verify?(params, signature = nil) ⇒ 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') = (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), ) end |