E
Erik Veenstra
I want to do an XOR of two strings:
"a string".xor("another string")
It's not that hard to implement, but it's not fast either,
since it walks through the data string, byte-by-byte.
Any ideas? For example: "It's memory-hungry!". Any solutions?
gegroet,
Erik V. - http://www.erikveen.dds.nl/
----------------------------------------------------------------
class String
def xor(other)
if other.empty?
self
else
a1 = self.unpack("c*")
a2 = other.unpack("c*")
a2 *= 2 while a2.length < a1.length
a1.zip(a2).collect{|c1,c2| c1^c2}.pack("c*")
end
end
end
----------------------------------------------------------------
"a string".xor("another string")
It's not that hard to implement, but it's not fast either,
since it walks through the data string, byte-by-byte.
Any ideas? For example: "It's memory-hungry!". Any solutions?
gegroet,
Erik V. - http://www.erikveen.dds.nl/
----------------------------------------------------------------
class String
def xor(other)
if other.empty?
self
else
a1 = self.unpack("c*")
a2 = other.unpack("c*")
a2 *= 2 while a2.length < a1.length
a1.zip(a2).collect{|c1,c2| c1^c2}.pack("c*")
end
end
end
----------------------------------------------------------------