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
# File 'lib/signed_form/hmac.rb', line 7

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

  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


15
16
17
# File 'lib/signed_form/hmac.rb', line 15

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

#verify(signature, data) ⇒ Object


19
20
21
# File 'lib/signed_form/hmac.rb', line 19

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