mirror of
https://github.com/dkam/hsmr.git
synced 2025-12-28 08:44:53 +00:00
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:
36
lib/component.rb
Normal file
36
lib/component.rb
Normal 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
|
||||
Reference in New Issue
Block a user