Module: CryptBufferConcern::Xor

Included in:
CryptBuffer
Defined in:
lib/crypto-toolbox/crypt_buffer/concerns/xor.rb

Instance Method Summary collapse

Instance Method Details

#xor(input, expand_input: false) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/crypto-toolbox/crypt_buffer/concerns/xor.rb', line 19

def xor(input,expand_input: false)
  if expand_input
    xor_all_with(input)
  else
    xor_bytes(CryptBuffer(input).bytes)
  end
end

#xor_all_with(input) ⇒ Object



27
28
29
30
# File 'lib/crypto-toolbox/crypt_buffer/concerns/xor.rb', line 27

def xor_all_with(input)
  expanded = expand_bytes(CryptBuffer(input).bytes,self.bytes.length)
  xor_bytes(expanded)
end

#xor_at(input, pos) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/crypto-toolbox/crypt_buffer/concerns/xor.rb', line 3

def xor_at(input,pos)
  return self if input.nil? || (pos.abs > length)
  
  case input
  when Array
    # map our current data to xor all inputs with the given bytepos.
    # all other bytes are kept as they were
    tmp = bytes.map.with_index{|b,i| i == pos ? xor_multiple(b,input) : b }
    CryptBuffer(tmp)
  else
    tmp = bytes
    tmp[pos] = tmp[pos] ^ input
    CryptBuffer(tmp)
  end 
end

#xor_spaceObject



32
33
34
# File 'lib/crypto-toolbox/crypt_buffer/concerns/xor.rb', line 32

def xor_space
  xor(0x20,expand_input: true)
end