Sha256: f1d6766b3684828fe958adec3b8baa8b3c4910bcec3cbbea846e0442236e5d43

Contents?: true

Size: 975 Bytes

Versions: 187

Compression:

Stored size: 975 Bytes

Contents

module Fog
  class HMAC

    def initialize(type, key)
      @key = key
      case type
      when 'sha1'
        setup_sha1
      when 'sha256'
        setup_sha256
      end
    end

    def sign(data)
      @signer.call(data)
    end

    private

    def setup_sha1
      @digest = OpenSSL::Digest::Digest.new('sha1')
      @signer = lambda do |data|
        OpenSSL::HMAC.digest(@digest, @key, data)
      end
    end

    def setup_sha256
      begin
        @digest = OpenSSL::Digest::Digest.new('sha256')
        @signer = lambda do |data|
          OpenSSL::HMAC.digest(@digest, @key, data)
        end
      rescue RuntimeError => error
        unless error.message == 'Unsupported digest algorithm (sha256).'
          raise error
        else
          require 'hmac-sha2'
          @hmac = ::HMAC::SHA256.new(@key)
          @signer = lambda do |data|
            @hmac.update(data)
            @hmac.digest
          end
        end
      end
    end

  end
end

Version data entries

187 entries across 187 versions & 23 rubygems

Version Path
cifrado-0.2.1 vendor/fog/lib/fog/core/hmac.rb
cifrado-0.2.0 vendor/fog/lib/fog/core/hmac.rb
fog-maestrodev-1.18.0.20131209091424 lib/fog/core/hmac.rb
fog-maestrodev-1.18.0.20131209090811 lib/fog/core/hmac.rb
fog-maestrodev-1.18.0.20131206115947 lib/fog/core/hmac.rb
fog-maestrodev-1.18.0.20131205181604 lib/fog/core/hmac.rb
fog-maestrodev-1.18.0.20131127194823 lib/fog/core/hmac.rb
fog-maestrodev-1.18.0.20131126183714 lib/fog/core/hmac.rb
fog-maestrodev-1.18.0.20131126122111 lib/fog/core/hmac.rb
fog-maestrodev-1.18.0.20131125111730 lib/fog/core/hmac.rb
fog-maestrodev-1.18.0.20131125083406 lib/fog/core/hmac.rb
fog-maestrodev-1.18.0.20131123105121 lib/fog/core/hmac.rb
fog-maestrodev-1.18.0.20131122203507 lib/fog/core/hmac.rb
fog-maestrodev-1.18.0.20131121075022 lib/fog/core/hmac.rb
fog-maestrodev-1.18.0.20131118164830 lib/fog/core/hmac.rb
fog-maestrodev-1.18.0.20131115184302 lib/fog/core/hmac.rb
fog-maestrodev-1.18.0.20131114200144 lib/fog/core/hmac.rb
gapinc-fog-1.12.1.2.1 lib/fog/core/hmac.rb
fog-maestrodev-1.18.0.20131112185232 lib/fog/core/hmac.rb
fog-maestrodev-1.18.0.20131111203459 lib/fog/core/hmac.rb