class String

Copyright © 2006-2024 Hal Brodigan (postmodern.mod3 at gmail.com)

ronin-support is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

ronin-support is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with ronin-support. If not, see <www.gnu.org/licenses/>.

Public Class Methods

ascii(string) click to toggle source

Creates a new ASCII encoding string.

@param [String] string

The string to convert to ASCII.

@return [String]

The new ASCII encoded string.

@api public

@since 1.0.0

# File lib/ronin/support/core_ext/string.rb, line 34
def self.ascii(string)
  string.encode(Encoding::ASCII_8BIT)
end

Public Instance Methods

^(key)
Alias for: xor
aes128_decrypt(**kwargs) click to toggle source

Decrypts the String using AES-128.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments for {Ronin::Support::Crypto.aes128_cipher}.

@option kwargs [:cbc, :cfb, :ofb, :ctr, Symbol] mode (:cbc)

The desired AES cipher mode.

@option kwargs [Symbol] :hash (:md5)

The algorithm to hash the `:password`.

@option kwargs [String] :key

The secret key to use.

@option kwargs [String] :password

The password for the cipher.

@option kwargs [String] :iv

The optional Initial Vector (IV).

@option kwargs [Integer] :padding

Sets the padding for the cipher.

@return [String]

The encrypted data.

@raise [ArgumentError]

Either the the `key:` or `password:` keyword argument must be given.

@example

"\x88\xA53\xE9|\xE2\x8E\xA0\xABv\xCF\x94\x17\xBB*\xC5".aes128_decrypt(password: 's3cr3t')
# => "top secret"

@since 1.0.0

# File lib/ronin/support/crypto/core_ext/string.rb, line 376
def aes128_decrypt(**kwargs)
  Ronin::Support::Crypto.aes128_decrypt(self,**kwargs)
end
aes128_encrypt(**kwargs) click to toggle source

Encrypts the String using AES-128.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments for {Ronin::Support::Crypto.aes128_cipher}.

@option kwargs [:cbc, :cfb, :ofb, :ctr, Symbol] mode (:cbc)

The desired AES cipher mode.

@option kwargs [Symbol] :hash (:md5)

The algorithm to hash the `:password`.

@option kwargs [String] :key

The secret key to use.

@option kwargs [String] :password

The password for the cipher.

@option kwargs [String] :iv

The optional Initial Vector (IV).

@option kwargs [Integer] :padding

Sets the padding for the cipher.

@return [String]

The encrypted data.

@raise [ArgumentError]

Either the the `key:` or `password:` keyword argument must be given.

@example

"top secret".aes128_encrypt(password: 's3cr3t')
# => "\x88\xA53\xE9|\xE2\x8E\xA0\xABv\xCF\x94\x17\xBB*\xC5"

@since 1.0.0

# File lib/ronin/support/crypto/core_ext/string.rb, line 336
def aes128_encrypt(**kwargs)
  Ronin::Support::Crypto.aes128_encrypt(self,**kwargs)
end
aes256_decrypt(**kwargs) click to toggle source

Decrypts the String using AES-256.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments for {Ronin::Support::Crypto.aes256_cipher}.

@option kwargs [:cbc, :cfb, :ofb, :ctr, Symbol] mode (:cbc)

The desired AES cipher mode.

@option kwargs [Symbol] :hash (:sha256)

The algorithm to hash the `:password`.

@option kwargs [String] :key

The secret key to use.

@option kwargs [String] :password

The password for the cipher.

@option kwargs [String] :iv

The optional Initial Vector (IV).

@option kwargs [Integer] :padding

Sets the padding for the cipher.

@return [String]

The encrypted data.

@raise [ArgumentError]

Either the the `key:` or `password:` keyword argument must be given.

@example

"\xF0[\x17\xDA\xA2\x82\x93\xF4\xB6s\xB5\xD8\x1F\xF2\xC6\\".aes256_decrypt(password: 's3cr3t')
# => "top secret"

@since 1.0.0

# File lib/ronin/support/crypto/core_ext/string.rb, line 456
def aes256_decrypt(**kwargs)
  Ronin::Support::Crypto.aes256_decrypt(self,**kwargs)
end
aes256_encrypt(**kwargs) click to toggle source

Encrypts the String using AES-256.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments for {Ronin::Support::Crypto.aes256_cipher}.

@option kwargs [:cbc, :cfb, :ofb, :ctr, Symbol] mode (:cbc)

The desired AES cipher mode.

@option kwargs [Symbol] :hash (:sha256)

The algorithm to hash the `:password`.

@option kwargs [String] :key

The secret key to use.

@option kwargs [String] :password

The password for the cipher.

@option kwargs [String] :iv

The optional Initial Vector (IV).

@option kwargs [Integer] :padding

Sets the padding for the cipher.

@return [String]

The encrypted data.

@raise [ArgumentError]

Either the the `key:` or `password:` keyword argument must be given.

@example

"top secret".aes256_encrypt(password: 's3cr3t')
# => "\xF0[\x17\xDA\xA2\x82\x93\xF4\xB6s\xB5\xD8\x1F\xF2\xC6\\"

@since 1.0.0

# File lib/ronin/support/crypto/core_ext/string.rb, line 416
def aes256_encrypt(**kwargs)
  Ronin::Support::Crypto.aes256_encrypt(self,**kwargs)
end
aes_decrypt(**kwargs) click to toggle source

Decrypts the String using AES.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments for {Ronin::Support::Crypto.aes_cipher}.

@option kwargs [Integer] :key_size

The desired key size in bits.

@option kwargs [:cbc, :cfb, :ofb, :ctr, Symbol] mode (:cbc)

The desired AES cipher mode.

@option kwargs [Symbol] :hash (:sha256)

The algorithm to hash the `:password`.

@option kwargs [String] :key

The secret key to use.

@option kwargs [String] :password

The password for the cipher.

@option kwargs [String] :iv

The optional Initial Vector (IV).

@option kwargs [Integer] :padding

Sets the padding for the cipher.

@return [String]

The encrypted data.

@raise [ArgumentError]

Either the the `key:` or `password:` keyword argument must be given.

@example

"\xF0[\x17\xDA\xA2\x82\x93\xF4\xB6s\xB5\xD8\x1F\xF2\xC6\\".aes_decrypt(key_size: 256, password: 's3cr3t')
# => "top secret"

@since 1.0.0

# File lib/ronin/support/crypto/core_ext/string.rb, line 296
def aes_decrypt(**kwargs)
  Ronin::Support::Crypto.aes_decrypt(self,**kwargs)
end
aes_encrypt(**kwargs) click to toggle source

Encrypts the String using AES.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments for {Ronin::Support::Crypto.aes_cipher}.

@option kwargs [Integer] :key_size

The desired key size in bits.

@option kwargs [:cbc, :cfb, :ofb, :ctr, Symbol] mode (:cbc)

The desired AES cipher mode.

@option kwargs [Symbol] :hash (:sha256)

The algorithm to hash the `:password`.

@option kwargs [String] :key

The secret key to use.

@option kwargs [String] :password

The password for the cipher.

@option kwargs [String] :iv

The optional Initial Vector (IV).

@option kwargs [Integer] :padding

Sets the padding for the cipher.

@return [String]

The encrypted data.

@raise [ArgumentError]

Either the the `key:` or `password:` keyword argument must be given.

@example

"top secret".aes_encrypt(key_size: 256, password: 's3cr3t')
# => "\xF0[\x17\xDA\xA2\x82\x93\xF4\xB6s\xB5\xD8\x1F\xF2\xC6\\"

@since 1.0.0

# File lib/ronin/support/crypto/core_ext/string.rb, line 253
def aes_encrypt(**kwargs)
  Ronin::Support::Crypto.aes_encrypt(self,**kwargs)
end
base16_decode() click to toggle source
Base16

decodes the String.

[Base16]: en.wikipedia.org/wiki/Base16

@return [String]

The Base16 decodes String.

@example

"54686520717569636b2062726f776e20666f78206a756d7073206f76657220746865206c617a7920646f67".base16_decode
# => "The quick brown fox jumps over the lazy dog"

@api public

@since 1.0.0

# File lib/ronin/support/encoding/base16/core_ext/string.rb, line 59
def base16_decode
  Ronin::Support::Encoding::Base16.decode(self)
end
base16_encode() click to toggle source
Base16

encodes the String.

[Base16]: en.wikipedia.org/wiki/Base16

@return [String]

The Base16 encoded String.

@example

"The quick brown fox jumps over the lazy dog".base16_encode
# => "54686520717569636b2062726f776e20666f78206a756d7073206f76657220746865206c617a7920646f67"

@api public

@since 1.0.0

# File lib/ronin/support/encoding/base16/core_ext/string.rb, line 39
def base16_encode
  Ronin::Support::Encoding::Base16.encode(self)
end
base32_decode() click to toggle source
Base32

decodes the String.

[Base32]: en.wikipedia.org/wiki/Base32

@return [String]

The Base32 decodes String.

@example

"KRUGKIDROVUWG2ZAMJZG653OEBTG66BANJ2W24DTEBXXMZLSEB2GQZJANRQXU6JAMRXWO===".base32_decode
# => "The quick brown fox jumps over the lazy dog"

@api public

@since 1.0.0

# File lib/ronin/support/encoding/base32/core_ext/string.rb, line 59
def base32_decode
  Ronin::Support::Encoding::Base32.decode(self)
end
base32_encode() click to toggle source
Base32

encodes the String.

[Base32]: en.wikipedia.org/wiki/Base32

@return [String]

The Base32 encoded String.

@example

"The quick brown fox jumps over the lazy dog".base32_encode
# => "KRUGKIDROVUWG2ZAMJZG653OEBTG66BANJ2W24DTEBXXMZLSEB2GQZJANRQXU6JAMRXWO==="

@api public

@since 1.0.0

# File lib/ronin/support/encoding/base32/core_ext/string.rb, line 39
def base32_encode
  Ronin::Support::Encoding::Base32.encode(self)
end
base36_decode() click to toggle source
Base36

decodes the String.

[Base36]: en.wikipedia.org/wiki/Base36

@return [Integer]

The Base36 decoded Integer.

@example

"rs".base36_decode
# => 1000

@api public

@since 1.1.0

# File lib/ronin/support/encoding/base36/core_ext/string.rb, line 39
def base36_decode
  Ronin::Support::Encoding::Base36.decode(self)
end
base62_decode() click to toggle source
Base62

decodes the String.

[Base62]: en.wikipedia.org/wiki/Base62

@return [Integer]

The Base62 decoded Integer.

@example

"LZ".base62_decode
# => 1337

@api public

@since 1.1.0

# File lib/ronin/support/encoding/base62/core_ext/string.rb, line 39
def base62_decode
  Ronin::Support::Encoding::Base62.decode(self)
end
base64_decode(mode: nil) click to toggle source
Base64

decodes a string.

[Base64]: en.wikipedia.org/wiki/Base64

@param [Symbol, nil] mode

The base64 mode to use. May be either:

* `nil`
* `:strict`
* `:url_safe`

@return [String]

The base64 decoded form of the string.

@note

`mode` argument is only available on Ruby >= 1.9.

@example

"aGVsbG8=\n".base64_decode
# => "hello"

@api public

# File lib/ronin/support/encoding/base64/core_ext/string.rb, line 72
def base64_decode(mode: nil)
  Ronin::Support::Encoding::Base64.decode(self, mode: mode)
end
base64_encode(mode: nil) click to toggle source
Base64

encodes a string.

[Base64]: en.wikipedia.org/wiki/Base64

@param [Symbol, nil] mode

The base64 mode to use. May be either:

* `nil`
* `:strict`
* `:url_safe`

@return [String]

The base64 encoded form of the string.

@example

"hello".base64_encode
# => "aGVsbG8=\n"

@api public

# File lib/ronin/support/encoding/base64/core_ext/string.rb, line 44
def base64_encode(mode: nil)
  Ronin::Support::Encoding::Base64.encode(self, mode: mode)
end
bit_flips() 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:

"foo".bit_flips

@api public

# File lib/ronin/support/binary/bit_flip/core_ext/string.rb, line 55
def bit_flips
  Ronin::Support::Binary::BitFlip::String.bit_flips(self)
end
Also aliased as: flip_bits
c_decode()
Alias for: c_unescape
c_encode() click to toggle source

C escapes every character in the String.

@return [String]

The C escaped String.

@example

"hello".c_encode
# => "\\x68\\x65\\x6c\\x6c\\x6f"

@see Ronin::Support::Encoding::C.encode

@api public

@since 1.0.0

# File lib/ronin/support/encoding/c/core_ext/string.rb, line 79
def c_encode
  Ronin::Support::Encoding::C.encode(self)
end
c_escape() click to toggle source

Escapes a String for C.

@return [String]

The C escaped String.

@example

"hello\nworld\n".c_escape
# => "hello\\nworld\\n"

@see Ronin::Support::Encoding::C.escape

@since 1.0.0

@api public

# File lib/ronin/support/encoding/c/core_ext/string.rb, line 39
def c_escape
  Ronin::Support::Encoding::C.escape(self)
end
c_string() click to toggle source

Converts the String into a C string.

@return [String]

@example

"hello\nworld\n".c_string
# => "\"hello\\nworld\\n\""

@see Ronin::Support::Encoding::C.quote

@since 1.0.0

@api public

# File lib/ronin/support/encoding/c/core_ext/string.rb, line 100
def c_string
  Ronin::Support::Encoding::C.quote(self)
end
c_unescape() click to toggle source

Unescapes a C escaped String.

@return [String]

The unescaped C String.

@example

"\\x68\\x65\\x6c\\x6c\\x6f\\x20\\x77\\x6f\\x72\\x6c\\x64".c_unescape
# => "hello world"

@see Ronin::Support::Encoding::C.unescape

@since 1.0.0

@api public

# File lib/ronin/support/encoding/c/core_ext/string.rb, line 59
def c_unescape
  Ronin::Support::Encoding::C.unescape(self)
end
Also aliased as: c_decode
c_unquote() click to toggle source

Removes the quotes an unescapes a C string.

@return [String]

The un-quoted String if the String begins and ends with quotes, or the
same String if it is not quoted.

@example

"\"hello\\nworld\"".c_unquote
# => "hello\nworld"

@see Ronin::Support::Encoding::C.unquote

@since 1.0.0

@api public

# File lib/ronin/support/encoding/c/core_ext/string.rb, line 121
def c_unquote
  Ronin::Support::Encoding::C.unquote(self)
end
common_prefix(other) click to toggle source

The common prefix of the string and the specified other string.

@param [String] other

The other String to compare against.

@return [String]

The common prefix between the two Strings.

@api public

# File lib/ronin/support/core_ext/string.rb, line 169
def common_prefix(other)
  min_length = [length, other.length].min

  min_length.times do |i|
    if self[i] != other[i]
      return self[0,i]
    end
  end

  return self[0,min_length]
end
common_suffix(other) click to toggle source

Finds the common suffix of the string and the specified other string.

@param [String] other

The other String to compare against.

@return [String]

The common suffix of the two Strings.

@since 0.2.0

@api public

# File lib/ronin/support/core_ext/string.rb, line 194
def common_suffix(other)
  min_length = [length, other.length].min

  (min_length - 1).times do |i|
    index       = (length - i - 1)
    other_index = (other.length - i - 1)

    if self[index] != other[other_index]
      return self[(index + 1)..]
    end
  end

  return ''
end
decrypt(cipher,**kwargs) click to toggle source

Decrypts the String.

@param [String] cipher

The cipher to use.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments for {Ronin::Support::Crypto.cipher}.

@option kwargs [Symbol] :hash (:sha1)

The algorithm to hash the `:password`.

@option kwargs [String] :key

The secret key to use.

@option kwargs [String] :password

The password for the cipher.

@option kwargs [String] :iv

The optional Initial Vector (IV).

@option kwargs [Integer] :padding

Sets the padding for the cipher.

@return [String]

The decrypted String.

@example

"\xF0[\x17\xDA\xA2\x82\x93\xF4\xB6s\xB5\xD8\x1F\xF2\xC6\\".decrypt('aes-256-cbc', password: 's3cr3t')
# => "top secret"

@see rubydoc.info/stdlib/openssl/OpenSSL/Cipher

@since 0.6.0

@api public

# File lib/ronin/support/crypto/core_ext/string.rb, line 208
def decrypt(cipher,**kwargs)
  Ronin::Support::Crypto.decrypt(self, cipher:    cipher,
                                       direction: :decrypt,
                                       **kwargs)
end
each_bit_flip(&block) click to toggle source

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

@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:

"foo".each_bit_flip { |string| puts string }

@api public

# File lib/ronin/support/binary/bit_flip/core_ext/string.rb, line 40
def each_bit_flip(&block)
  Ronin::Support::Binary::BitFlip::String.each_bit_flip(self,&block)
end
each_homoglyph(**kwargs,&block) click to toggle source

Enumerates over every homoglyph variation of the String.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [:ascii, :greek, :cyrillic, :punctuation, :latin_numbers,

              :full_width, nil] char_set
The character set to use.

@yield [homoglyph]

The given block will be passed each homoglyph variation of the String.

@yieldparam [String] homoglyph

A variation of the String.

@return [Enumerator]

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

@example

"microsoft".each_homoglyph do |homoglyph|
  # ...
end

@see Ronin::Support::Text::Homoglyph.each_substitution

@api public

@since 1.0.0

# File lib/ronin/support/text/homoglyph/core_ext/string.rb, line 86
def each_homoglyph(**kwargs,&block)
  Ronin::Support::Text::Homoglyph.each_substitution(self,**kwargs,&block)
end
each_substring(min=1,&block) click to toggle source

Enumerates over every sub-string within the string.

@param [Integer] min

Minimum length of each sub-string.

@yield [substring,(index)]

The given block will receive every sub-string contained within the
string. If the block accepts two arguments, then the index of
the sub-string will also be passed in.

@yieldparam [String] substring

A sub-string from the string.

@yieldparam [Integer] index

The optional index of the sub-string.

@return [String]

The original string

@example

"hello".each_substring(3).to_a
# => ["hel", "hell", "hello", "ell", "ello", "llo"]

@api public

# File lib/ronin/support/core_ext/string.rb, line 92
def each_substring(min=1,&block)
  return enum_for(__method__,min) unless block

  (0..(length - min)).each do |i|
    ((i + min)..length).each do |j|
      sub_string = self[i...j]

      if block.arity == 2
        block.call(sub_string,i)
      else
        block.call(sub_string)
      end
    end
  end

  return self
end
each_typo(**kwargs,&block) click to toggle source

Enumerates over every typo mistake for the String.

@param [Hash{Symbol => Boolean}] kwargs

Additional keyword arguments.

@option kwargs [Boolean] omit

Enables/disables omission of repeated characters.

@option kwargs [Boolean] repeat

Enables/disables repeatition of single characters.

@option kwargs [Boolean] swap

Enables/disables swapping of certain common character pairs.

@option kwargs [Boolean] suffix

Enables/disables changing the suffixes of words.

@yield [typoed]

If a block is given, it will be passed each possible typo of the
original String.

@yieldparam [String]

A modified version of the original String.

@return [Enumerator]

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

@example

"consciousness".each_typo do |typo|
  # ...
end

@see Ronin::Support::Text::Typo.each_substitution

@api public

@since 1.0.0

# File lib/ronin/support/text/typo/core_ext/string.rb, line 97
def each_typo(**kwargs,&block)
  Ronin::Support::Text::Typo.each_substitution(self,**kwargs,&block)
end
each_unique_substring(min=1,&block) click to toggle source

Enumerates over the unique sub-strings contained in the string.

@param [Integer] min

Minimum length of each unique sub-string.

@yield [substring,(index)]

The given block will receive every unique sub-string contained
within the string. If the block accepts two arguments, then the
index of the unique sub-string will also be passed in.

@yieldparam [String] substring

A unique sub-string from the string.

@yieldparam [Integer] index

The optional index of the unique sub-string.

@return [String]

The original string

@example

"xoxo".each_unique_substring(2).to_a
# => ["xo", "xox", "xoxo", "ox", "oxo"]

@see each_substring

@api public

# File lib/ronin/support/core_ext/string.rb, line 138
def each_unique_substring(min=1,&block)
  return enum_for(__method__,min) unless block

  unique_strings = {}

  each_substring(min) do |sub_string,index|
    unless unique_strings.has_key?(sub_string)
      unique_strings[sub_string] = index

      if block.arity == 2
        block.call(sub_string,index)
      else
        block.call(sub_string)
      end
    end
  end

  return self
end
encode_bytes(include: nil, exclude: nil) { |b| ... } click to toggle source

Creates a new String by formatting each byte.

@param [Enumerable<Integer, String>] include

The bytes to format.

@param [Enumerable<Integer, String>] exclude

The bytes not to format.

@yield [byte]

The block which will return the formatted version of each byte
within the String.

@yieldparam [Integer] byte

The byte to format.

@return [String]

The formatted version of the String.

@example

"hello".encode_bytes { |b| "%x" % b }
# => "68656c6c6f"

@api public

# File lib/ronin/support/encoding/core_ext/string.rb, line 48
def encode_bytes(include: nil, exclude: nil)
  included  = (Chars::CharSet.new(*include) if include)
  excluded  = (Chars::CharSet.new(*exclude) if exclude)
  formatted = String.new(encoding: ::Encoding::UTF_8)

  each_byte do |b|
    formatted << if (included.nil? || included.include_byte?(b)) &&
                    (excluded.nil? || !excluded.include_byte?(b))
                   yield(b)
                 else
                   b
                 end
  end

  return formatted
end
encode_chars(include: nil, exclude: nil) { |c| ... } click to toggle source

Creates a new String by formatting each character.

@param [Enumerable<Integer, String>] include

The bytes to format.

@param [Enumerable<Integer, String>] exclude

The bytes not to format.

@yield [char]

The block which will return the formatted version of each character
within the String.

@yieldparam [String] char

The character to format.

@return [String]

The formatted version of the String.

@example

"hello".encode_chars { |c| c * 3 }
# => "hhheeellllllooo"

@api public

# File lib/ronin/support/encoding/core_ext/string.rb, line 90
def encode_chars(include: nil, exclude: nil)
  included  = (Chars::CharSet.new(*include) if include)
  excluded  = (Chars::CharSet.new(*exclude) if exclude)
  formatted = String.new(encoding: ::Encoding::UTF_8)

  each_char do |c|
    formatted << if (included.nil? || included.include_char?(c)) &&
                    (excluded.nil? || !excluded.include_char?(c))
                   yield(c)
                 else
                   c
                 end
  end

  return formatted
end
encrypt(cipher,**kwargs) click to toggle source

Encrypts the String.

@param [String] cipher

The cipher to use.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments for {Ronin::Support::Crypto.cipher}.

@option kwargs [Symbol] :hash (:sha1)

The algorithm to hash the `:password`.

@option kwargs [String] :key

The secret key to use.

@option kwargs [String] :password

The password for the cipher.

@option kwargs [String] :iv

The optional Initial Vector (IV).

@option kwargs [Integer] :padding

Sets the padding for the cipher.

@return [String]

The encrypted String.

@example

"top secret".encrypt('aes-256-cbc', password: 's3cr3t')
# => "\xF0[\x17\xDA\xA2\x82\x93\xF4\xB6s\xB5\xD8\x1F\xF2\xC6\\"

@see rubydoc.info/stdlib/openssl/OpenSSL/Cipher

@since 0.6.0

@api public

# File lib/ronin/support/crypto/core_ext/string.rb, line 165
def encrypt(cipher,**kwargs)
  Ronin::Support::Crypto.encrypt(self, cipher:    cipher,
                                       direction: :encrypt,
                                       **kwargs)
end
entropy(**kwargs) click to toggle source

Calculates the entropy for the given string.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [Integer] :base (2)

The base to calculate the entropy for.

@return [Float]

The entropy for the string.

@see Ronin::Support::Text::Entropy.calculate

@since 1.0.0

@api public

# File lib/ronin/support/text/entropy/core_ext/string.rb, line 41
def entropy(**kwargs)
  Ronin::Support::Text::Entropy.calculate(self,**kwargs)
end
escape()
Alias for: ruby_escape
flip_bits()
Alias for: bit_flips
gunzip() click to toggle source

Gzip uncompresses the string.

@return [String]

The gunzipped version of the string.

@example

"\x1F\x8B\b\x00K\x05\x8Fb\x00\x03\xCBH\xCD\xC9\xC9W(\xCF/\xCAI\xE1\x02\x00-;\b\xAF\f\x00\x00\x00".gunzip
# => "hello world\n"

@api public

@since 1.0.0

# File lib/ronin/support/compression/core_ext/string.rb, line 71
def gunzip
  gz = Ronin::Support::Compression::Gzip::Reader.new(self)

  return gz.read
end
gzip() click to toggle source

Gzip compresses the string.

@return [String]

The gzipped version of the string.

@example

"hello world\n".gzip
# => "\x1F\x8B\b\x00K\x05\x8Fb\x00\x03\xCBH\xCD\xC9\xC9W(\xCF/\xCAI\xE1\x02\x00-;\b\xAF\f\x00\x00\x00"

@api public

@since 1.0.0

# File lib/ronin/support/compression/core_ext/string.rb, line 91
def gzip
  buffer = StringIO.new(encoding: Encoding::ASCII_8BIT)

  Ronin::Support::Compression::Gzip::Writer.wrap(buffer) do |gz|
    gz.write(self)
  end

  return buffer.string
end
hex_decode() click to toggle source

Hex-decodes the String.

@return [String]

The hex decoded version of the String.

@example

"68656C6C6F".hex_decode
# => "hello"

@see Ronin::Support::Encoding::Hex.decodde

# File lib/ronin/support/encoding/hex/core_ext/string.rb, line 53
def hex_decode
  Ronin::Support::Encoding::Hex.decode(self)
end
hex_encode() click to toggle source

Hex-encodes characters in the String.

@return [String]

The hex encoded version of the String.

@example

"hello".hex_encode
# => "68656C6C6F"

@see Ronin::Support::Encoding::Hex.encodde

@since 0.6.0

# File lib/ronin/support/encoding/hex/core_ext/string.rb, line 37
def hex_encode
  Ronin::Support::Encoding::Hex.encode(self)
end
hex_escape() click to toggle source

Hex-escapes the characters within the String.

@return [String]

The hex escaped version of the String.

@example

"hello\nworld".hex_escape
# => "hello\\nworld"

@see Ronin::Support::Encoding::Hex.escape

@api public

# File lib/ronin/support/encoding/hex/core_ext/string.rb, line 71
def hex_escape
  Ronin::Support::Encoding::Hex.escape(self)
end
hex_string() click to toggle source

Converts the String into a double-quoted hex string.

@return [String]

@example

"hello\nworld".hex_string
# => "\"hello\\nworld\""

@see Ronin::Support::Encoding::Hex.quote

@since 1.0.0

@api public

# File lib/ronin/support/encoding/hex/core_ext/string.rb, line 108
def hex_string
  Ronin::Support::Encoding::Hex.quote(self)
end
hex_unescape() click to toggle source

Unescapes the characters within the String.

@return [String]

The hex unescaped version of the String.

@example

"hello\\nworld".hex_unescape
# => "hello\nworld"

@see Ronin::Support::Encoding::Hex.unescape

@api public

# File lib/ronin/support/encoding/hex/core_ext/string.rb, line 89
def hex_unescape
  Ronin::Support::Encoding::Hex.unescape(self)
end
hex_unquote() click to toggle source

Removes the quotes and unescapes a hex string.

@return [String]

The un-quoted String if the String begins and ends with quotes, or the
same String if it is not quoted.

@example

"\"hello\\nworld\"".hex_unquote
# => "hello\nworld"

@see Ronin::Support::Encoding::Hex.unquote

@since 1.0.0

@api public

# File lib/ronin/support/encoding/hex/core_ext/string.rb, line 129
def hex_unquote
  Ronin::Support::Encoding::Hex.unquote(self)
end
hmac(key: , digest: :sha1) click to toggle source

Calculates the HMAC of the String.

@param [String] key

The secret key for the HMAC.

@param [Symbol] digest

The digest algorithm for the HMAC.

@return [String]

The hex-encoded HMAC for the String.

@see Ronin::Support::Crypto.hmac

@since 0.6.0

@api public

# File lib/ronin/support/crypto/core_ext/string.rb, line 123
def hmac(key: , digest: :sha1)
  hmac = Ronin::Support::Crypto.hmac(self, key: key, digest: digest)
  return hmac.hexdigest
end
homoglyph(**kwargs) click to toggle source

Returns a random homoglyph substitution of the String.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [:ascii, :greek, :cyrillic, :punctuation, :latin_numbers,

              :full_width, nil] char_set
The character set to use.

@return [String]

A random homoglyphic variation of the String.

@raise [ArgumentError]

Could not find any matching characters to replace in the String.

@raise [Ronin::Support::Text::Homoglyph::NotViable]

No homoglyph replaceable characters were found in the String.

@example

"microsoft".homoglyph
# => "microsoft"

@see Ronin::Support::Text::Homoglyph.substitute

@api public

@since 1.0.0

# File lib/ronin/support/text/homoglyph/core_ext/string.rb, line 52
def homoglyph(**kwargs)
  Ronin::Support::Text::Homoglyph.substitute(self,**kwargs)
end
homoglyphs(**kwargs) click to toggle source

Returns every homoglyph variation of the String.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [:ascii, :greek, :cyrillic, :punctuation, :latin_numbers,

              :full_width, nil] char_set
The character set to use.

@yield [homoglyph]

The given block will be passed each homoglyph variation of the given
word.

@return [Array<String>]

All variation of the given String.

@example

"microsoft".homoglyphs
# =>
# ["ⅿicrosoft",
#  "microsoft",
#  "mіcrosoft",
#  "mⅰcrosoft",
#  "microsoft",
#  "miϲrosoft",
#  "miсrosoft",
#  "miⅽrosoft",
#  "microsoft",
#  "microsoft",
#  "micrοsoft",
#  "microsοft",
#  "micrоsoft",
#  "microsоft",
#  "microsoft",
#  "microsoft",
#  "microѕoft",
#  "microsoft",
#  "microsoft",
#  "microsoft"]

@see Ronin::Support::Text::Homoglyph.each_substitution

@api public

@since 1.0.0

# File lib/ronin/support/text/homoglyph/core_ext/string.rb, line 137
def homoglyphs(**kwargs)
  Ronin::Support::Text::Homoglyph.each_substitution(self,**kwargs).to_a
end
html_decode()
Alias for: html_unescape
html_encode(**kwargs) click to toggle source

Encodes the chars in the String for HTML.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [:decimal, :hex] :format (:decimal)

The numeric format for the escaped characters.

@option kwargs [Boolean] :zero_pad

Controls whether the escaped characters will be left-padded with
up to seven `0` characters.

@option kwargs [:lower, :upper, nil] :case

Controls whether to output lowercase or uppercase XML special
characters. Defaults to lowercase hexadecimal.

@return [String]

The encoded HTML String.

@raise [ArgumentError]

The `format:` or `case:` keyword argument is invalid.

@example

"abc".html_encode
# => "&#97;&#98;&#99;"

@example Zero-padding:

"abc".html_encode(zero_pad: true)
# => "&#0000097;&#0000098;&#0000099;"

@example Hexadecimal encoded characters:

"abc".html_encode(format: :hex)
# => "&#x61;&#x62;&#x63;"

@example Uppercase hexadecimal encoded characters:

"abc\xff".html_encode(format: :hex, case: :upper)
# => "&#X61;&#X62;&#X63;&#XFF;"

@see Ronin::Support::Encoding::HTML.encode

@since 0.2.0

@api public

# File lib/ronin/support/encoding/html/core_ext/string.rb, line 124
def html_encode(**kwargs)
  Ronin::Support::Encoding::HTML.encode(self,**kwargs)
end
html_escape(**kwargs) click to toggle source

HTML escapes the String.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [:lower, :upper, nil] :case

Controls whether to output lowercase or uppercase XML special
characters. Defaults to lowercase hexadecimal.

@return [String]

The HTML escaped String.

@raise [ArgumentError]

The `case:` keyword argument is invalid.

@example

"one & two".html_escape
# => "one &amp; two"

@example Uppercase escaped characters:

"one & two".html_escape(case: :upper)
# => "one &AMP; two"

@see rubydoc.info/stdlib/cgi/1.9.2/CGI.escapeHTML @see Ronin::Support::Encoding::HTML.escape

@since 0.2.0

@api public

# File lib/ronin/support/encoding/html/core_ext/string.rb, line 54
def html_escape(**kwargs)
  Ronin::Support::Encoding::HTML.escape(self,**kwargs)
end
html_unescape() click to toggle source

Unescapes the HTML encoded String.

@return [String]

The unescaped String.

@example

"&lt;p&gt;one &lt;span&gt;two&lt;/span&gt;&lt;/p&gt;".html_unescape
# => "<p>one <span>two</span></p>"

@see rubydoc.info/stdlib/cgi/1.9.2/CGI.unescapeHTML @see Ronin::Support::Encoding::HTML.unescape

@since 0.2.0

@api public

# File lib/ronin/support/encoding/html/core_ext/string.rb, line 75
def html_unescape
  Ronin::Support::Encoding::HTML.unescape(self)
end
Also aliased as: html_decode
http_decode() click to toggle source

HTTP decodes the HTTP encoded String.

@return [String]

The decoded String.

@example

"sweet+%26+sour".http_decode
# => "sweet & sour"

@see Ronin::Support::Encoding::HTTP.decode

@api public

# File lib/ronin/support/encoding/http/core_ext/string.rb, line 125
def http_decode
  Ronin::Support::Encoding::HTTP.decode(self)
end
http_encode(**kwargs) click to toggle source

HTTP encodes each byte of the String.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [:lower, :upper, nil] :case

Controls whether to output lowercase or uppercase hexadecimal.
Defaults to uppercase hexadecimal.

@return [String]

The HTTP hexadecimal encoded form of the String.

@raise [ArgumentError]

The `case:` keyword argument was not `:lower`, `:upper`, or `nil`.

@example

"hello".http_encode
# => "%68%65%6c%6c%6f"

@example Lowercase encoding:

"hello".http_encode(case: :lower)
# => "%68%65%6c%6c%6f"

@see Ronin::Support::Encoding::HTTP.encode

@api public

@since 1.0.0

# File lib/ronin/support/encoding/http/core_ext/string.rb, line 107
def http_encode(**kwargs)
  Ronin::Support::Encoding::HTTP.encode(self,**kwargs)
end
http_escape(**kwargs) click to toggle source

HTTP escapes the String.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [:lower, :upper, nil] :case

Controls whether to output lowercase or uppercase hexadecimal.
Defaults to uppercase hexadecimal.

@return [String]

The HTTP escaped form of the String.

@raise [ArgumentError]

The `case:` keyword argument was not `:lower`, `:upper`, or `nil`.

@example

"x > y".http_escape
# => "x+%3E+y"

@example Lowercase encoding:

"x > y".html_escape(case: :lower)
# => "x+%3e+y"

@see Ronin::Support::Encoding::HTTP.escape

@api public

@since 0.6.0

# File lib/ronin/support/encoding/http/core_ext/string.rb, line 53
def http_escape(**kwargs)
  Ronin::Support::Encoding::HTTP.escape(self,**kwargs)
end
http_unescape() click to toggle source

HTTP unescapes the String.

@return [String]

The raw String.

@example

"sweet+%26+sour".http_unescape
# => "sweet & sour"

@see Ronin::Support::Encoding::HTTP.unescape

@api public

@since 0.6.0

# File lib/ronin/support/encoding/http/core_ext/string.rb, line 73
def http_unescape
  Ronin::Support::Encoding::HTTP.unescape(self)
end
insert_after(pattern,data) click to toggle source

Inserts data after the occurrence of a pattern.

@param [String, Regexp] pattern

The pattern to search for.

@param [String] data

The data to insert after the pattern.

@return [String]

The new modified String.

@api public

# File lib/ronin/support/core_ext/string.rb, line 264
def insert_after(pattern,data)
  string = dup
  match  = string.match(pattern)

  if match
    index = match.end(match.length - 1)

    string.insert(index,data)
  end

  return string
end
insert_before(pattern,data) click to toggle source

Inserts data before the occurrence of a pattern.

@param [String, Regexp] pattern

The pattern to search for.

@param [String] data

The data to insert before the pattern.

@return [String]

The new modified String.

@api public

# File lib/ronin/support/core_ext/string.rb, line 242
def insert_before(pattern,data)
  string = dup
  index  = string.index(pattern)

  string.insert(index,data) if index
  return string
end
js_decode()
Alias for: js_unescape
js_encode() click to toggle source

JavaScript escapes every character of the String.

@return [String]

The JavaScript escaped String.

@example

"hello".js_encode
# => "\\u0068\\u0065\\u006C\\u006C\\u006F"

@see Ronin::Support::Encoding::JS.encode

@api public

@since 1.0.0

# File lib/ronin/support/encoding/js/core_ext/string.rb, line 79
def js_encode
  Ronin::Support::Encoding::JS.encode(self)
end
js_escape() click to toggle source

Escapes a String for JavaScript.

@return [String]

The JavaScript escaped String.

@example

"hello\nworld\n".js_escape
# => "hello\\nworld\\n"

@see Ronin::Support::Encoding::JS.escape

@since 0.2.0

@api public

# File lib/ronin/support/encoding/js/core_ext/string.rb, line 39
def js_escape
  Ronin::Support::Encoding::JS.escape(self)
end
js_string() click to toggle source

Converts the String into a JavaScript string.

@return [String]

@example

"hello\nworld\n".js_string
# => "\"hello\\nworld\\n\""

@see Ronin::Support::Encoding::JS.quote

@since 1.0.0

@api public

# File lib/ronin/support/encoding/js/core_ext/string.rb, line 100
def js_string
  Ronin::Support::Encoding::JS.quote(self)
end
js_unescape() click to toggle source

Unescapes a JavaScript escaped String.

@return [String]

The unescaped JavaScript String.

@example

"\\u0068\\u0065\\u006C\\u006C\\u006F world".js_unescape
# => "hello world"

@see Ronin::Support::Encoding::JS.unescape

@since 0.2.0

@api public

# File lib/ronin/support/encoding/js/core_ext/string.rb, line 59
def js_unescape
  Ronin::Support::Encoding::JS.unescape(self)
end
Also aliased as: js_decode
js_unquote() click to toggle source

Removes the quotes an unescapes a JavaScript string.

@return [String]

The un-quoted String if the String begins and ends with quotes, or the
same String if it is not quoted.

@example

"\"hello\\nworld\"".js_unquote
# => "hello\nworld"

@see Ronin::Support::Encoding::JS.unquote

@since 1.0.0

@api public

# File lib/ronin/support/encoding/js/core_ext/string.rb, line 121
def js_unquote
  Ronin::Support::Encoding::JS.unquote(self)
end
md5() click to toggle source

@return [String]

The MD5 checksum of the String.

@example

"hello".md5
# => "5d41402abc4b2a76b9719d911017c592"

@api public

# File lib/ronin/support/crypto/core_ext/string.rb, line 35
def md5
  Digest::MD5.hexdigest(self)
end
pad(padding,length) click to toggle source

Creates a new String by padding the String with repeating text, out to a specified length.

@param [String] padding

The text to pad the new String with.

@param [String] length

The maximum length to pad the new String out to.

@return [String]

The padded version of the String.

@example

"hello".pad('A',50)
# => "helloAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"

@api public

# File lib/ronin/support/core_ext/string.rb, line 296
def pad(padding,length)
  ljust(length,padding)
end
powershell_decode()
Also aliased as: psh_decode
Alias for: powershell_unescape
powershell_encode() click to toggle source
PowerShell encodes][1

every character in the String.

[1]: docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_special_characters?view=powershell-7.2

@return [String]

The PowerShell encoded String.

@example

"hello world".powershell_encode
# => "$([char]0x68)$([char]0x65)$([char]0x6c)$([char]0x6c)$([char]0x6f)$([char]0x20)$([char]0x77)$([char]0x6f)$([char]0x72)$([char]0x6c)$([char]0x64)"

@see Ronin::Support::Encoding::PowerShell.encode

@api public

@since 1.0.0

# File lib/ronin/support/encoding/powershell/core_ext/string.rb, line 89
def powershell_encode
  Ronin::Support::Encoding::PowerShell.encode(self)
end
Also aliased as: psh_encode
powershell_escape() click to toggle source
PowerShell escapes][1

the characters in the String.

[1]: docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_special_characters?view=powershell-7.2

@return [String]

The PowerShell escaped string.

@example

"hello\nworld".powershell_escape
# => "hello`nworld"

@see Ronin::Support::Encoding::PowerShell.escape

@api public

@since 1.0.0

# File lib/ronin/support/encoding/powershell/core_ext/string.rb, line 41
def powershell_escape
  Ronin::Support::Encoding::PowerShell.escape(self)
end
Also aliased as: psh_escape
powershell_string() click to toggle source

Converts the String into a double-quoted PowerShell escaped String.

@return [String]

The quoted and escaped PowerShell string.

@example

"hello\nworld".powershell_string
# => "\"hello`nworld\""

@see Ronin::Support::Encoding::PowerShell.quote

@api public

@since 1.0.0

# File lib/ronin/support/encoding/powershell/core_ext/string.rb, line 114
def powershell_string
  Ronin::Support::Encoding::PowerShell.quote(self)
end
Also aliased as: psh_string
powershell_unescape() click to toggle source
PowerShell unescapes][1

the characters in the String.

[1]: docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_special_characters?view=powershell-7.2

@return [String]

The PowerShell unescaped string.

@example

"hello`nworld".powershell_unescape
# => "hello\nworld"

@see Ronin::Support::Encoding::PowerShell.unescape

@api public

@since 1.0.0

# File lib/ronin/support/encoding/powershell/core_ext/string.rb, line 65
def powershell_unescape
  Ronin::Support::Encoding::PowerShell.unescape(self)
end
Also aliased as: psh_unescape, powershell_decode
powershell_unquote() click to toggle source

Removes the quotes an unescapes a PowerShell string.

@return [String]

The un-quoted String if the String begins and ends with quotes, or the
same String if it is not quoted.

@example

"\"hello`nworld\"".powershell_unquote
# => "hello\nworld"
"'hello''world'".powershell_unquote
# => "hello'world"

@see Ronin::Support::Encoding::PowerShell.unquote

@since 1.0.0

@api public

# File lib/ronin/support/encoding/powershell/core_ext/string.rb, line 139
def powershell_unquote
  Ronin::Support::Encoding::PowerShell.unquote(self)
end
Also aliased as: psh_unquote
psh_decode()
Alias for: powershell_decode
psh_encode()
Alias for: powershell_encode
psh_escape()
Alias for: powershell_escape
psh_string()
Alias for: powershell_string
psh_unescape()
Alias for: powershell_unescape
psh_unquote()
Alias for: powershell_unquote
punycode_decode() click to toggle source

Decodes a [punycode] String back into unicode.

[punycode]: en.wikipedia.org/wiki/Punycode

@return [String]

The decoded unicode String.

@example

"xn--8ws00zhy3a".punycode_decode
# => "詹姆斯"

@api public

@since 1.0.0

# File lib/ronin/support/encoding/punycode/core_ext/string.rb, line 59
def punycode_decode
  Ronin::Support::Encoding::Punycode.decode(self)
end
punycode_encode() click to toggle source

Encodes a unicode String into [punycode].

[punycode]: en.wikipedia.org/wiki/Punycode

@return [String]

The punycode String.

@example

"詹姆斯".punycode_encode
# => "xn--8ws00zhy3a"

@api public

@since 1.0.0

# File lib/ronin/support/encoding/punycode/core_ext/string.rb, line 39
def punycode_encode
  Ronin::Support::Encoding::Punycode.encode(self)
end
qp_decode()
qp_encode()
qp_escape()
qp_unescape()
quoted_printable_decode()
quoted_printable_encode()
quoted_printable_escape() click to toggle source

Escapes the String as [Quoted-Printable].

[Quoted-Printable]: en.wikipedia.org/wiki/Quoted-printable

@return [String]

The quoted-printable escaped String.

@example

'<a href="https://example.com/">link</a>'.quoted_printable_escape
# => "<a href=3D\"https://example.com/\">link</a>=\n"

@see Ronin::Support::Encoding::QuotedPrintable.escape

@api public

@since 1.0.0

# File lib/ronin/support/encoding/quoted_printable/core_ext/string.rb, line 41
def quoted_printable_escape
  Ronin::Support::Encoding::QuotedPrintable.escape(self)
end
quoted_printable_unescape() click to toggle source

Unescapes a [Quoted-Printable] encoded String.

[Quoted-Printable]: en.wikipedia.org/wiki/Quoted-printable

@return [String]

The unescaped String.

@example

"<a href=3D\"https://example.com/\">link</a>=\n".quoted_printable_unescape
# => "<a href=\"https://example.com/\">link</a>"

@see Ronin::Support::Encoding::QuotedPrintable.unescape

@api public

@since 1.0.0

# File lib/ronin/support/encoding/quoted_printable/core_ext/string.rb, line 67
def quoted_printable_unescape
  Ronin::Support::Encoding::QuotedPrintable.unescape(self)
end
random_case() click to toggle source

Creates a new String by randomizing the case of each character in the String.

@return [String]

The new String with randomized case.

@example

"a".random_case
# => "A"
"ab".random_case
# => "aB"
"foo".random_case
# => "FoO"
"The quick brown fox jumps over 13 lazy dogs.".random_case
# => "the quIcK broWn fox Jumps oveR 13 lazY Dogs."

@api public

# File lib/ronin/support/text/core_ext/string.rb, line 42
def random_case
  Ronin::Support::Text::Random.swapcase(self)
end
rmd160() click to toggle source

Calculates the RMD160 checksum for the String.

@return [String]

The RMD160 checksum of the String.

@example

"hello".rmd160
# => "108f07b8382412612c048d07d13f814118445acd"

@api public

@since 0.6.0

@note JRuby and TruffleRuby do not yet support RMD160.

# File lib/ronin/support/crypto/core_ext/string.rb, line 101
def rmd160
  Digest::RMD160.hexdigest(self)
end
rot(n=13,**kwargs) click to toggle source

Rotates the characters in the string using the given alphabet.

@param [Integer] n

The number of characters to shift each character by.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [Array<Array<String>>] :alphabets

The alphabet(s) to use.

@return [String]

The rotated string.

@note

This method was added as a joke and should not be used for secure
cryptographic communications.

@example ROT13 “encryption”:

"The quick brown fox jumps over 13 lazy dogs.".rot
# => "Gur dhvpx oebja sbk whzcf bire 46 ynml qbtf."

@example ROT13 “decryption”:

"Gur dhvpx oebja sbk whzcf bire 46 ynml qbtf.".rot(-13)
# => "The quick brown fox jumps over 13 lazy dogs."

@since 1.0.0

# File lib/ronin/support/crypto/core_ext/string.rb, line 551
def rot(n=13,**kwargs)
  Ronin::Support::Crypto.rot(self,n,**kwargs)
end
rsa_decrypt(**kwargs) click to toggle source

Decrypts the String using the given RSA key.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments for {Ronin::Support::Crypto.rsa_decrypt}.

@option kwargs [String, nil] :key

The PEM or DER encoded RSA key string.

@option kwargs [String, nil] :key_file

The path to the PEM or DER encoded RSA key file.

@option kwargs [String, nil] :key_password

The optional password to decrypt the encrypted RSA key.

@option kwargs [:pkcs1_oaep, :pkcs1, :sslv23,

              nil, false] :padding (:pkcs1)
Optional padding mode. `nil` and `false` will disable padding.

@return [String]

The decrypted data.

@raise [ArgumentError]

Either the `key:` or `key_file:` keyword argument must be given.

@since 1.0.0

# File lib/ronin/support/crypto/core_ext/string.rb, line 518
def rsa_decrypt(**kwargs)
  Ronin::Support::Crypto.rsa_decrypt(self,**kwargs)
end
rsa_encrypt(**kwargs) click to toggle source

Encrypts the String using the given RSA key.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments for {Ronin::Support::Crypto.rsa_encrypt}.

@option kwargs [String, nil] :key

The PEM or DER encoded RSA key string.

@option kwargs [String, nil] :key_file

The path to the PEM or DER encoded RSA key file.

@option kwargs [String, nil] :key_password

The optional password to decrypt the encrypted RSA key.

@option kwargs [:pkcs1_oaep, :pkcs1, :sslv23,

              nil, false] :padding (:pkcs1)
Optional padding mode. `nil` and `false` will disable padding.

@return [String]

The encrypted data.

@raise [ArgumentError]

Either the `key:` or `key_file:` keyword argument must be given.

@since 1.0.0

# File lib/ronin/support/crypto/core_ext/string.rb, line 487
def rsa_encrypt(**kwargs)
  Ronin::Support::Crypto.rsa_encrypt(self,**kwargs)
end
ruby_decode()
Alias for: ruby_unescape
ruby_encode() click to toggle source

Ruby escapes every character in the String.

@return [String]

The Ruby escaped String.

@example

"hello".ruby_encode
# => "\\x68\\x65\\x6c\\x6c\\x6f"

@see Ronin::Support::Encoding::Ruby.encode

@api public

@since 1.0.0

# File lib/ronin/support/encoding/ruby/core_ext/string.rb, line 83
def ruby_encode
  Ronin::Support::Encoding::Ruby.encode(self)
end
ruby_escape() click to toggle source

Escapes a String for Ruby.

@return [String]

The Ruby escaped String.

@example

"hello\nworld\n".ruby_escape
# => "hello\\nworld\\n"

@see Ronin::Support::Encoding::Ruby.escape

@since 1.0.0

@api public

# File lib/ronin/support/encoding/ruby/core_ext/string.rb, line 39
def ruby_escape
  Ronin::Support::Encoding::Ruby.escape(self)
end
Also aliased as: escape
ruby_string() click to toggle source

Rubyonverts the String into a Ruby string.

@return [String]

@example

"hello\nworld\n".ruby_string
# => "\"hello\\nworld\\n\""

@see Ronin::Support::Encoding::Ruby.quote

@since 1.0.0

@api public

# File lib/ronin/support/encoding/ruby/core_ext/string.rb, line 104
def ruby_string
  Ronin::Support::Encoding::Ruby.quote(self)
end
ruby_unescape() click to toggle source

Unescapes a Ruby escaped String.

@return [String]

The unescaped Ruby String.

@example

"\x68\x65\x6c\x6c\x6f\x20\x77\x6f\x72\x6c\x64".ruby_unescape
# => "hello world"

@see Ronin::Support::Encoding::Ruby.unescape

@since 1.0.0

@api public

# File lib/ronin/support/encoding/ruby/core_ext/string.rb, line 61
def ruby_unescape
  Ronin::Support::Encoding::Ruby.unescape(self)
end
Also aliased as: unescape, ruby_decode
ruby_unquote() click to toggle source

Removes the quotes an unescapes a Ruby string.

@return [String]

The un-quoted String if the String begins and ends with quotes, or the
same String if it is not quoted.

@example

"\"hello\\nworld\"".ruby_unquote
# => "hello\nworld"

@see Ronin::Support::Encoding::Ruby.unquote

@since 1.0.0

@api public

# File lib/ronin/support/encoding/ruby/core_ext/string.rb, line 125
def ruby_unquote
  Ronin::Support::Encoding::Ruby.unquote(self)
end
sha1() click to toggle source

@return [String]

The SHA1 checksum of the String.

@example

"hello".sha1
# => "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d"

@api public

# File lib/ronin/support/crypto/core_ext/string.rb, line 49
def sha1
  Digest::SHA1.hexdigest(self)
end
Also aliased as: sha128
sha128()
Alias for: sha1
sha2()
Alias for: sha256
sha256() click to toggle source

@return [String]

The SHA256 checksum of the String.

@example

"hello".sha256
# => "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"

@api public

# File lib/ronin/support/crypto/core_ext/string.rb, line 65
def sha256
  Digest::SHA256.hexdigest(self)
end
Also aliased as: sha2
sha512() click to toggle source

@return [String]

The SHA512 checksum of the String.

@example

"hello".sha512
# => "9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043"

@api public

# File lib/ronin/support/crypto/core_ext/string.rb, line 81
def sha512
  Digest::SHA512.hexdigest(self)
end
shell_decode()
Alias for: shell_unescape
shell_encode() click to toggle source

Shell encodes every character in the String.

@return [String]

The shell encoded String.

@example

"hello world".shell_encode
# => "\\x68\\x65\\x6c\\x6c\\x6f\\x0a\\x77\\x6f\\x72\\x6c\\x64"

@see Ronin::Support::Encoding::Shell.encode

@api public

@since 1.0.0

# File lib/ronin/support/encoding/shell/core_ext/string.rb, line 81
def shell_encode
  Ronin::Support::Encoding::Shell.encode(self)
end
shell_escape() click to toggle source

Shell escapes the characters in the String.

@return [String]

The shell escaped string.

@example

"hello\nworld".shell_escape
# => "hello\\nworld"

@see Ronin::Support::Encoding::Shell.escape

@api public

@since 1.0.0

# File lib/ronin/support/encoding/shell/core_ext/string.rb, line 41
def shell_escape
  Ronin::Support::Encoding::Shell.escape(self)
end
shell_string() click to toggle source

Converts the String into a double-quoted shell escaped String.

@return [String]

The quoted and escaped shell string.

@example

"hello world".shell_string
# => "\"hello world\""
"hello\nworld".shell_string
# => "$'hello\\nworld'"

@see Ronin::Support::Encoding::Shell.quote

@api public

@since 1.0.0

# File lib/ronin/support/encoding/shell/core_ext/string.rb, line 105
def shell_string
  Ronin::Support::Encoding::Shell.quote(self)
end
shell_unescape() click to toggle source

Shell unescapes the characters in the String.

@return [String]

The shell unescaped string.

@example

"hello\\nworld".shell_unescape
# => "hello\nworld"

@see Ronin::Support::Encoding::Shell.unescape

@api public

@since 1.0.0

# File lib/ronin/support/encoding/shell/core_ext/string.rb, line 61
def shell_unescape
  Ronin::Support::Encoding::Shell.unescape(self)
end
Also aliased as: shell_decode
shell_unquote() click to toggle source

Removes the quotes an unescapes a shell string.

@return [String]

The un-quoted String if the String begins and ends with quotes, or the
same String if it is not quoted.

@example

"\"hello \\\"world\\\"\"".shell_unquote
# => "hello \"world\""
"'hello\\'world'".shell_unquote
# => "hello'world"
"$'hello\\nworld'".shell_unquote
# => "hello\nworld"

@see Ronin::Support::Encoding::Shell.unquote

@since 1.0.0

@api public

# File lib/ronin/support/encoding/shell/core_ext/string.rb, line 130
def shell_unquote
  Ronin::Support::Encoding::Shell.unquote(self)
end
sql_decode() click to toggle source

Returns the SQL decoded form of the String.

@example

"'Conan O''Brian'".sql_decode
# => "Conan O'Brian"

@example

"2f6574632f706173737764".sql_decode
# => "/etc/passwd"

@raise

The String is neither hex encoded or SQL escaped.

@see Ronin::Support::Encoding::SQL.decode

@api public

# File lib/ronin/support/encoding/sql/core_ext/string.rb, line 110
def sql_decode
  Ronin::Support::Encoding::SQL.decode(self)
end
sql_encode() click to toggle source

Returns the SQL hex-string encoded form of the String.

@example

"/etc/passwd".sql_encode
# => "0x2f6574632f706173737764"

@api public

@see Ronin::Support::Encoding::SQL.encode

# File lib/ronin/support/encoding/sql/core_ext/string.rb, line 88
def sql_encode
  Ronin::Support::Encoding::SQL.encode(self)
end
sql_escape(**kwargs) click to toggle source

Escapes an String for SQL.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [:single, :double, :tick] :quotes (:single)

Specifies whether to create a single or double quoted string.

@return [String]

The escaped String.

@raise [ArgumentError]

The quotes argument was neither `:single`, `:double` nor `:tick`.

@example

"O'Brian".sql_escape
# => "'O''Brian'"

@example Encode with double-quotes:

"O'Brian".sql_escape(:double)
# => "\"O'Brian\""

@api public

@see Ronin::Support::Encoding::SQL.escape

# File lib/ronin/support/encoding/sql/core_ext/string.rb, line 50
def sql_escape(**kwargs)
  Ronin::Support::Encoding::SQL.escape(self,**kwargs)
end
sql_unescape() click to toggle source

Unescapes a SQL String.

@return [String]

The unescaped String.

@raise [ArgumentError]

The String was not quoted with single, double or tick-mark quotes.

@example

"'O''Brian'".sql_unescape
# => "O'Brian"

@api public

@see Ronin::Support::Encoding::SQL.unescape

@since 1.0.0

# File lib/ronin/support/encoding/sql/core_ext/string.rb, line 73
def sql_unescape
  Ronin::Support::Encoding::SQL.unescape(self)
end
to_ascii() click to toggle source

Converts the string into an ASCII encoded string.

@return [String]

The new ASCII string.

@api public

@since 1.0.0

# File lib/ronin/support/core_ext/string.rb, line 48
def to_ascii
  encode(Encoding::ASCII_8BIT)
end
to_utf8() click to toggle source

Converts the string into an UTF-8 encoded string.

@return [String]

The new UTF-8 string.

@api public

@since 1.0.0

# File lib/ronin/support/core_ext/string.rb, line 62
def to_utf8
  encode(Encoding::UTF_8)
end
typo(**kwargs) click to toggle source

Returns a random typo substitution for the String.

@param [Hash{Symbol => Boolean}] kwargs

Additional keyword arguments.

@option kwargs [Boolean] omit

Enables/disables omission of repeated characters.

@option kwargs [Boolean] repeat

Enables/disables repeatition of single characters.

@option kwargs [Boolean] swap

Enables/disables swapping of certain common character pairs.

@option kwargs [Boolean] suffix

Enables/disables changing the suffixes of words.

@return [String]

A random typo of the String.

@example

"microsoft".typo
# => "microssoft"

@see Ronin::Support::Text::Typo.substitute

@api public

@since 1.0.0

# File lib/ronin/support/text/typo/core_ext/string.rb, line 54
def typo(**kwargs)
  Ronin::Support::Text::Typo.substitute(self,**kwargs)
end
typos(**kwargs) click to toggle source

Returns every typo for the String.

@param [Hash{Symbol => Boolean}] kwargs

Additional keyword arguments.

@option kwargs [Boolean] omit

Enables/disables omission of repeated characters.

@option kwargs [Boolean] repeat

Enables/disables repeatition of single characters.

@option kwargs [Boolean] swap

Enables/disables swapping of certain common character pairs.

@option kwargs [Boolean] suffix

Enables/disables changing the suffixes of words.

@return [Array<String>]

Every typo variation of the String.

@example

"consciousness".typos
# =>
# ["consciusness",
#  "consciosness",
#  "conscuosness",
#  "consciosness",
#  "coonsciousness",
#  "conscioousness",
#  "conssciousness",
#  "conscioussness",
#  "consciousnesss",
#  "consciuosness",
#  "consciousnes"]

@see Ronin::Support::Text::Typo.each_substitution

@api public

@since 1.0.0

# File lib/ronin/support/text/typo/core_ext/string.rb, line 143
def typos(**kwargs)
  Ronin::Support::Text::Typo.each_substitution(self,**kwargs).to_a
end
uncommon_substring(other) click to toggle source

Finds the uncommon sub-string within the specified other string, which does not occur within the string.

@param [String] other

The other String to compare against.

@return [String]

The uncommon sub-string between the two Strings.

@api public

# File lib/ronin/support/core_ext/string.rb, line 221
def uncommon_substring(other)
  prefix  = common_prefix(other)
  postfix = self[prefix.length..].common_suffix(other[prefix.length..])

  return self[prefix.length...(length - postfix.length)]
end
unescape()
Alias for: ruby_unescape
unhexdump(**kwargs) click to toggle source

Converts a multitude of hexdump formats back into raw-data.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments for
{Ronin::Support::Binary::Unhexdump::Parser#initialize}.

@option kwargs [:od, :hexdump] :format (:hexdump)

The expected format of the hexdump. Must be either `:od` or
`:hexdump`.

@option kwargs [Symbol] :type (:byte)

Denotes the encoding used for the bytes within the hexdump.
Must be one of the following:
* `:byte`
* `:char`
* `:uint8`
* `:uint16`
* `:uint32`
* `:uint64`
* `:int8`
* `:int16`
* `:int32`
* `:int64`
* `:uchar`
* `:ushort`
* `:uint`
* `:ulong`
* `:ulong_long`
* `:short`
* `:int`
* `:long`
* `:long_long`
* `:float`
* `:double`
* `:float_le`
* `:double_le`
* `:float_be`
* `:double_be`
* `:uint16_le`
* `:uint32_le`
* `:uint64_le`
* `:int16_le`
* `:int32_le`
* `:int64_le`
* `:uint16_be`
* `:uint32_be`
* `:uint64_be`
* `:int16_be`
* `:int32_be`
* `:int64_be`
* `:ushort_le`
* `:uint_le`
* `:ulong_le`
* `:ulong_long_le`
* `:short_le`
* `:int_le`
* `:long_le`
* `:long_long_le`
* `:ushort_be`
* `:uint_be`
* `:ulong_be`
* `:ulong_long_be`
* `:short_be`
* `:int_be`
* `:long_be`
* `:long_long_be`

@option kwargs [2, 8, 10, 16, nil] :address_base

The numerical base that the offset addresses are encoded in.

@option kwargs [2, 8, 10, 16, nil] base

The numerical base that the hexdumped numbers are encoded in.

@option kwargs [Boolean] named_chars

Indicates to parse `od`-style named characters (ex: `nul`,
`del`, etc).

@return [String]

The raw-data from the hexdump.

@raise [ArgumentError]

Unsupported `type:` value, or the `format:` was not `:hexdump` or
`:od`.

@example Unhexdump a hexdump created by GNU ‘hexdump -C`:

hexdump.unhexdump
# => "hello\n"

@example Unhexdump a hexdump created by GNU ‘hexdump`:

hexdump.unhexdump(type: :uint16_le)
# => "hello\n"

@example Unhexdump a hexdump created by ‘od`:

od.unhexdump(format: :od)
# => "hello\n"

@api public

# File lib/ronin/support/binary/unhexdump/core_ext/string.rb, line 122
def unhexdump(**kwargs)
  parser = Ronin::Support::Binary::Unhexdump::Parser.new(**kwargs)
  parser.unhexdump(self)
end
unpack(*arguments,**kwargs) click to toggle source

Unpacks the String.

@param [String, Array<Symbol, (Symbol, Integer)>] arguments

The `String#unpack` format string or a list of
{Ronin::Support::Binary::Template} types.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments for
{Ronin::Support::Binary::Template#initialize}.

@option kwargs [:little, :big, :net, nil] :endian

The desired endianness of the packed data.

@option kwargs [:x86, :x86_64, :ppc, :ppc64,

              :arm, :arm_be, :arm64, :arm64_be,
              :mips, :mips_le, :mips64, :mips64_le, nil] :arch
The desired architecture that the data was packed for.

@option kwargs [:linux, :macos, :windows,

              :android, :apple_ios, :bsd,
              :freebsd, :openbsd, :netbsd] :os
The Operating System (OS) to use.

@return [Array]

The values unpacked from the String.

@raise [ArgumentError]

One of the arguments was not a known {Ronin::Support::Binary::Template}
type.

@example using {Ronin::Support::Binary::Template} types:

"A\0\0\0hello\0".unpack(:uint32_le, :string)
# => [10, "hello"]

@example using a ‘String#unpack` format string:

"A\0\0\0".unpack('V')
# => 65

@see rubydoc.info/stdlib/core/String:unpack @see Ronin::Support::Binary::Template

@since 0.5.0

@api public

# File lib/ronin/support/binary/core_ext/string.rb, line 73
def unpack(*arguments,**kwargs)
  if (arguments.length == 1 && arguments.first.kind_of?(String))
    unpack_original(arguments.first)
  else
    template = Ronin::Support::Binary::Template.new(arguments,**kwargs)
    template.unpack(self)
  end
end
Also aliased as: unpack_original
unpack1(argument,**kwargs) click to toggle source

Unpacks a single value from the String.

@param [String, Symbol] argument

The `String#unpack` format String (ex: `L<`) or a
{Ronin::Support::Binary::CTypes} type name (ex: `:uint32_le`).

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments for
{Ronin::Support::Binary::Template#initialize}.

@option kwargs [:little, :big, :net, nil] :endian

The desired endianness of the packed data.

@option kwargs [:x86, :x86_64, :ppc, :ppc64,

              :arm, :arm_be, :arm64, :arm64_be,
              :mips, :mips_le, :mips64, :mips64_le, nil] :arch
The desired architecture that the data was packed for.

@return [Integer, Float, String]

The unpacked value.

@raise [ArgumentError]

The given argument was not a String or a Symbol, or the given C type is
unknown.

@since 1.0.0

@api public

# File lib/ronin/support/binary/core_ext/string.rb, line 114
def unpack1(argument,**kwargs)
  case argument
  when String
    unpack1_original(argument)
  when Symbol
    platform = Ronin::Support::Binary::CTypes.platform(**kwargs)
    type     = platform[argument]

    unpack1_original(type.pack_string)
  else
    raise(ArgumentError,"argument must be either a String or a Symbol: #{argument.inspect}")
  end
end
Also aliased as: unpack1_original
unpack1_original(argument,**kwargs)
Alias for: unpack1
unpack_original(*arguments,**kwargs)
Alias for: unpack
uri_decode()
Alias for: uri_unescape
uri_encode(**kwargs) click to toggle source

URI encodes the String.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [:lower, :upper, nil] :case

Controls whether to output lowercase or uppercase hexadecimal.
Defaults to uppercase hexadecimal.

@return [String]

The URI encoded form of the String.

@example

"plain text".uri_encode
# => "%70%6C%61%69%6E%20%74%65%78%74"

@example Lowercase encoding:

"plain text".uri_encode(case: :lower)
# => "%70%6c%61%69%6e%20%74%65%78%74"

@see Ronin::Support::Encoding::URI.encode

@api public

# File lib/ronin/support/encoding/uri/core_ext/string.rb, line 95
def uri_encode(**kwargs)
  Ronin::Support::Encoding::URI.encode(self,**kwargs)
end
uri_escape(**kwargs) click to toggle source

URI escapes the String.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [:lower, :upper, nil] :case

Controls whether to output lowercase or uppercase hexadecimal.
Defaults to uppercase hexadecimal.

@return [String]

The URI escaped form of the String.

@example

"x > y".uri_escape
# => "x%20%3E%20y"

@example Lowercase encoding:

"x > y".uri_escape(case: :lower)
# => "x%20%3e%20y"

@see Ronin::Support::Encoding::URI.escape

@api public

# File lib/ronin/support/encoding/uri/core_ext/string.rb, line 48
def uri_escape(**kwargs)
  Ronin::Support::Encoding::URI.escape(self,**kwargs)
end
uri_form_decode()
Also aliased as: www_form_decode
Alias for: uri_form_unescape
uri_form_encode(**kwargs) click to toggle source

URI Form encodes every character in the String.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [:lower, :upper, nil] :case

Controls whether to output lowercase or uppercase hexadecimal.
Defaults to uppercase hexadecimal.

@return [String]

The URI Form encoded String.

@example

"hello world".uri_form_encode
# => "%68%65%6C%6C%6F+%77%6F%72%6C%64"

@example Lowercase encoding:

"hello world".uri_form_encode(case: :lower)
# => "%68%65%6c%6c%6f+%77%6f%72%6c%64"

@see Ronin::Support::Encoding::URI::Form.unescape

@api public

@since 1.0.0

# File lib/ronin/support/encoding/uri/core_ext/string.rb, line 186
def uri_form_encode(**kwargs)
  Ronin::Support::Encoding::URI::Form.encode(self,**kwargs)
end
Also aliased as: www_form_encode
uri_form_escape(**kwargs) click to toggle source

URI Form escapes the String.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [:lower, :upper, nil] :case

Controls whether to output lowercase or uppercase hexadecimal.
Defaults to uppercase hexadecimal.

@return [String]

The URI Form escaped String.

@example

"hello world".uri_form_escape
# => "hello+world"
"hello\0world".uri_form_escape
# => "hello%00world"

@example Lowercase encoding:

"hello\xffworld".uri_form_escape(case: :lower)
# => "hello%ffworld"

@see www.w3.org/TR/2013/CR-html5-20130806/forms.html#url-encoded-form-data @see Ronin::Support::Encoding::URI::Form.escape

@api public

@since 1.0.0

# File lib/ronin/support/encoding/uri/core_ext/string.rb, line 131
def uri_form_escape(**kwargs)
  Ronin::Support::Encoding::URI::Form.escape(self,**kwargs)
end
Also aliased as: www_form_escape
uri_form_unescape() click to toggle source

URI Form unescapes the String.

@return [String]

The URI Form unescaped String.

@example

"hello+world".uri_form_unescape
# => "hello world"
"hello%00world".uri_form_unescape
# => "hello\u0000world"

@see Ronin::Support::Encoding::URI::Form.unescape

@api public

# File lib/ronin/support/encoding/uri/core_ext/string.rb, line 153
def uri_form_unescape
  Ronin::Support::Encoding::URI::Form.unescape(self)
end
uri_unescape() click to toggle source

URI unescapes the String.

@return [String]

The unescaped URI form of the String.

@example

"sweet%20%26%20sour".uri_unescape
# => "sweet & sour"

@see Ronin::Support::Encoding::URI.unescape

@api public

# File lib/ronin/support/encoding/uri/core_ext/string.rb, line 66
def uri_unescape
  Ronin::Support::Encoding::URI.unescape(self)
end
Also aliased as: uri_decode
uu_decode() click to toggle source

Decodes the [uuencoded] String.

[uuencoding]: en.wikipedia.org/wiki/Uuencoding

@return [String]

The decoded String.

@example

"+:&5L;&\\@=V]R;&0`\n".uu_decode
# => "hello world"

@see Ronin::Support::Encoding::UUEncoding.decode

@api public

@since 1.0.0

# File lib/ronin/support/encoding/uuencoding/core_ext/string.rb, line 66
def uu_decode
  Ronin::Support::Encoding::UUEncoding.decode(self)
end
Also aliased as: uudecode, uu_unescape
uu_encode() click to toggle source
uuencodes][uuencoding

the String.

[uuencoding]: en.wikipedia.org/wiki/Uuencoding

@return [String]

The UU encoded String.

@example

"hello world".uu_encode
# => "+:&5L;&\\@=V]R;&0`\n"

@see Ronin::Support::Encoding::UUEncoding.encode

@api public

@since 1.0.0

# File lib/ronin/support/encoding/uuencoding/core_ext/string.rb, line 41
def uu_encode
  Ronin::Support::Encoding::UUEncoding.encode(self)
end
Also aliased as: uuencode, uu_escape
uu_escape()
Alias for: uu_encode
uu_unescape()
Alias for: uu_decode
uudecode()
Alias for: uu_decode
uuencode()
Alias for: uu_encode
www_form_decode()
Alias for: uri_form_decode
www_form_encode(**kwargs)
Alias for: uri_form_encode
www_form_escape(**kwargs)
Alias for: uri_form_escape
www_form_unescape()
Alias for: uri_form_unescape
xml_decode()
Alias for: xml_unescape
xml_encode(**kwargs) click to toggle source

Encodes each character in the String as an XML character.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [:decimal, :hex] :format (:decimal)

The numeric format for the escaped characters.

@option kwargs [Boolean] :zero_pad

Controls whether the escaped characters will be left-padded with
up to seven `0` characters.

@option kwargs [:lower, :upper, nil] :case

Controls whether to output lowercase or uppercase XML special
characters. Defaults to lowercase hexadecimal.

@return [String]

The XML encoded String.

@example

"abc".xml_encode
# => "&#97;&#98;&#99;"

@example Zero-padding:

"abc".xml_encode(zero_pad: true)
# => "&#0000097;&#0000098;&#0000099;"

@example Hexadecimal encoded characters:

"abc".xml_encode(format: :hex)
# => "&#x61;&#x62;&#x63;"

@example Uppercase hexadecimal encoded characters:

"abc\xff".xml_encode(format: :hex, case: :upper)
# => "&#X61;&#X62;&#X63;&#XFF;"

@see Ronin::Support::Encoding::XML.encode

@since 1.0.0

@api public

# File lib/ronin/support/encoding/xml/core_ext/string.rb, line 118
def xml_encode(**kwargs)
  Ronin::Support::Encoding::XML.encode(self,**kwargs)
end
xml_escape(**kwargs) click to toggle source

XML escapes the String.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments.

@option kwargs [:lower, :upper, nil] :case

Controls whether to output lowercase or uppercase XML special
characters. Defaults to lowercase hexadecimal.

@return [String]

The XML escaped String.

@example

"one & two".xml_escape
# => "one &amp; two"

@example Uppercase escaped characters:

"one & two".xml_escape(case: :upper)
# => "one &AMP; two"

@see rubydoc.info/stdlib/cgi/CGI.escapeHTML @see Ronin::Support::Encoding::XML.escape

@since 0.2.0

@api public

# File lib/ronin/support/encoding/xml/core_ext/string.rb, line 51
def xml_escape(**kwargs)
  Ronin::Support::Encoding::XML.escape(self,**kwargs)
end
xml_unescape() click to toggle source

Unescapes the XML encoded String.

@return [String]

The unescaped String.

@example

"&lt;p&gt;one &lt;span&gt;two&lt;/span&gt;&lt;/p&gt;".xml_unescape
# => "<p>one <span>two</span></p>"

@see rubydoc.info/stdlib/cgi/CGI.unescapeHash @see Ronin::Support::Encoding::XML.unescape

@since 0.2.0

@api public

# File lib/ronin/support/encoding/xml/core_ext/string.rb, line 72
def xml_unescape
  Ronin::Support::Encoding::XML.unescape(self)
end
Also aliased as: xml_decode
xor(key) click to toggle source

XOR encodes the String.

@param [Enumerable, Integer] key

The byte to XOR against each byte in the String.

@return [String]

The XOR encoded String.

@example

"hello".xor(0x41)
# => ")$--."

@example

"hello again".xor([0x55, 0x41, 0xe1])
# => "=$\x8d9.\xc14&\x80</"

@api public

# File lib/ronin/support/crypto/core_ext/string.rb, line 574
def xor(key)
  Ronin::Support::Crypto.xor(self,key)
end
Also aliased as: ^
zlib_deflate() click to toggle source

Zlib deflate a string.

@return [String]

The Zlib deflated form of the string.

@example

"hello".zlib_deflate
# => "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15"

@api public

# File lib/ronin/support/compression/core_ext/string.rb, line 53
def zlib_deflate
  Ronin::Support::Compression.zlib_deflate(self)
end
zlib_inflate() click to toggle source

Zlib inflate a string.

@return [String]

The Zlib inflated form of the string.

@example

"x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15".zlib_inflate
# => "hello"

@api public

# File lib/ronin/support/compression/core_ext/string.rb, line 37
def zlib_inflate
  Ronin::Support::Compression.zlib_inflate(self)
end