Migrate to test::unit. Start moving functionality into HSMR Module and mixin to key and component models to reduce duplication.

This commit is contained in:
Dan Milne
2011-11-24 15:43:47 +11:00
parent 1d899d0b8a
commit 3c620201c6
10 changed files with 289 additions and 71 deletions

36
lib/component.rb Normal file
View File

@@ -0,0 +1,36 @@
module HSMR
class Component
include HSMR
attr_reader :key
attr_reader :length
attr_reader :parity
def component
@key
end
def initialize(key=nil, length=DOUBLE)
## Should check for odd parity
if key.nil?
key = generate(length)
else
key=key.gsub(/ /,'')
#raise TypeError, "Component argument expected" unless other.is_a? Component
end
@key = key.unpack('a2'*(key.length/2)).map{|x| x.hex}.pack('c'*(key.length/2))
@length = @key.length
@key = @key
end
def xor(other)
other = Component.new(other) unless other.is_a? Component
raise TypeError, "Component argument expected" unless other.is_a? Component
@a = @key.unpack('C2'*(@key.length/2))
@b = other.component.unpack('C2'*(other.component.length/2))
result = @a.zip(@b).map {|x,y| x^y}.map {|z| z.to_s(16) }.join.upcase
Key.new(result)
end
end
end