Skip to content

Instantly share code, notes, and snippets.

@murachue
Forked from yoggy/xor_method_for_string.rb
Created March 7, 2011 10:25
Show Gist options
  • Save murachue/858348 to your computer and use it in GitHub Desktop.
Save murachue/858348 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
#
# Stringクラスにxor(^)を追加してみるテスト。主にCTF用?
#
class String
def ^(str)
sho = self
lon = str
sho, lon = lon, sho if sho.size > lon.size
(0...lon.size).inject("") {|r,i| r + (lon[i] ^ sho[i % sho.size]).chr}
end
def hexdump
(0...self.size).inject("") {|r,i| r + ("%02x "%self[i]) }
end
end
a = "\x01\x23\x45\x67\x89\xab\xcd\xef"
b = "\x76\x54\x32\x10"
c = a ^ b
puts c.hexdump # => "\x77\x77\x77\x77\xFF\xFF\xFF\xFF"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment