Sha256: 7521737d545201ff0904cccc5f94bf714710ef509bda18bafc3c45b9fa6346a9

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

module SmileIdentityCore
  class Signature

    def initialize(partner_id, api_key)
      @api_key = api_key
      @partner_id = partner_id
    end

    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('|')

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

    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))

        decrypted == hash_signature
      rescue => e
        raise e
      end
    end

    def generate_signature(timestamp=Time.now.to_s)
      hmac = OpenSSL::HMAC.new(@api_key, 'sha256')
      hmac.update(timestamp.to_s)
      hmac.update(@partner_id)
      hmac.update("sid_request")
      signature = Base64.strict_encode64(hmac.digest())
      {
        signature: signature,
        timestamp: timestamp.to_s
      }
    end

    def confirm_signature(timestamp, msg_signature)
      generate_signature(timestamp)[:signature] == msg_signature
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
smile-identity-core-1.2.1 lib/smile-identity-core/signature.rb
smile-identity-core-1.2.0 lib/smile-identity-core/signature.rb