Sha256: 2c9f976c6421b1298c40d354dc67e6e2e7894480e74d147358d012fba1b3dda9

Contents?: true

Size: 900 Bytes

Versions: 37

Compression:

Stored size: 900 Bytes

Contents

# -*- coding: binary -*-

module Rex::Text
  # XOR a string against a variable-length key
  #
  # @param key [String] XOR key
  # @param value [String] String to XOR
  # @return [String] XOR'd string
  def self.xor(key, value)
    unless key && value
      raise ArgumentError, 'XOR key and value must be supplied'
    end

    xor_key =
      case key
      when String
        if key.empty?
          raise ArgumentError, 'XOR key must not be empty'
        end

        key
      when Integer
        unless key.between?(0x00, 0xff)
          raise ArgumentError, 'XOR key must be between 0x00 and 0xff'
        end

        # Convert integer to string
        [key].pack('C')
      end

    # Get byte arrays for key and value
    xor_key   = xor_key.bytes
    xor_value = value.bytes

    # XOR value against cycled key
    xor_value.zip(xor_key.cycle).map { |v, k| v ^ k }.pack('C*')
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
rex-text-0.2.59 lib/rex/text/xor.rb
rex-text-0.2.57 lib/rex/text/xor.rb
rex-text-0.2.56 lib/rex/text/xor.rb
rex-text-0.2.55 lib/rex/text/xor.rb
rex-text-0.2.54 lib/rex/text/xor.rb
rex-text-0.2.53 lib/rex/text/xor.rb
rex-text-0.2.52 lib/rex/text/xor.rb
rex-text-0.2.51 lib/rex/text/xor.rb
rex-text-0.2.50 lib/rex/text/xor.rb
rex-text-0.2.49 lib/rex/text/xor.rb
rex-text-0.2.48 lib/rex/text/xor.rb
rex-text-0.2.47 lib/rex/text/xor.rb
rex-text-0.2.46 lib/rex/text/xor.rb
rex-text-0.2.45 lib/rex/text/xor.rb
rex-text-0.2.44 lib/rex/text/xor.rb
rex-text-0.2.43 lib/rex/text/xor.rb
rex-text-0.2.42 lib/rex/text/xor.rb
rex-text-0.2.41 lib/rex/text/xor.rb
rex-text-0.2.40 lib/rex/text/xor.rb
rex-text-0.2.39 lib/rex/text/xor.rb