Module: Codebot::Cryptography
- Defined in:
- lib/codebot/cryptography.rb
Overview
This module provides convenience methods for performing cryptographic operations.
Class Method Summary collapse
-
.generate_endpoint ⇒ String
Generates a random name for an integration endpoint.
-
.generate_secret(len = nil) ⇒ String
Generates a random webhook secret.
-
.valid_signature?(body, secret, signature) ⇒ Boolean
Verifies a webhook signature.
Class Method Details
.generate_endpoint ⇒ String
Generates a random name for an integration endpoint.
13 14 15 |
# File 'lib/codebot/cryptography.rb', line 13 def self.generate_endpoint SecureRandom.uuid end |
.generate_secret(len = nil) ⇒ String
Generates a random webhook secret.
20 21 22 |
# File 'lib/codebot/cryptography.rb', line 20 def self.generate_secret(len = nil) SecureRandom.base64(len || 32) end |
.valid_signature?(body, secret, signature) ⇒ Boolean
Verifies a webhook signature.
30 31 32 33 34 35 36 |
# File 'lib/codebot/cryptography.rb', line 30 def self.valid_signature?(body, secret, signature) return false if signature.nil? digest = OpenSSL::Digest.new 'sha1' good_signature = 'sha1=' + OpenSSL::HMAC.hexdigest(digest, secret, body) Rack::Utils.secure_compare good_signature, signature end |