lib/noise/pattern.rb in noise-ruby-0.2.0 vs lib/noise/pattern.rb in noise-ruby-0.3.0
- old
+ new
@@ -10,26 +10,55 @@
SS = 'ss'
PSK = 'psk'
end
class Pattern
- attr_reader :one_way, :tokens
+ attr_reader :one_way, :tokens, :modifiers
def self.create(name)
- class_name = "Noise::Pattern#{name}"
+ pattern_set = name.scan(/([A-Z]+)([^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
+ klass.new(modifiers)
end
- def initialize
+ def initialize(modifiers)
@pre_messages = [[], []]
@tokens = []
@name = ''
@one_way = false
@psk_count = 0
+ @modifiers = modifiers
end
+ def apply_pattern_modifiers
+ @modifiers.each do |modifier|
+ if modifier.start_with?('psk')
+ begin
+ index = modifier.gsub(/psk/, '').to_i
+ rescue
+ raise Noise::Exceptions::PSKValueError
+ end
+ # if index * 2 > @tokens.size
+ # raise PSKValueError
+ # end
+ if index == 0
+ @tokens[0].insert(0, Token::PSK)
+ else
+ @tokens[index - 1] << Token::PSK
+ end
+ @psk_count += 1
+ elsif modifier == 'fallback'
+ raise NotImplementedError
+ else
+ raise Noise::Exceptions::PSKValueError
+ end
+ end
+ end
+
# initiator [Boolean]
def required_keypairs(initiator)
initiator ? required_keypairs_of_initiator : required_keypairs_of_responder
end
@@ -55,140 +84,140 @@
@pre_messages[1].dup
end
end
class OneWayPattern < Pattern
- def initialize
- super
+ def initialize(modifiers)
+ super(modifiers)
@one_way = true
end
end
class PatternN < OneWayPattern
- def initialize
- super
+ def initialize(modifiers)
+ super(modifiers)
@name = 'N'
@pre_messages = [[], [Token::S]]
@tokens = [[Token::E, Token::ES]]
end
end
class PatternK < OneWayPattern
- def initialize
- super
+ def initialize(modifiers)
+ super(modifiers)
@name = 'K'
@pre_messages = [[Token::S], [Token::S]]
@tokens = [[Token::E, Token::ES, Token::SS]]
end
end
class PatternX < OneWayPattern
- def initialize
- super
+ def initialize(modifiers)
+ super(modifiers)
@name = 'X'
@pre_messages = [[], [Token::S]]
@tokens = [[Token::E, Token::ES, Token::S, Token::SS]]
end
end
class PatternNN < Pattern
- def initialize
- super
+ def initialize(modifiers)
+ super(modifiers)
@name = 'NN'
@pre_messages = []
@tokens = [[Token::E], [Token::E, Token::EE]]
end
end
class PatternKN < Pattern
- def initialize
- super
+ def initialize(modifiers)
+ super(modifiers)
@name = 'KN'
@pre_messages = [[Token::S], []]
@tokens = [[Token::E], [Token::E, Token::EE, Token::SE]]
end
end
class PatternNK < Pattern
- def initialize
- super
+ def initialize(modifiers)
+ super(modifiers)
@name = 'NK'
@pre_messages = [[], [Token::S]]
@tokens = [[Token::E, Token::ES], [Token::E, Token::EE]]
end
end
class PatternKK < Pattern
- def initialize
- super
+ def initialize(modifiers)
+ super(modifiers)
@name = 'KK'
@pre_messages = [[Token::S], [Token::S]]
@tokens = [[Token::E, Token::ES, Token::SS], [Token::E, Token::EE, Token::SE]]
end
end
class PatternNX < Pattern
- def initialize
- super
+ def initialize(modifiers)
+ super(modifiers)
@name = 'NX'
@tokens = [[Token::E], [Token::E, Token::EE, Token::S, Token::ES]]
end
end
class PatternKX < Pattern
- def initialize
- super
+ def initialize(modifiers)
+ super(modifiers)
@name = 'KX'
@pre_messages = [[Token::S], []]
@tokens = [[Token::E], [Token::E, Token::EE, Token::SE, Token::S, Token::ES]]
end
end
class PatternXN < Pattern
- def initialize
- super
+ def initialize(modifiers)
+ super(modifiers)
@name = 'XN'
@tokens = [[Token::E], [Token::E, Token::EE], [Token::S, Token::SE]]
end
end
class PatternIN < Pattern
- def initialize
- super
+ def initialize(modifiers)
+ super(modifiers)
@name = 'IN'
@tokens = [[Token::E, Token::S], [Token::E, Token::EE, Token::SE]]
end
end
class PatternXK < Pattern
- def initialize
- super
+ def initialize(modifiers)
+ super(modifiers)
@name = 'XK'
@pre_messages = [[], [Token::S]]
@tokens = [[Token::E, Token::ES], [Token::E, Token::EE], [Token::S, Token::SE]]
end
end
class PatternIK < Pattern
- def initialize
- super
+ def initialize(modifiers)
+ super(modifiers)
@name = 'IK'
@pre_messages = [[], [Token::S]]
@tokens = [[Token::E, Token::ES, Token::S, Token::SS], [Token::E, Token::EE, Token::SE]]
end
end
class PatternXX < Pattern
- def initialize
- super
+ def initialize(modifiers)
+ super(modifiers)
@name = 'XX'
@tokens = [[Token::E], [Token::E, Token::EE, Token::S, Token::ES], [Token::S, Token::SE]]
end
end
class PatternIX < Pattern
- def initialize
- super
+ def initialize(modifiers)
+ super(modifiers)
@name = 'IX'
@tokens = [[Token::E, Token::S], [Token::E, Token::EE, Token::SE, Token::S, Token::ES]]
end
end
end