Skip to content

Instantly share code, notes, and snippets.

@danhawkins
Created July 31, 2024 05:50
Show Gist options
  • Save danhawkins/fee72ed1da05dac5db4632b24f26356d to your computer and use it in GitHub Desktop.
Save danhawkins/fee72ed1da05dac5db4632b24f26356d to your computer and use it in GitHub Desktop.
module Webhooks
class HmacSignature
DIGEST = OpenSSL::Digest.new('sha1').freeze
def initialize(secret_token, payload_body)
@secret_token = secret_token
@payload_body = payload_body.to_s
end
def generate
return unless secret_token
hmac = OpenSSL::HMAC.hexdigest(DIGEST, secret_token, payload_body)
"sha1=#{hmac}"
end
def verify_signature(received_signature)
signature = 'sha1=' + OpenSSL::HMAC.hexdigest(DIGEST, secret_token, payload_body)
Rack::Utils.secure_compare(signature, received_signature) ? true : false
end
attr_reader :secret_token, :payload_body
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment