Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/ronin/support/core_ext/string.rb,
lib/ronin/support/text/core_ext/string.rb,
lib/ronin/support/binary/core_ext/string.rb,
lib/ronin/support/crypto/core_ext/string.rb,
lib/ronin/support/encoding/core_ext/string.rb,
lib/ronin/support/text/typo/core_ext/string.rb,
lib/ronin/support/encoding/c/core_ext/string.rb,
lib/ronin/support/compression/core_ext/string.rb,
lib/ronin/support/encoding/js/core_ext/string.rb,
lib/ronin/support/encoding/hex/core_ext/string.rb,
lib/ronin/support/encoding/sql/core_ext/string.rb,
lib/ronin/support/encoding/uri/core_ext/string.rb,
lib/ronin/support/encoding/xml/core_ext/string.rb,
lib/ronin/support/text/entropy/core_ext/string.rb,
lib/ronin/support/encoding/html/core_ext/string.rb,
lib/ronin/support/encoding/http/core_ext/string.rb,
lib/ronin/support/encoding/ruby/core_ext/string.rb,
lib/ronin/support/encoding/shell/core_ext/string.rb,
lib/ronin/support/text/homoglyph/core_ext/string.rb,
lib/ronin/support/binary/bit_flip/core_ext/string.rb,
lib/ronin/support/encoding/base16/core_ext/string.rb,
lib/ronin/support/encoding/base32/core_ext/string.rb,
lib/ronin/support/encoding/base36/core_ext/string.rb,
lib/ronin/support/encoding/base62/core_ext/string.rb,
lib/ronin/support/encoding/base64/core_ext/string.rb,
lib/ronin/support/binary/unhexdump/core_ext/string.rb,
lib/ronin/support/encoding/punycode/core_ext/string.rb,
lib/ronin/support/encoding/powershell/core_ext/string.rb,
lib/ronin/support/encoding/uuencoding/core_ext/string.rb,
lib/ronin/support/encoding/quoted_printable/core_ext/string.rb

Overview

Copyright (c) 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 https://www.gnu.org/licenses/.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ascii(string) ⇒ String

Creates a new ASCII encoding string.

Parameters:

  • string (String)

    The string to convert to ASCII.

Returns:

  • (String)

    The new ASCII encoded string.

Since:

  • 1.0.0



34
35
36
# File 'lib/ronin/support/core_ext/string.rb', line 34

def self.ascii(string)
  string.encode(Encoding::ASCII_8BIT)
end

Instance Method Details

#aes128_decrypt(**kwargs) ⇒ String

Decrypts the String using AES-128.

Examples:

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

Parameters:

Options Hash (**kwargs):

  • mode (:cbc, :cfb, :ofb, :ctr, Symbol) — default: :cbc

    The desired AES cipher mode.

  • :hash (Symbol) — default: :md5

    The algorithm to hash the :password.

  • :key (String)

    The secret key to use.

  • :password (String)

    The password for the cipher.

  • :iv (String)

    The optional Initial Vector (IV).

  • :padding (Integer)

    Sets the padding for the cipher.

Returns:

  • (String)

    The encrypted data.

Raises:

  • (ArgumentError)

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

Since:

  • 1.0.0



376
377
378
# 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) ⇒ String

Encrypts the String using AES-128.

Examples:

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

Parameters:

Options Hash (**kwargs):

  • mode (:cbc, :cfb, :ofb, :ctr, Symbol) — default: :cbc

    The desired AES cipher mode.

  • :hash (Symbol) — default: :md5

    The algorithm to hash the :password.

  • :key (String)

    The secret key to use.

  • :password (String)

    The password for the cipher.

  • :iv (String)

    The optional Initial Vector (IV).

  • :padding (Integer)

    Sets the padding for the cipher.

Returns:

  • (String)

    The encrypted data.

Raises:

  • (ArgumentError)

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

Since:

  • 1.0.0



336
337
338
# 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) ⇒ String

Decrypts the String using AES-256.

Examples:

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

Parameters:

Options Hash (**kwargs):

  • mode (:cbc, :cfb, :ofb, :ctr, Symbol) — default: :cbc

    The desired AES cipher mode.

  • :hash (Symbol) — default: :sha256

    The algorithm to hash the :password.

  • :key (String)

    The secret key to use.

  • :password (String)

    The password for the cipher.

  • :iv (String)

    The optional Initial Vector (IV).

  • :padding (Integer)

    Sets the padding for the cipher.

Returns:

  • (String)

    The encrypted data.

Raises:

  • (ArgumentError)

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

Since:

  • 1.0.0



456
457
458
# 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) ⇒ String

Encrypts the String using AES-256.

Examples:

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

Parameters:

Options Hash (**kwargs):

  • mode (:cbc, :cfb, :ofb, :ctr, Symbol) — default: :cbc

    The desired AES cipher mode.

  • :hash (Symbol) — default: :sha256

    The algorithm to hash the :password.

  • :key (String)

    The secret key to use.

  • :password (String)

    The password for the cipher.

  • :iv (String)

    The optional Initial Vector (IV).

  • :padding (Integer)

    Sets the padding for the cipher.

Returns:

  • (String)

    The encrypted data.

Raises:

  • (ArgumentError)

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

Since:

  • 1.0.0



416
417
418
# 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) ⇒ String

Decrypts the String using AES.

Examples:

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

Parameters:

Options Hash (**kwargs):

  • :key_size (Integer)

    The desired key size in bits.

  • mode (:cbc, :cfb, :ofb, :ctr, Symbol) — default: :cbc

    The desired AES cipher mode.

  • :hash (Symbol) — default: :sha256

    The algorithm to hash the :password.

  • :key (String)

    The secret key to use.

  • :password (String)

    The password for the cipher.

  • :iv (String)

    The optional Initial Vector (IV).

  • :padding (Integer)

    Sets the padding for the cipher.

Returns:

  • (String)

    The encrypted data.

Raises:

  • (ArgumentError)

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

Since:

  • 1.0.0



296
297
298
# 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) ⇒ String

Encrypts the String using AES.

Examples:

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

Parameters:

Options Hash (**kwargs):

  • :key_size (Integer)

    The desired key size in bits.

  • mode (:cbc, :cfb, :ofb, :ctr, Symbol) — default: :cbc

    The desired AES cipher mode.

  • :hash (Symbol) — default: :sha256

    The algorithm to hash the :password.

  • :key (String)

    The secret key to use.

  • :password (String)

    The password for the cipher.

  • :iv (String)

    The optional Initial Vector (IV).

  • :padding (Integer)

    Sets the padding for the cipher.

Returns:

  • (String)

    The encrypted data.

Raises:

  • (ArgumentError)

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

Since:

  • 1.0.0



253
254
255
# File 'lib/ronin/support/crypto/core_ext/string.rb', line 253

def aes_encrypt(**kwargs)
  Ronin::Support::Crypto.aes_encrypt(self,**kwargs)
end

#base16_decodeString

Base16 decodes the String.

Examples:

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

Returns:

  • (String)

    The Base16 decodes String.

Since:

  • 1.0.0



59
60
61
# File 'lib/ronin/support/encoding/base16/core_ext/string.rb', line 59

def base16_decode
  Ronin::Support::Encoding::Base16.decode(self)
end

#base16_encodeString

Base16 encodes the String.

Examples:

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

Returns:

  • (String)

    The Base16 encoded String.

Since:

  • 1.0.0



39
40
41
# File 'lib/ronin/support/encoding/base16/core_ext/string.rb', line 39

def base16_encode
  Ronin::Support::Encoding::Base16.encode(self)
end

#base32_decodeString

Base32 decodes the String.

Examples:

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

Returns:

  • (String)

    The Base32 decodes String.

Since:

  • 1.0.0



59
60
61
# File 'lib/ronin/support/encoding/base32/core_ext/string.rb', line 59

def base32_decode
  Ronin::Support::Encoding::Base32.decode(self)
end

#base32_encodeString

Base32 encodes the String.

Examples:

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

Returns:

  • (String)

    The Base32 encoded String.

Since:

  • 1.0.0



39
40
41
# File 'lib/ronin/support/encoding/base32/core_ext/string.rb', line 39

def base32_encode
  Ronin::Support::Encoding::Base32.encode(self)
end

#base36_decodeInteger

Base36 decodes the String.

Examples:

"rs".base36_decode
# => 1000

Returns:

  • (Integer)

    The Base36 decoded Integer.

Since:

  • 1.1.0



39
40
41
# File 'lib/ronin/support/encoding/base36/core_ext/string.rb', line 39

def base36_decode
  Ronin::Support::Encoding::Base36.decode(self)
end

#base62_decodeInteger

Base62 decodes the String.

Examples:

"LZ".base62_decode
# => 1337

Returns:

  • (Integer)

    The Base62 decoded Integer.

Since:

  • 1.1.0



39
40
41
# 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) ⇒ String

Note:

mode argument is only available on Ruby >= 1.9.

Base64 decodes a string.

Examples:

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

Parameters:

  • mode (Symbol, nil) (defaults to: nil)

    The base64 mode to use. May be either:

    • nil
    • :strict
    • :url_safe

Returns:

  • (String)

    The base64 decoded form of the string.



72
73
74
# 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) ⇒ String

Base64 encodes a string.

Examples:

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

Parameters:

  • mode (Symbol, nil) (defaults to: nil)

    The base64 mode to use. May be either:

    • nil
    • :strict
    • :url_safe

Returns:

  • (String)

    The base64 encoded form of the string.



44
45
46
# 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_flipsArray<String> Also known as: flip_bits

Returns every bit flip of every byte in the string.

Examples:

bit-flip all bytes in the String:

"foo".bit_flips

Returns:



55
56
57
# 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

#c_encodeString

C escapes every character in the String.

Examples:

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

Returns:

  • (String)

    The C escaped String.

See Also:

Since:

  • 1.0.0



79
80
81
# File 'lib/ronin/support/encoding/c/core_ext/string.rb', line 79

def c_encode
  Ronin::Support::Encoding::C.encode(self)
end

#c_escapeString

Escapes a String for C.

Examples:

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

Returns:

  • (String)

    The C escaped String.

See Also:

Since:

  • 1.0.0



39
40
41
# File 'lib/ronin/support/encoding/c/core_ext/string.rb', line 39

def c_escape
  Ronin::Support::Encoding::C.escape(self)
end

#c_stringString

Converts the String into a C string.

Examples:

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

Returns:

See Also:

Since:

  • 1.0.0



100
101
102
# File 'lib/ronin/support/encoding/c/core_ext/string.rb', line 100

def c_string
  Ronin::Support::Encoding::C.quote(self)
end

#c_unescapeString Also known as: c_decode

Unescapes a C escaped String.

Examples:

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

Returns:

  • (String)

    The unescaped C String.

See Also:

Since:

  • 1.0.0



59
60
61
# File 'lib/ronin/support/encoding/c/core_ext/string.rb', line 59

def c_unescape
  Ronin::Support::Encoding::C.unescape(self)
end

#c_unquoteString

Removes the quotes an unescapes a C string.

Examples:

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

Returns:

  • (String)

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

See Also:

Since:

  • 1.0.0



121
122
123
# 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) ⇒ String

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

Parameters:

  • other (String)

    The other String to compare against.

Returns:

  • (String)

    The common prefix between the two Strings.



169
170
171
172
173
174
175
176
177
178
179
# 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) ⇒ String

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

Parameters:

  • other (String)

    The other String to compare against.

Returns:

  • (String)

    The common suffix of the two Strings.

Since:

  • 0.2.0



194
195
196
197
198
199
200
201
202
203
204
205
206
207
# 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) ⇒ String

Decrypts the String.

Examples:

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

Parameters:

Options Hash (**kwargs):

  • :hash (Symbol) — default: :sha1

    The algorithm to hash the :password.

  • :key (String)

    The secret key to use.

  • :password (String)

    The password for the cipher.

  • :iv (String)

    The optional Initial Vector (IV).

  • :padding (Integer)

    Sets the padding for the cipher.

Returns:

  • (String)

    The decrypted String.

See Also:

Since:

  • 0.6.0



208
209
210
211
212
# 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 {|string| ... } ⇒ Enumerator

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

Examples:

bit-flip all bytes in the String:

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

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.



40
41
42
# 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) {|homoglyph| ... } ⇒ Enumerator

Enumerates over every homoglyph variation of the String.

The character set to use.

Examples:

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

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • char_set (:ascii, :greek, :cyrillic, :punctuation, :latin_numbers, :full_width, nil)

Yields:

  • (homoglyph)

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

Yield Parameters:

  • homoglyph (String)

    A variation of the String.

Returns:

  • (Enumerator)

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

See Also:

Since:

  • 1.0.0



86
87
88
# 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) {|substring, (index)| ... } ⇒ String

Enumerates over every sub-string within the string.

Examples:

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

Parameters:

  • min (Integer) (defaults to: 1)

    Minimum length of each sub-string.

Yields:

  • (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.

Yield Parameters:

  • substring (String)

    A sub-string from the string.

  • index (Integer)

    The optional index of the sub-string.

Returns:

  • (String)

    The original string



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# 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) {|typoed| ... } ⇒ Enumerator

Enumerates over every typo mistake for the String.

Examples:

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

Parameters:

  • kwargs (Hash{Symbol => Boolean})

    Additional keyword arguments.

Options Hash (**kwargs):

  • omit (Boolean)

    Enables/disables omission of repeated characters.

  • repeat (Boolean)

    Enables/disables repeatition of single characters.

  • swap (Boolean)

    Enables/disables swapping of certain common character pairs.

  • suffix (Boolean)

    Enables/disables changing the suffixes of words.

Yields:

  • (typoed)

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

Yield Parameters:

  • A (String)

    modified version of the original String.

Returns:

  • (Enumerator)

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

See Also:

Since:

  • 1.0.0



97
98
99
# 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) {|substring, (index)| ... } ⇒ String

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

Examples:

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

Parameters:

  • min (Integer) (defaults to: 1)

    Minimum length of each unique sub-string.

Yields:

  • (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.

Yield Parameters:

  • substring (String)

    A unique sub-string from the string.

  • index (Integer)

    The optional index of the unique sub-string.

Returns:

  • (String)

    The original string

See Also:



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# 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) {|byte| ... } ⇒ String

Creates a new String by formatting each byte.

Examples:

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

Parameters:

Yields:

  • (byte)

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

Yield Parameters:

  • byte (Integer)

    The byte to format.

Returns:

  • (String)

    The formatted version of the String.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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) {|char| ... } ⇒ String

Creates a new String by formatting each character.

Examples:

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

Parameters:

Yields:

  • (char)

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

Yield Parameters:

  • char (String)

    The character to format.

Returns:

  • (String)

    The formatted version of the String.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# 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) ⇒ String

Encrypts the String.

Examples:

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

Parameters:

Options Hash (**kwargs):

  • :hash (Symbol) — default: :sha1

    The algorithm to hash the :password.

  • :key (String)

    The secret key to use.

  • :password (String)

    The password for the cipher.

  • :iv (String)

    The optional Initial Vector (IV).

  • :padding (Integer)

    Sets the padding for the cipher.

Returns:

  • (String)

    The encrypted String.

See Also:

Since:

  • 0.6.0



165
166
167
168
169
# 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) ⇒ Float

Calculates the entropy for the given string.

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :base (Integer) — default: 2

    The base to calculate the entropy for.

Returns:

  • (Float)

    The entropy for the string.

See Also:

Since:

  • 1.0.0



41
42
43
# File 'lib/ronin/support/text/entropy/core_ext/string.rb', line 41

def entropy(**kwargs)
  Ronin::Support::Text::Entropy.calculate(self,**kwargs)
end

#gunzipString

Gzip uncompresses the string.

Examples:

"\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"

Returns:

  • (String)

    The gunzipped version of the string.

Since:

  • 1.0.0



71
72
73
74
75
# 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

#gzipString

Gzip compresses the string.

Examples:

"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"

Returns:

  • (String)

    The gzipped version of the string.

Since:

  • 1.0.0



91
92
93
94
95
96
97
98
99
# 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_decodeString

Hex-decodes the String.

Examples:

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

Returns:

  • (String)

    The hex decoded version of the String.

See Also:

  • Ronin::Support::Encoding::Hex.decodde


53
54
55
# File 'lib/ronin/support/encoding/hex/core_ext/string.rb', line 53

def hex_decode
  Ronin::Support::Encoding::Hex.decode(self)
end

#hex_encodeString

Hex-encodes characters in the String.

Examples:

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

Returns:

  • (String)

    The hex encoded version of the String.

See Also:

  • Ronin::Support::Encoding::Hex.encodde

Since:

  • 0.6.0



37
38
39
# File 'lib/ronin/support/encoding/hex/core_ext/string.rb', line 37

def hex_encode
  Ronin::Support::Encoding::Hex.encode(self)
end

#hex_escapeString

Hex-escapes the characters within the String.

Examples:

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

Returns:

  • (String)

    The hex escaped version of the String.

See Also:



71
72
73
# File 'lib/ronin/support/encoding/hex/core_ext/string.rb', line 71

def hex_escape
  Ronin::Support::Encoding::Hex.escape(self)
end

#hex_stringString

Converts the String into a double-quoted hex string.

Examples:

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

Returns:

See Also:

Since:

  • 1.0.0



108
109
110
# File 'lib/ronin/support/encoding/hex/core_ext/string.rb', line 108

def hex_string
  Ronin::Support::Encoding::Hex.quote(self)
end

#hex_unescapeString

Unescapes the characters within the String.

Examples:

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

Returns:

  • (String)

    The hex unescaped version of the String.

See Also:



89
90
91
# File 'lib/ronin/support/encoding/hex/core_ext/string.rb', line 89

def hex_unescape
  Ronin::Support::Encoding::Hex.unescape(self)
end

#hex_unquoteString

Removes the quotes and unescapes a hex string.

Examples:

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

Returns:

  • (String)

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

See Also:

Since:

  • 1.0.0



129
130
131
# 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) ⇒ String

Calculates the HMAC of the String.

Parameters:

  • key (String)

    The secret key for the HMAC.

  • digest (Symbol) (defaults to: :sha1)

    The digest algorithm for the HMAC.

Returns:

  • (String)

    The hex-encoded HMAC for the String.

See Also:

Since:

  • 0.6.0



123
124
125
126
# 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) ⇒ String

Returns a random homoglyph substitution of the String.

The character set to use.

Examples:

"microsoft".homoglyph
# => "microsoft"

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • char_set (:ascii, :greek, :cyrillic, :punctuation, :latin_numbers, :full_width, nil)

Returns:

  • (String)

    A random homoglyphic variation of the String.

Raises:

See Also:

Since:

  • 1.0.0



52
53
54
# 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) {|homoglyph| ... } ⇒ Array<String>

Returns every homoglyph variation of the String.

The character set to use.

Examples:

"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"]

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • char_set (:ascii, :greek, :cyrillic, :punctuation, :latin_numbers, :full_width, nil)

Yields:

  • (homoglyph)

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

Returns:

  • (Array<String>)

    All variation of the given String.

See Also:

Since:

  • 1.0.0



137
138
139
# 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_encode(**kwargs) ⇒ String

Encodes the chars in the String for HTML.

Examples:

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

Zero-padding:

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

Hexadecimal encoded characters:

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

Uppercase hexadecimal encoded characters:

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

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :format (:decimal, :hex) — default: :decimal

    The numeric format for the escaped characters.

  • :zero_pad (Boolean)

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

  • :case (:lower, :upper, nil)

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

Returns:

  • (String)

    The encoded HTML String.

Raises:

  • (ArgumentError)

    The format: or case: keyword argument is invalid.

See Also:

Since:

  • 0.2.0



124
125
126
# 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) ⇒ String

HTML escapes the String.

Examples:

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

Uppercase escaped characters:

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

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :case (:lower, :upper, nil)

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

Returns:

  • (String)

    The HTML escaped String.

Raises:

  • (ArgumentError)

    The case: keyword argument is invalid.

See Also:

Since:

  • 0.2.0



54
55
56
# 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_unescapeString Also known as: html_decode

Unescapes the HTML encoded String.

Examples:

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

Returns:

  • (String)

    The unescaped String.

See Also:

Since:

  • 0.2.0



75
76
77
# File 'lib/ronin/support/encoding/html/core_ext/string.rb', line 75

def html_unescape
  Ronin::Support::Encoding::HTML.unescape(self)
end

#http_decodeString

HTTP decodes the HTTP encoded String.

Examples:

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

Returns:

  • (String)

    The decoded String.

See Also:



125
126
127
# 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) ⇒ String

HTTP encodes each byte of the String.

Examples:

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

Lowercase encoding:

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

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :case (:lower, :upper, nil)

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

Returns:

  • (String)

    The HTTP hexadecimal encoded form of the String.

Raises:

  • (ArgumentError)

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

See Also:

Since:

  • 1.0.0



107
108
109
# 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) ⇒ String

HTTP escapes the String.

Examples:

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

Lowercase encoding:

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

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :case (:lower, :upper, nil)

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

Returns:

  • (String)

    The HTTP escaped form of the String.

Raises:

  • (ArgumentError)

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

See Also:

Since:

  • 0.6.0



53
54
55
# 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_unescapeString

HTTP unescapes the String.

Examples:

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

Returns:

  • (String)

    The raw String.

See Also:

Since:

  • 0.6.0



73
74
75
# 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) ⇒ String

Inserts data after the occurrence of a pattern.

Parameters:

  • pattern (String, Regexp)

    The pattern to search for.

  • data (String)

    The data to insert after the pattern.

Returns:

  • (String)

    The new modified String.



264
265
266
267
268
269
270
271
272
273
274
275
# 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) ⇒ String

Inserts data before the occurrence of a pattern.

Parameters:

  • pattern (String, Regexp)

    The pattern to search for.

  • data (String)

    The data to insert before the pattern.

Returns:

  • (String)

    The new modified String.



242
243
244
245
246
247
248
# 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_encodeString

JavaScript escapes every character of the String.

Examples:

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

Returns:

  • (String)

    The JavaScript escaped String.

See Also:

Since:

  • 1.0.0



79
80
81
# File 'lib/ronin/support/encoding/js/core_ext/string.rb', line 79

def js_encode
  Ronin::Support::Encoding::JS.encode(self)
end

#js_escapeString

Escapes a String for JavaScript.

Examples:

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

Returns:

  • (String)

    The JavaScript escaped String.

See Also:

Since:

  • 0.2.0



39
40
41
# File 'lib/ronin/support/encoding/js/core_ext/string.rb', line 39

def js_escape
  Ronin::Support::Encoding::JS.escape(self)
end

#js_stringString

Converts the String into a JavaScript string.

Examples:

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

Returns:

See Also:

Since:

  • 1.0.0



100
101
102
# File 'lib/ronin/support/encoding/js/core_ext/string.rb', line 100

def js_string
  Ronin::Support::Encoding::JS.quote(self)
end

#js_unescapeString Also known as: js_decode

Unescapes a JavaScript escaped String.

Examples:

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

Returns:

  • (String)

    The unescaped JavaScript String.

See Also:

Since:

  • 0.2.0



59
60
61
# File 'lib/ronin/support/encoding/js/core_ext/string.rb', line 59

def js_unescape
  Ronin::Support::Encoding::JS.unescape(self)
end

#js_unquoteString

Removes the quotes an unescapes a JavaScript string.

Examples:

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

Returns:

  • (String)

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

See Also:

Since:

  • 1.0.0



121
122
123
# File 'lib/ronin/support/encoding/js/core_ext/string.rb', line 121

def js_unquote
  Ronin::Support::Encoding::JS.unquote(self)
end

#md5String

Returns The MD5 checksum of the String.

Examples:

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

Returns:

  • (String)

    The MD5 checksum of the String.



35
36
37
# File 'lib/ronin/support/crypto/core_ext/string.rb', line 35

def md5
  Digest::MD5.hexdigest(self)
end

#pad(padding, length) ⇒ String

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

Examples:

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

Parameters:

  • padding (String)

    The text to pad the new String with.

  • length (String)

    The maximum length to pad the new String out to.

Returns:

  • (String)

    The padded version of the String.



296
297
298
# File 'lib/ronin/support/core_ext/string.rb', line 296

def pad(padding,length)
  ljust(length,padding)
end

#powershell_encodeString Also known as: psh_encode

PowerShell encodes every character in the String.

Examples:

"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)"

Returns:

  • (String)

    The PowerShell encoded String.

See Also:

Since:

  • 1.0.0



89
90
91
# File 'lib/ronin/support/encoding/powershell/core_ext/string.rb', line 89

def powershell_encode
  Ronin::Support::Encoding::PowerShell.encode(self)
end

#powershell_escapeString Also known as: psh_escape

PowerShell escapes the characters in the String.

Examples:

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

Returns:

  • (String)

    The PowerShell escaped string.

See Also:

Since:

  • 1.0.0



41
42
43
# File 'lib/ronin/support/encoding/powershell/core_ext/string.rb', line 41

def powershell_escape
  Ronin::Support::Encoding::PowerShell.escape(self)
end

#powershell_stringString Also known as: psh_string

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

Examples:

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

Returns:

  • (String)

    The quoted and escaped PowerShell string.

See Also:

Since:

  • 1.0.0



114
115
116
# File 'lib/ronin/support/encoding/powershell/core_ext/string.rb', line 114

def powershell_string
  Ronin::Support::Encoding::PowerShell.quote(self)
end

#powershell_unescapeString Also known as: psh_unescape, powershell_decode

PowerShell unescapes the characters in the String.

Examples:

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

Returns:

  • (String)

    The PowerShell unescaped string.

See Also:

Since:

  • 1.0.0



65
66
67
# File 'lib/ronin/support/encoding/powershell/core_ext/string.rb', line 65

def powershell_unescape
  Ronin::Support::Encoding::PowerShell.unescape(self)
end

#powershell_unquoteString Also known as: psh_unquote

Removes the quotes an unescapes a PowerShell string.

Examples:

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

Returns:

  • (String)

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

See Also:

Since:

  • 1.0.0



139
140
141
# File 'lib/ronin/support/encoding/powershell/core_ext/string.rb', line 139

def powershell_unquote
  Ronin::Support::Encoding::PowerShell.unquote(self)
end

#punycode_decodeString

Decodes a punycode String back into unicode.

Examples:

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

Returns:

  • (String)

    The decoded unicode String.

Since:

  • 1.0.0



59
60
61
# File 'lib/ronin/support/encoding/punycode/core_ext/string.rb', line 59

def punycode_decode
  Ronin::Support::Encoding::Punycode.decode(self)
end

#punycode_encodeString

Encodes a unicode String into punycode.

Examples:

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

Returns:

  • (String)

    The punycode String.

Since:

  • 1.0.0



39
40
41
# File 'lib/ronin/support/encoding/punycode/core_ext/string.rb', line 39

def punycode_encode
  Ronin::Support::Encoding::Punycode.encode(self)
end

#quoted_printable_escapeString Also known as: quoted_printable_encode, qp_escape, qp_encode

Escapes the String as Quoted-Printable.

Examples:

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

Returns:

  • (String)

    The quoted-printable escaped String.

See Also:

Since:

  • 1.0.0



41
42
43
# 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_unescapeString Also known as: quoted_printable_decode, qp_decode, qp_unescape

Unescapes a Quoted-Printable encoded String.

Examples:

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

Returns:

  • (String)

    The unescaped String.

See Also:

Since:

  • 1.0.0



67
68
69
# 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_caseString

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

Examples:

"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."

Returns:

  • (String)

    The new String with randomized case.



42
43
44
# File 'lib/ronin/support/text/core_ext/string.rb', line 42

def random_case
  Ronin::Support::Text::Random.swapcase(self)
end

#rmd160String

Note:

JRuby and TruffleRuby do not yet support RMD160.

Calculates the RMD160 checksum for the String.

Examples:

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

Returns:

  • (String)

    The RMD160 checksum of the String.

Since:

  • 0.6.0



101
102
103
# File 'lib/ronin/support/crypto/core_ext/string.rb', line 101

def rmd160
  Digest::RMD160.hexdigest(self)
end

#rot(n = 13, **kwargs) ⇒ String

Note:

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

Rotates the characters in the string using the given alphabet.

Examples:

ROT13 "encryption":

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

ROT13 "decryption":

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

Parameters:

  • n (Integer) (defaults to: 13)

    The number of characters to shift each character by.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

Returns:

  • (String)

    The rotated string.

Since:

  • 1.0.0



551
552
553
# 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) ⇒ String

Decrypts the String using the given RSA key.

Optional padding mode. nil and false will disable padding.

Parameters:

Options Hash (**kwargs):

  • :key (String, nil)

    The PEM or DER encoded RSA key string.

  • :key_file (String, nil)

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

  • :key_password (String, nil)

    The optional password to decrypt the encrypted RSA key.

  • :padding (:pkcs1_oaep, :pkcs1, :sslv23, nil, false) — default: :pkcs1

Returns:

  • (String)

    The decrypted data.

Raises:

  • (ArgumentError)

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

Since:

  • 1.0.0



518
519
520
# 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) ⇒ String

Encrypts the String using the given RSA key.

Optional padding mode. nil and false will disable padding.

Parameters:

Options Hash (**kwargs):

  • :key (String, nil)

    The PEM or DER encoded RSA key string.

  • :key_file (String, nil)

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

  • :key_password (String, nil)

    The optional password to decrypt the encrypted RSA key.

  • :padding (:pkcs1_oaep, :pkcs1, :sslv23, nil, false) — default: :pkcs1

Returns:

  • (String)

    The encrypted data.

Raises:

  • (ArgumentError)

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

Since:

  • 1.0.0



487
488
489
# File 'lib/ronin/support/crypto/core_ext/string.rb', line 487

def rsa_encrypt(**kwargs)
  Ronin::Support::Crypto.rsa_encrypt(self,**kwargs)
end

#ruby_encodeString

Ruby escapes every character in the String.

Examples:

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

Returns:

  • (String)

    The Ruby escaped String.

See Also:

Since:

  • 1.0.0



83
84
85
# File 'lib/ronin/support/encoding/ruby/core_ext/string.rb', line 83

def ruby_encode
  Ronin::Support::Encoding::Ruby.encode(self)
end

#ruby_escapeString Also known as: escape

Escapes a String for Ruby.

Examples:

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

Returns:

  • (String)

    The Ruby escaped String.

See Also:

Since:

  • 1.0.0



39
40
41
# File 'lib/ronin/support/encoding/ruby/core_ext/string.rb', line 39

def ruby_escape
  Ronin::Support::Encoding::Ruby.escape(self)
end

#ruby_stringString

Rubyonverts the String into a Ruby string.

Examples:

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

Returns:

See Also:

Since:

  • 1.0.0



104
105
106
# File 'lib/ronin/support/encoding/ruby/core_ext/string.rb', line 104

def ruby_string
  Ronin::Support::Encoding::Ruby.quote(self)
end

#ruby_unescapeString Also known as: unescape, ruby_decode

Unescapes a Ruby escaped String.

Examples:

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

Returns:

  • (String)

    The unescaped Ruby String.

See Also:

Since:

  • 1.0.0



61
62
63
# File 'lib/ronin/support/encoding/ruby/core_ext/string.rb', line 61

def ruby_unescape
  Ronin::Support::Encoding::Ruby.unescape(self)
end

#ruby_unquoteString

Removes the quotes an unescapes a Ruby string.

Examples:

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

Returns:

  • (String)

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

See Also:

Since:

  • 1.0.0



125
126
127
# File 'lib/ronin/support/encoding/ruby/core_ext/string.rb', line 125

def ruby_unquote
  Ronin::Support::Encoding::Ruby.unquote(self)
end

#sha1String Also known as: sha128

Returns The SHA1 checksum of the String.

Examples:

"hello".sha1
# => "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d"

Returns:

  • (String)

    The SHA1 checksum of the String.



49
50
51
# File 'lib/ronin/support/crypto/core_ext/string.rb', line 49

def sha1
  Digest::SHA1.hexdigest(self)
end

#sha256String Also known as: sha2

Returns The SHA256 checksum of the String.

Examples:

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

Returns:

  • (String)

    The SHA256 checksum of the String.



65
66
67
# File 'lib/ronin/support/crypto/core_ext/string.rb', line 65

def sha256
  Digest::SHA256.hexdigest(self)
end

#sha512String

Returns The SHA512 checksum of the String.

Examples:

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

Returns:

  • (String)

    The SHA512 checksum of the String.



81
82
83
# File 'lib/ronin/support/crypto/core_ext/string.rb', line 81

def sha512
  Digest::SHA512.hexdigest(self)
end

#shell_encodeString

Shell encodes every character in the String.

Examples:

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

Returns:

  • (String)

    The shell encoded String.

See Also:

Since:

  • 1.0.0



81
82
83
# File 'lib/ronin/support/encoding/shell/core_ext/string.rb', line 81

def shell_encode
  Ronin::Support::Encoding::Shell.encode(self)
end

#shell_escapeString

Shell escapes the characters in the String.

Examples:

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

Returns:

  • (String)

    The shell escaped string.

See Also:

Since:

  • 1.0.0



41
42
43
# File 'lib/ronin/support/encoding/shell/core_ext/string.rb', line 41

def shell_escape
  Ronin::Support::Encoding::Shell.escape(self)
end

#shell_stringString

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

Examples:

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

Returns:

  • (String)

    The quoted and escaped shell string.

See Also:

Since:

  • 1.0.0



105
106
107
# File 'lib/ronin/support/encoding/shell/core_ext/string.rb', line 105

def shell_string
  Ronin::Support::Encoding::Shell.quote(self)
end

#shell_unescapeString Also known as: shell_decode

Shell unescapes the characters in the String.

Examples:

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

Returns:

  • (String)

    The shell unescaped string.

See Also:

Since:

  • 1.0.0



61
62
63
# File 'lib/ronin/support/encoding/shell/core_ext/string.rb', line 61

def shell_unescape
  Ronin::Support::Encoding::Shell.unescape(self)
end

#shell_unquoteString

Removes the quotes an unescapes a shell string.

Examples:

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

Returns:

  • (String)

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

See Also:

Since:

  • 1.0.0



130
131
132
# File 'lib/ronin/support/encoding/shell/core_ext/string.rb', line 130

def shell_unquote
  Ronin::Support::Encoding::Shell.unquote(self)
end

#sql_decodeObject

Returns the SQL decoded form of the String.

Examples:

"'Conan O''Brian'".sql_decode
# => "Conan O'Brian"
"2f6574632f706173737764".sql_decode
# => "/etc/passwd"

Raises:

  • The String is neither hex encoded or SQL escaped.

See Also:



110
111
112
# File 'lib/ronin/support/encoding/sql/core_ext/string.rb', line 110

def sql_decode
  Ronin::Support::Encoding::SQL.decode(self)
end

#sql_encodeObject

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

Examples:

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

See Also:



88
89
90
# 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) ⇒ String

Escapes an String for SQL.

Examples:

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

Encode with double-quotes:

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

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :quotes (:single, :double, :tick) — default: :single

    Specifies whether to create a single or double quoted string.

Returns:

  • (String)

    The escaped String.

Raises:

  • (ArgumentError)

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

See Also:



50
51
52
# 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_unescapeString

Unescapes a SQL String.

Examples:

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

Returns:

  • (String)

    The unescaped String.

Raises:

  • (ArgumentError)

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

See Also:

Since:

  • 1.0.0



73
74
75
# File 'lib/ronin/support/encoding/sql/core_ext/string.rb', line 73

def sql_unescape
  Ronin::Support::Encoding::SQL.unescape(self)
end

#to_asciiString

Converts the string into an ASCII encoded string.

Returns:

  • (String)

    The new ASCII string.

Since:

  • 1.0.0



48
49
50
# File 'lib/ronin/support/core_ext/string.rb', line 48

def to_ascii
  encode(Encoding::ASCII_8BIT)
end

#to_utf8String

Converts the string into an UTF-8 encoded string.

Returns:

  • (String)

    The new UTF-8 string.

Since:

  • 1.0.0



62
63
64
# File 'lib/ronin/support/core_ext/string.rb', line 62

def to_utf8
  encode(Encoding::UTF_8)
end

#typo(**kwargs) ⇒ String

Returns a random typo substitution for the String.

Examples:

"microsoft".typo
# => "microssoft"

Parameters:

  • kwargs (Hash{Symbol => Boolean})

    Additional keyword arguments.

Options Hash (**kwargs):

  • omit (Boolean)

    Enables/disables omission of repeated characters.

  • repeat (Boolean)

    Enables/disables repeatition of single characters.

  • swap (Boolean)

    Enables/disables swapping of certain common character pairs.

  • suffix (Boolean)

    Enables/disables changing the suffixes of words.

Returns:

  • (String)

    A random typo of the String.

See Also:

Since:

  • 1.0.0



54
55
56
# 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) ⇒ Array<String>

Returns every typo for the String.

Examples:

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

Parameters:

  • kwargs (Hash{Symbol => Boolean})

    Additional keyword arguments.

Options Hash (**kwargs):

  • omit (Boolean)

    Enables/disables omission of repeated characters.

  • repeat (Boolean)

    Enables/disables repeatition of single characters.

  • swap (Boolean)

    Enables/disables swapping of certain common character pairs.

  • suffix (Boolean)

    Enables/disables changing the suffixes of words.

Returns:

  • (Array<String>)

    Every typo variation of the String.

See Also:

Since:

  • 1.0.0



143
144
145
# 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) ⇒ String

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

Parameters:

  • other (String)

    The other String to compare against.

Returns:

  • (String)

    The uncommon sub-string between the two Strings.



221
222
223
224
225
226
# 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

#unhexdump(**kwargs) ⇒ String

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

Examples:

Unhexdump a hexdump created by GNU hexdump -C:

hexdump.unhexdump
# => "hello\n"

Unhexdump a hexdump created by GNU hexdump:

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

Unhexdump a hexdump created by od:

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

Parameters:

Options Hash (**kwargs):

  • :format (:od, :hexdump) — default: :hexdump

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

  • :type (Symbol) — default: :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
  • :address_base (2, 8, 10, 16, nil)

    The numerical base that the offset addresses are encoded in.

  • base (2, 8, 10, 16, nil)

    The numerical base that the hexdumped numbers are encoded in.

  • named_chars (Boolean)

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

Returns:

  • (String)

    The raw-data from the hexdump.

Raises:

  • (ArgumentError)

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



122
123
124
125
# 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) ⇒ Array

Unpacks the String.

The desired architecture that the data was packed for.

The Operating System (OS) to use.

Examples:

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

using a String#unpack format string:

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

Parameters:

Options Hash (**kwargs):

  • :endian (:little, :big, :net, nil)

    The desired endianness of the packed data.

  • :arch (:x86, :x86_64, :ppc, :ppc64, :arm, :arm_be, :arm64, :arm64_be, :mips, :mips_le, :mips64, :mips64_le, nil)
  • :os (:linux, :macos, :windows, :android, :apple_ios, :bsd, :freebsd, :openbsd, :netbsd)

Returns:

  • (Array)

    The values unpacked from the String.

Raises:

See Also:

Since:

  • 0.5.0



73
74
75
76
77
78
79
80
# 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

#unpack1(argument, **kwargs) ⇒ Integer, ...

Unpacks a single value from the String.

The desired architecture that the data was packed for.

Parameters:

Options Hash (**kwargs):

  • :endian (:little, :big, :net, nil)

    The desired endianness of the packed data.

  • :arch (:x86, :x86_64, :ppc, :ppc64, :arm, :arm_be, :arm64, :arm64_be, :mips, :mips_le, :mips64, :mips64_le, nil)

Returns:

Raises:

  • (ArgumentError)

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

Since:

  • 1.0.0



114
115
116
117
118
119
120
121
122
123
124
125
126
# 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

#unpack1_originalObject



82
# File 'lib/ronin/support/binary/core_ext/string.rb', line 82

alias unpack1_original unpack1

#unpack_originalObject



25
# File 'lib/ronin/support/binary/core_ext/string.rb', line 25

alias unpack_original unpack

#uri_encode(**kwargs) ⇒ String

URI encodes the String.

Examples:

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

Lowercase encoding:

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

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :case (:lower, :upper, nil)

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

Returns:

  • (String)

    The URI encoded form of the String.

See Also:



95
96
97
# 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) ⇒ String

URI escapes the String.

Examples:

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

Lowercase encoding:

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

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :case (:lower, :upper, nil)

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

Returns:

  • (String)

    The URI escaped form of the String.

See Also:



48
49
50
# 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_encode(**kwargs) ⇒ String Also known as: www_form_encode

URI Form encodes every character in the String.

Examples:

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

Lowercase encoding:

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

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :case (:lower, :upper, nil)

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

Returns:

  • (String)

    The URI Form encoded String.

See Also:

Since:

  • 1.0.0



186
187
188
# 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

#uri_form_escape(**kwargs) ⇒ String Also known as: www_form_escape

URI Form escapes the String.

Examples:

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

Lowercase encoding:

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

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :case (:lower, :upper, nil)

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

Returns:

  • (String)

    The URI Form escaped String.

See Also:

Since:

  • 1.0.0



131
132
133
# 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

#uri_form_unescapeString Also known as: www_form_unescape, uri_form_decode

URI Form unescapes the String.

Examples:

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

Returns:

  • (String)

    The URI Form unescaped String.

See Also:



153
154
155
# 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_unescapeString Also known as: uri_decode

URI unescapes the String.

Examples:

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

Returns:

  • (String)

    The unescaped URI form of the String.

See Also:



66
67
68
# File 'lib/ronin/support/encoding/uri/core_ext/string.rb', line 66

def uri_unescape
  Ronin::Support::Encoding::URI.unescape(self)
end

#uu_decodeString Also known as: uudecode, uu_unescape

Decodes the uuencoded String.

Examples:

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

Returns:

  • (String)

    The decoded String.

See Also:

Since:

  • 1.0.0



66
67
68
# File 'lib/ronin/support/encoding/uuencoding/core_ext/string.rb', line 66

def uu_decode
  Ronin::Support::Encoding::UUEncoding.decode(self)
end

#uu_encodeString Also known as: uuencode, uu_escape

uuencodes the String.

Examples:

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

Returns:

  • (String)

    The UU encoded String.

See Also:

Since:

  • 1.0.0



41
42
43
# File 'lib/ronin/support/encoding/uuencoding/core_ext/string.rb', line 41

def uu_encode
  Ronin::Support::Encoding::UUEncoding.encode(self)
end

#xml_encode(**kwargs) ⇒ String

Encodes each character in the String as an XML character.

Examples:

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

Zero-padding:

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

Hexadecimal encoded characters:

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

Uppercase hexadecimal encoded characters:

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

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :format (:decimal, :hex) — default: :decimal

    The numeric format for the escaped characters.

  • :zero_pad (Boolean)

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

  • :case (:lower, :upper, nil)

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

Returns:

  • (String)

    The XML encoded String.

See Also:

Since:

  • 1.0.0



118
119
120
# 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) ⇒ String

XML escapes the String.

Examples:

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

Uppercase escaped characters:

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

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :case (:lower, :upper, nil)

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

Returns:

  • (String)

    The XML escaped String.

See Also:

Since:

  • 0.2.0



51
52
53
# 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_unescapeString Also known as: xml_decode

Unescapes the XML encoded String.

Examples:

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

Returns:

  • (String)

    The unescaped String.

See Also:

Since:

  • 0.2.0



72
73
74
# File 'lib/ronin/support/encoding/xml/core_ext/string.rb', line 72

def xml_unescape
  Ronin::Support::Encoding::XML.unescape(self)
end

#xor(key) ⇒ String Also known as: ^

XOR encodes the String.

Examples:

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

Parameters:

Returns:

  • (String)

    The XOR encoded String.



574
575
576
# File 'lib/ronin/support/crypto/core_ext/string.rb', line 574

def xor(key)
  Ronin::Support::Crypto.xor(self,key)
end

#zlib_deflateString

Zlib deflate a string.

Examples:

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

Returns:

  • (String)

    The Zlib deflated form of the string.



53
54
55
# File 'lib/ronin/support/compression/core_ext/string.rb', line 53

def zlib_deflate
  Ronin::Support::Compression.zlib_deflate(self)
end

#zlib_inflateString

Zlib inflate a string.

Examples:

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

Returns:

  • (String)

    The Zlib inflated form of the string.



37
38
39
# File 'lib/ronin/support/compression/core_ext/string.rb', line 37

def zlib_inflate
  Ronin::Support::Compression.zlib_inflate(self)
end