class SmileIdentityCore::Signature

Public Class Methods

new(partner_id, api_key) click to toggle source
# File lib/smile-identity-core/signature.rb, line 4
def initialize(partner_id, api_key)
  @api_key = api_key
  @partner_id = partner_id.to_i
end

Public Instance Methods

confirm_sec_key(timestamp, sec_key) click to toggle source
# File lib/smile-identity-core/signature.rb, line 26
def confirm_sec_key(timestamp, sec_key)
  begin
    hash_signature = Digest::SHA256.hexdigest([@partner_id.to_i, timestamp].join(":"))
    encrypted = sec_key.split('|')[0]

    public_key = OpenSSL::PKey::RSA.new(Base64.decode64(@api_key))
    decrypted = public_key.public_decrypt(Base64.decode64(encrypted))

    return decrypted == hash_signature
  rescue => e
    raise e
  end
end
generate_sec_key(timestamp=Time.now.to_i) click to toggle source
# File lib/smile-identity-core/signature.rb, line 9
def generate_sec_key(timestamp=Time.now.to_i)
  begin
    @timestamp = timestamp

    hash_signature = Digest::SHA256.hexdigest([@partner_id.to_i, @timestamp].join(":"))
    public_key = OpenSSL::PKey::RSA.new(Base64.decode64(@api_key))
    @sec_key = [Base64.encode64(public_key.public_encrypt(hash_signature)), hash_signature].join('|')

    return {
      sec_key: @sec_key,
      timestamp: @timestamp
    }
  rescue => e
    raise e
  end
end