Sha256: c6e0e0e5d4bfe76f43c161d028d46231deaf150645185e7b49a51413d55a6ce3

Contents?: true

Size: 966 Bytes

Versions: 1

Compression:

Stored size: 966 Bytes

Contents

require 'openssl'

module SignedForm
  module HMAC
    class << self
      attr_accessor :secret_key

      def create_hmac(data)
        if secret_key.nil? || secret_key.empty?
          raise Errors::NoSecretKey, "Please consult the README for instructions on creating a secret key"
        end

        OpenSSL::HMAC.hexdigest OpenSSL::Digest::SHA1.new, secret_key, data
      end

      def verify_hmac(signature, data)
        if secret_key.nil? || secret_key.empty?
          raise Errors::NoSecretKey, "Please consult the README for instructions on creating a secret key"
        end

        secure_compare OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA1.new, secret_key, data), signature
      end

      # After the Rack implementation
      def secure_compare(a, b)
        return false unless a.bytesize == b.bytesize

        l = a.unpack("C*")

        r, i = 0, -1
        b.each_byte { |v| r |= v ^ l[i+=1] }
        r == 0
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
signed_form-0.0.1.pre1 lib/signed_form/hmac.rb