Skip to content

Instantly share code, notes, and snippets.

@dmb2
Created May 28, 2015 20:58
Show Gist options
  • Save dmb2/c413d6ba437c8e1364ee to your computer and use it in GitHub Desktop.
Save dmb2/c413d6ba437c8e1364ee to your computer and use it in GitHub Desktop.
Xor two strings, assumes you've got a Converters class that converts the bytestring to hex first.
class CryptoTools
def self.xor_str(str_a,str_b)
if str_a.length != str_b.length
raise "Length of input strings doesn't match!"
end
str_a.bytes.zip(str_b.bytes).map{ |x,y| x^y }.pack("C*")
end
def self.hex_xor(hex_a,hex_b)
Converters.str_to_hex(xor_str(Converters.hex_to_bytes(hex_a),
Converters.hex_to_bytes(hex_b)))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment