module Ronin::Support::Binary::BitFlip::String

Methods for bit-flipping Strings.

Public Class Methods

bit_flips(string) click to toggle source

Returns every bit flip of every byte in the string.

@return [Array<String>]

The bit-flipped strings.

@example bit-flip all bytes in the String:

Binary::BitFlip.bit_flips("foo")

@api public

@see String#bit_flips

# 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) { |new_string| ... } click to toggle source

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

@param [String] string

The string to bit flip.

@yield [string]

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

@yieldparam [String] string

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

@return [Enumerator]

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

@example bit-flip all bytes in the String:

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

@see String#each_bit_flip

# 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