Class: ECHConfig::ECHConfigContents

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/ech_config/ech_config_contents.rb,
lib/ech_config/ech_config_contents.rb

Overview

typed: true frozen_string_literal: true

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.



23
24
25
26
27
28
29
30
31
# File 'lib/ech_config/ech_config_contents.rb', line 23

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

#extensionsObject (readonly)

Returns the value of attribute extensions.



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

def extensions
  @extensions
end

#key_configObject (readonly)

Returns the value of attribute key_config.



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

def key_config
  @key_config
end

#maximum_name_lengthObject (readonly)

Returns the value of attribute maximum_name_length.



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

def maximum_name_length
  @maximum_name_length
end

#public_nameObject (readonly)

Returns the value of attribute public_name.



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

def public_name
  @public_name
end

Class Method Details

.decode(octet) ⇒ Object



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
73
74
# File 'lib/ech_config/ech_config_contents.rb', line 45

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

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

  pn_len = octet.slice(1, 1)&.unpack1('C')
  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')
  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

#encodeObject



34
35
36
37
38
39
# File 'lib/ech_config/ech_config_contents.rb', line 34

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