From 900d64ec85ab35a33cc6e9ff0e74bfd12a76be82 Mon Sep 17 00:00:00 2001 From: Hung-Chi Su Date: Sat, 7 Nov 2015 20:33:28 +0800 Subject: [PATCH] add xor function --- lib/string_ext.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/string_ext.rb b/lib/string_ext.rb index 4228c84..4aa821f 100644 --- a/lib/string_ext.rb +++ b/lib/string_ext.rb @@ -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