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
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
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
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
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
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
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
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
-
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
-
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
-
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
-
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
-
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
-
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
-
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
-
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 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-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-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-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
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
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
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
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
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
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
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 # => "abc"
@example Zero-padding:
"abc".html_encode(zero_pad: true) # => "abc"
@example Hexadecimal encoded characters:
"abc".html_encode(format: :hex) # => "abc"
@example Uppercase hexadecimal encoded characters:
"abc\xff".html_encode(format: :hex, case: :upper) # => "abcÿ"
@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 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 & two"
@example Uppercase escaped characters:
"one & two".html_escape(case: :upper) # => "one & 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
Unescapes the HTML encoded String
.
@return [String]
The unescaped String.
@example
"<p>one <span>two</span></p>".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
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 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 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 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
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
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
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
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
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
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
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
@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
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 encodes][1
-
every character in the
String
.
@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
- PowerShell escapes][1
-
the characters in the
String
.
@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
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
- PowerShell unescapes][1
-
the characters in the
String
.
@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
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
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
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
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
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
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
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
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
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
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 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
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
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
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
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
@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
@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
@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 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 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
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 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
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
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
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
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
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
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
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
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
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
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
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
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
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
@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
@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 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
@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
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
@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
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
- 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
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 # => "abc"
@example Zero-padding:
"abc".xml_encode(zero_pad: true) # => "abc"
@example Hexadecimal encoded characters:
"abc".xml_encode(format: :hex) # => "abc"
@example Uppercase hexadecimal encoded characters:
"abc\xff".xml_encode(format: :hex, case: :upper) # => "abcÿ"
@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 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 & two"
@example Uppercase escaped characters:
"one & two".xml_escape(case: :upper) # => "one & 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
Unescapes the XML encoded String
.
@return [String]
The unescaped String.
@example
"<p>one <span>two</span></p>".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
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
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 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