Class: Hitch::MCP::Internal::HmacIdentity

Inherits:
Object
  • Object
show all
Defined in:
app/models/hitch/mcp/internal/hmac_identity.rb

Overview

HMAC-SHA256 digest over canonical JSON identity components, shared by rate-limit keying and observation identity. Each caller keeps its own salt, component layout, and output shape.

Constant Summary collapse

MAX_COMPONENT_BYTES =
2_048

Class Method Summary collapse

Class Method Details

.component(value, invalid_message:) ⇒ Object



25
26
27
28
29
30
31
32
# File 'app/models/hitch/mcp/internal/hmac_identity.rb', line 25

def component(value, invalid_message:)
  unless value.is_a?(String) && value.valid_encoding? && !value.empty? &&
      value.bytesize <= MAX_COMPONENT_BYTES
    raise ArgumentError, invalid_message
  end

  value
end

.digest(salt:, components:, key_generator:, unavailable_message:) ⇒ Object



16
17
18
19
20
21
22
23
# File 'app/models/hitch/mcp/internal/hmac_identity.rb', line 16

def digest(salt:, components:, key_generator:, unavailable_message:)
  secret = key_generator.generate_key(salt, 32)
  unless secret.is_a?(String) && secret.bytesize == 32
    raise ArgumentError, unavailable_message
  end

  OpenSSL::HMAC.hexdigest("SHA256", secret, JSON.generate(components)).freeze
end