Class: Noise::Pattern
- Inherits:
-
Object
- Object
- Noise::Pattern
- Defined in:
- lib/noise/pattern.rb
Direct Known Subclasses
DeferredPattern, OneWayPattern, PatternIK, PatternIN, PatternIX, PatternKK, PatternKN, PatternKX, PatternNK, PatternNN, PatternNX, PatternXK, PatternXN, PatternXX
Instance Attribute Summary collapse
-
#fallback ⇒ Object
readonly
Returns the value of attribute fallback.
-
#modifiers ⇒ Object
readonly
Returns the value of attribute modifiers.
-
#psk_count ⇒ Object
readonly
Returns the value of attribute psk_count.
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
Class Method Summary collapse
Instance Method Summary collapse
- #apply_pattern_modifiers ⇒ Object
-
#initialize(modifiers) ⇒ Pattern
constructor
A new instance of Pattern.
- #initiator_pre_messages ⇒ Object
- #one_way? ⇒ Boolean
-
#required_keypairs(initiator) ⇒ Object
initiator [Boolean].
- #required_keypairs_of_initiator ⇒ Object
- #required_keypairs_of_responder ⇒ Object
- #responder_pre_messages ⇒ Object
Constructor Details
#initialize(modifiers) ⇒ Pattern
Returns a new instance of Pattern.
26 27 28 29 30 31 32 |
# File 'lib/noise/pattern.rb', line 26 def initialize(modifiers) = [[], []] @tokens = [] @name = '' @psk_count = 0 @modifiers = modifiers end |
Instance Attribute Details
#fallback ⇒ Object (readonly)
Returns the value of attribute fallback.
15 16 17 |
# File 'lib/noise/pattern.rb', line 15 def fallback @fallback end |
#modifiers ⇒ Object (readonly)
Returns the value of attribute modifiers.
15 16 17 |
# File 'lib/noise/pattern.rb', line 15 def modifiers @modifiers end |
#psk_count ⇒ Object (readonly)
Returns the value of attribute psk_count.
15 16 17 |
# File 'lib/noise/pattern.rb', line 15 def psk_count @psk_count end |
#tokens ⇒ Object (readonly)
Returns the value of attribute tokens.
15 16 17 |
# File 'lib/noise/pattern.rb', line 15 def tokens @tokens end |
Class Method Details
.create(name) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/noise/pattern.rb', line 17 def self.create(name) pattern_set = name.scan(/([A-Z1]+)([^A-Z]*)/)&.first pattern = pattern_set&.first modifiers = pattern_set[1].split('+') class_name = "Noise::Pattern#{pattern}" klass = Object.const_get(class_name) klass.new(modifiers) end |
Instance Method Details
#apply_pattern_modifiers ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/noise/pattern.rb', line 34 def apply_pattern_modifiers @modifiers.each do |modifier| if modifier.start_with?('psk') index = modifier.gsub(/psk/, '').to_i raise Noise::Exceptions::PSKValueError if index / 2 > @tokens.size if index.zero? @tokens[0].insert(0, Token::PSK) else @tokens[index - 1] << Token::PSK end @psk_count += 1 elsif modifier == 'fallback' @fallback = true else raise Noise::Exceptions::UnsupportedModifierError end end end |
#initiator_pre_messages ⇒ Object
73 74 75 |
# File 'lib/noise/pattern.rb', line 73 def [0].dup end |
#one_way? ⇒ Boolean
81 82 83 |
# File 'lib/noise/pattern.rb', line 81 def one_way? false end |
#required_keypairs(initiator) ⇒ Object
initiator [Boolean]
55 56 57 |
# File 'lib/noise/pattern.rb', line 55 def required_keypairs(initiator) initiator ? required_keypairs_of_initiator : required_keypairs_of_responder end |
#required_keypairs_of_initiator ⇒ Object
59 60 61 62 63 64 |
# File 'lib/noise/pattern.rb', line 59 def required_keypairs_of_initiator required = [] required << :s if %w[K X I].include?(@name[0]) required << :rs if one_way? || @name[1] == 'K' required end |
#required_keypairs_of_responder ⇒ Object
66 67 68 69 70 71 |
# File 'lib/noise/pattern.rb', line 66 def required_keypairs_of_responder required = [] required << :rs if @name[0] == 'K' required << :s if one_way? || %w[K X].include?(@name[1]) required end |
#responder_pre_messages ⇒ Object
77 78 79 |
# File 'lib/noise/pattern.rb', line 77 def [1].dup end |