add xor function

This commit is contained in:
Hung-Chi Su 2015-11-07 20:33:28 +08:00
parent f342937ce3
commit 900d64ec85
1 changed files with 9 additions and 1 deletions

View File

@ -1,5 +1,13 @@
class String
require 'pp'
def ^( key )
b1 = self.unpack("C*")
b2 = key.unpack("C*")
mul = (b1.length.to_f / b2.length.to_f).ceil
b2 = b2 * mul
b1.zip(b2).map{ |a,b| a^b }.pack("C*")
end
def fmtstr(value, index, **options)
# initialize
bytes = 1