Module: CryptBufferConcern::Xor

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

Instance Method Summary collapse

Instance Method Details

#^(other) ⇒ Object



5
6
7
# File 'lib/crypto-toolbox/crypt_buffer/concerns/xor.rb', line 5

def ^(other)
  xor(other)
end

#xor(input, expand_input: false) ⇒ Object



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

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



34
35
36
37
# File 'lib/crypto-toolbox/crypt_buffer/concerns/xor.rb', line 34

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

#xor_at(input, pos) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/crypto-toolbox/crypt_buffer/concerns/xor.rb', line 9

def xor_at(input,pos)
  return self if input.nil? || (pos.abs > length)

  case input.respond_to?(:to_ary)
  when true
    # map our current data to xor all inputs with the given bytepos.
    # all other bytes are kept as they were
    abs_pos = normalize_pos(pos)
    tmp = bytes.map.with_index{|b,i| i == abs_pos ? xor_multiple(b,input.to_ary) : b }
    CryptBuffer(tmp)
  else
    tmp = bytes
    tmp[pos] = tmp[pos] ^ input
    CryptBuffer(tmp)
  end 
end

#xor_spaceObject



39
40
41
# File 'lib/crypto-toolbox/crypt_buffer/concerns/xor.rb', line 39

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