Skip to content

Instantly share code, notes, and snippets.

@djcas9
Created April 7, 2010 00:16
Show Gist options
  • Save djcas9/358304 to your computer and use it in GitHub Desktop.
Save djcas9/358304 to your computer and use it in GitHub Desktop.
def xor(key)
if key.kind_of?(String)
kenum = key.bytes.cycle
else
kenum = key.to_s(16).bytes.cycle
end
self.to_s(16).bytes.map {|b| b ^ kenum.next }.pack("C*")
end
n = 1234.xor(0x34)
pp n #{=> }"\aP\001"
pp n.xor(0x34).hex #=> 1234
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment