Module: Ronin::Support::Binary::BitFlip::String

Defined in:
lib/ronin/support/binary/bit_flip.rb

Overview

Methods for bit-flipping Strings.

Class Method Summary collapse

Class Method Details

.bit_flips(string) ⇒ Array<String>

Returns every bit flip of every byte in the string.

Examples:

bit-flip all bytes in the String:

Binary::BitFlip.bit_flips("foo")

Returns:

See Also:



161
162
163
# File 'lib/ronin/support/binary/bit_flip.rb', line 161

def self.bit_flips(string)
  each_bit_flip(string).to_a
end

.each_bit_flip(string) {|string| ... } ⇒ Enumerator

Enumerates over every bit flip of every byte in the string.

Examples:

bit-flip all bytes in the String:

Binary::BitFlip.each_bit_flip("foo") { |string| puts string }

Parameters:

  • string (String)

    The string to bit flip.

Yields:

  • (string)

    If a block is given, it will be passed each bit-flipped string.

Yield Parameters:

  • string (String)

    The String, but with one of it's bits flipped.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator object will be returned.

See Also:



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ronin/support/binary/bit_flip.rb', line 131

def self.each_bit_flip(string)
  return enum_for(__method__,string) unless block_given?

  bits = (0...8)

  string.each_byte.with_index do |byte,index|
    Integer.each_bit_flip(byte,bits) do |flipped_byte|
      new_string = string.dup
      new_string.force_encoding(Encoding::ASCII_8BIT)
      new_string.setbyte(index,flipped_byte)
      yield new_string
    end
  end

  return nil
end