Sha256: 4c33f001299bd852ee540002429d3ab62620e2707fb41ad89c6143900b3224cb

Contents?: true

Size: 750 Bytes

Versions: 1

Compression:

Stored size: 750 Bytes

Contents

# frozen_string_literal: true

require 'openssl'
require 'digest/sha1'

module ApiSignature
  class Generator
    SPLITTER = '|'

    delegate :valid?, :expired?, :timestamp, to: :validator

    attr_reader :options

    def initialize(options = {})
      @options = options
    end

    def generate_signature(secret)
      hmac = OpenSSL::HMAC.digest(digest, secret, string_to_sign)
      Base64.encode64(hmac).chomp
    end

    private

    def validator
      Validator.new(options)
    end

    def digest
      OpenSSL::Digest::SHA256.new
    end

    def string_to_sign
      [
        options[:request_method],
        options[:path],
        options[:access_key],
        timestamp.to_i
      ].map(&:to_s).join(SPLITTER)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
api_signature-0.1.5 lib/api_signature/generator.rb