module Ronin::Support::Encoding::Hex
Contains methods for hex encoding/decoding and escaping/unescaping data.
## Core-Ext Methods
-
{Integer#hex_encode}
-
{Integer#hex_escape}
-
{Integer#hex_int}
-
{String#hex_encode}
-
{String#hex_decode}
-
{String#hex_escape}
-
{String#hex_unescape}
-
{String#hex_string}
-
{String#hex_unquote}
Constants
- BACKSLASHED_CHARS
-
Backslash escaped characters.
Public Class Methods
Source
# File lib/ronin/support/encoding/hex.rb, line 127 def self.decode(data) decoded = String.new data.scan(/../) do |hex| decoded << hex.to_i(16).chr end return decoded end
@param [String] data
The given data to hex decode.
@return [String]
The hex decoded version of the String.
@example
Encoding::Hex.decode("68656C6C6F") # => "hello"
Source
# File lib/ronin/support/encoding/hex.rb, line 104 def self.encode(data) encoded = String.new data.each_byte do |byte| encoded << encode_byte(byte) end return encoded end
Hex
encodes the given data.
@param [String] data
The given data to hex encode.
@return [String]
The hex encoded version of the String.
@example
Encoding::Hex.encode("hello") # => "68656C6C6F"
Source
# File lib/ronin/support/encoding/hex.rb, line 53 def self.encode_byte(byte) "%.2x" % byte end
Hex
encodes the given byte.
@param [Integer] byte
The byte to hex encode.
@return [String]
The hex encoded version of the byte.
@example
Encoding::Hex.encode_byte(0x41) # => "41"
Source
# File lib/ronin/support/encoding/hex.rb, line 150 def self.escape(data) escaped = String.new if data.valid_encoding? data.each_codepoint do |codepoint| escaped << escape_byte(codepoint) end else data.each_byte do |byte| escaped << escape_byte(byte) end end return escaped end
Hex-escapes the given data.
@param [String] data
The given data to hex escape.
@return [String]
The hex escaped version of the String.
@example
Encoding::Hex.escape("hello") # => "\\x68\\x65\\x6c\\x6c\\x6f"
Source
# File lib/ronin/support/encoding/hex.rb, line 73 def self.escape_byte(byte) if byte >= 0x00 && byte <= 0xff if byte == 0x22 "\\\"" elsif byte == 0x5d "\\\\" elsif byte >= 0x20 && byte <= 0x7e byte.chr else "\\x%.2x" % byte end elsif byte > 0xff "\\x%x" % byte else raise(RangeError,"#{byte.inspect} out of char range") end end
Hex
escapes the given byte.
@param [Integer] byte
The byte value to hex escape.
@return [String]
The hex escaped version of the Integer.
@raise [RangeError]
The byte value is negative.
@example
Encoding::Hex.escape_byte(42) # => "\\x2a"
Source
# File lib/ronin/support/encoding/hex.rb, line 228 def self.quote(data) "\"#{escape(data)}\"" end
Converts the given data into a double-quoted hex string.
@param [String] data
The given data to hex-escape and double-quote.
@return [String]
The double-quoted and hex escaped string.
@example
Encoding::Hex.quote("hello\nworld") # => "\"hello\\nworld\""
Source
# File lib/ronin/support/encoding/hex.rb, line 194 def self.unescape(data) buffer = String.new(encoding: Encoding::UTF_8) scanner = StringScanner.new(data) until scanner.eos? buffer << if (unicode_escape = scanner.scan(/\\x[0-9a-fA-F]{4,8}/)) unicode_escape[2..].to_i(16).chr(Encoding::UTF_8) elsif (hex_escape = scanner.scan(/\\x[0-9a-fA-F]{1,2}/)) hex_escape[2..].to_i(16).chr elsif (backslash_escape = scanner.scan(/\\./)) BACKSLASHED_CHARS.fetch(backslash_escape) do backslash_escape[1] end else scanner.getch end end return buffer end
Removes the quotes an unescapes a quoted hex string.
@param [String] data
The quoted and hex escaped 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
Encoding::Hex.unescape("hello\\nworld") # => "hello\nworld"
Source
# File lib/ronin/support/encoding/hex.rb, line 246 def self.unquote(data) if ((data.start_with?('"') && data.end_with?('"')) || (data.start_with?("'") && data.end_with?("'"))) unescape(data[1..-2]) else data end end
Removes the quotes and unescapes a hex string.
@param [String] data
The quoted hex String to unescape and unquote.
@return [String]
The un-quoted String if the String begins and ends with quotes, or the same String if it is not quoted.
@example
Encoding::Hex.unquote("\"hello\\nworld\"") # => "hello\nworld"