Class: ECHConfig::ECHConfigContents

Inherits:
Object
  • Object
show all
Defined in:
lib/ech_config/ech_config_contents.rb,
lib/ech_config/ech_config_contents.rb,
sig/generated/ech_config/ech_config_contents.rbs,
sig/generated/ech_config/ech_config_contents.rbs

Overview

rbs_inline: enabled

Defined Under Namespace

Classes: Extensions, HpkeKeyConfig

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_config, maximum_name_length, public_name, extensions) ⇒ ECHConfigContents

Returns a new instance of ECHConfigContents.

Parameters:



20
21
22
23
24
25
26
27
28
# File 'lib/ech_config/ech_config_contents.rb', line 20

def initialize(key_config,
               maximum_name_length,
               public_name,
               extensions)
  @key_config = key_config
  @maximum_name_length = maximum_name_length
  @public_name = public_name
  @extensions = extensions
end

Instance Attribute Details

#extensions ⇒ Object (readonly)

Returns the value of attribute extensions.

Returns:

  • (Object)


13
14
15
# File 'lib/ech_config/ech_config_contents.rb', line 13

def extensions
  @extensions
end

#key_config ⇒ Object (readonly)

Returns the value of attribute key_config.

Returns:

  • (Object)


13
14
15
# File 'lib/ech_config/ech_config_contents.rb', line 13

def key_config
  @key_config
end

#maximum_name_length ⇒ Object (readonly)

Returns the value of attribute maximum_name_length.

Returns:

  • (Object)


13
14
15
# File 'lib/ech_config/ech_config_contents.rb', line 13

def maximum_name_length
  @maximum_name_length
end

#public_name ⇒ Object (readonly)

Returns the value of attribute public_name.

Returns:

  • (Object)


13
14
15
# File 'lib/ech_config/ech_config_contents.rb', line 13

def public_name
  @public_name
end

Class Method Details

.decode(octet) ⇒ ECHConfig::ECHConfigContents

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity

Parameters:

  • octet (String)

Returns:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ech_config/ech_config_contents.rb', line 43

def self.decode(octet)
  key_config, octet = HpkeKeyConfig.decode(octet)
  octet ||= ''
  raise ::ECHConfig::DecodeError if octet.nil? || octet.length < 2

  maximum_name_length = octet.slice(0, 1)&.unpack1('C') #: Integer
  raise ::ECHConfig::DecodeError if maximum_name_length.nil?

  pn_len = octet.slice(1, 1)&.unpack1('C') #: Integer
  i = 2
  raise ::ECHConfig::DecodeError if i + pn_len > octet.length

  public_name = octet.slice(i, pn_len)
  raise ::ECHConfig::DecodeError if public_name.nil?

  i += pn_len
  raise ::ECHConfig::DecodeError if i + 2 > octet.length

  ex_len = octet.slice(i, 2)&.unpack1('n') #: Integer
  i += 2
  raise ::ECHConfig::DecodeError if i + ex_len != octet.length

  extensions = Extensions.store(octet.slice(i, ex_len) || '')
  new(
    key_config,
    maximum_name_length,
    public_name,
    extensions
  )
end

Instance Method Details

#encode ⇒ String

Returns:

  • (String)


31
32
33
34
35
36
# File 'lib/ech_config/ech_config_contents.rb', line 31

def encode
  @key_config.encode \
  + [@maximum_name_length].pack('C') \
  + @public_name.then { |s| [s.length].pack('C') + s } \
  + @extensions.load.then { |s| [s.length].pack('n') + s }
end