Class: SignedForm::HMAC

Inherits:
Object
  • Object
show all
Defined in:
lib/signed_form/hmac.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ HMAC

Returns a new instance of HMAC.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/signed_form/hmac.rb', line 7

def initialize(options = {})
  self.secret_key = options[:secret_key]

  if secret_key.nil? || secret_key.empty?
    if defined?(::Rails) and ::Rails.application.respond_to?(:secrets)
      self.secret_key = ::Rails.application.secrets.secret_key_base
    end
  end

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

Instance Attribute Details

#secret_keyObject

Returns the value of attribute secret_key.



5
6
7
# File 'lib/signed_form/hmac.rb', line 5

def secret_key
  @secret_key
end

Instance Method Details

#create(data) ⇒ Object



21
22
23
# File 'lib/signed_form/hmac.rb', line 21

def create(data)
  OpenSSL::HMAC.hexdigest OpenSSL::Digest::SHA1.new, secret_key, data
end

#verify(signature, data) ⇒ Object



25
26
27
# File 'lib/signed_form/hmac.rb', line 25

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