Created
July 31, 2024 05:50
-
-
Save danhawkins/fee72ed1da05dac5db4632b24f26356d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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