Class: EventQ::SignatureProviders::Sha256SignatureProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/eventq_base/signature_providers/sha256_signature_provider.rb

Instance Method Summary collapse

Constructor Details

#initializeSha256SignatureProvider

Returns a new instance of Sha256SignatureProvider.



5
6
7
8
9
# File 'lib/eventq_base/signature_providers/sha256_signature_provider.rb', line 5

def initialize
  require 'openssl'
  require 'base64'
  @serializer = serialization_provider_manager.get_provider(EventQ::Configuration.serialization_provider)
end

Instance Method Details

#valid?(message:, secret:) ⇒ Boolean

This method is called to validate a message signature

Returns:

  • (Boolean)


19
20
21
22
# File 'lib/eventq_base/signature_providers/sha256_signature_provider.rb', line 19

def valid?(message:, secret:)
  signature = write(message: message, secret: secret)
  message.signature == signature
end

#write(message:, secret:) ⇒ Object

This method is called to create a signature for a message



12
13
14
15
16
# File 'lib/eventq_base/signature_providers/sha256_signature_provider.rb', line 12

def write(message:, secret:)
  json = @serializer.serialize(message.content)
  hash = OpenSSL::HMAC.digest('sha256', secret, json)
  Base64.encode64(hash)
end