lib/ctf_party/hex.rb in ctf-party-2.3.0 vs lib/ctf_party/hex.rb in ctf-party-3.0.0
- old
+ new
@@ -57,11 +57,11 @@
out = to_i.to_s(16)
# padding
out = ('0' * (opts[:padding] - out.size)) + out if out.size < opts[:padding]
# char case management
out = out.upcase if opts[:case] == :upper
- # adding prefix must be done after case change, complex conditional to avoid cropping when odd byte lenght
+ # adding prefix must be done after case change, complex conditional to avoid cropping when odd byte length
out = (out.size.odd? ? [out[0]] + out[1..].scan(/.{1,2}/) : out.scan(/.{2}/)).map do |x|
opts[:prefixall] + x
end.join
return opts[:prefix] + out
end
@@ -179,21 +179,27 @@
# Encode an hexadecimal string to a binary string
# @param opts [Hash] optional parameters
# @option opts [String] :prefix Prefix of the input. Default value is a void
# string. Example of values: `0x`, `\x`, `\\x`.
+ # @option opts [Integer] :even Returns an even number of chars (pad with `0`). Default value is a 1.
+ # `0` for false and `1` for true.
# @return [String] the binary encoded string
# @example
# 'ab'.hex2bin # => "10101011"
# '\xf3'.hex2bin(prefix: '\x') # => "11110011"
# '\\x6e\\x6f\\x72\\x61\\x6a'.hex2bin(prefix: '\\x') # => "110111001101111011100100110000101101010"
def hex2bin(opts = {})
opts[:prefix] ||= ''
+ opts[:even] ||= 1
# remove prefix
out = gsub(opts[:prefix], '')
# convert
- return out.to_i(16).to_s(2)
+ out = out.to_i(16).to_s(2)
+ # padding
+ out = "0#{out}" if out.size.odd? && opts[:even] == 1
+ return out
end
# Encode an hexadecimal string to a binary string in place as described
# for {String#hex2bin}.
# @example
@@ -223,12 +229,14 @@
opts[:case] ||= :lower
# convert
out = to_i(2).to_s(16)
# char case management
out = out.upcase if opts[:case] == :upper
- # adding prefix must be done after case change
- out = out.scan(/.{2}/).map { |x| opts[:prefixall] + x }.join
+ # adding prefix must be done after case change, complex conditional to avoid cropping when odd byte length
+ out = (out.size.odd? ? [out[0]] + out[1..].scan(/.{1,2}/) : out.scan(/.{2}/)).map do |x|
+ opts[:prefixall] + x
+ end.join
return opts[:prefix] + out
end
# Encode an binary string to a hexadecimal string in place as described
# for {String#bin2hex}.
@@ -238,54 +246,85 @@
# a # => "f3"
def bin2hex!(opts = {})
replace(bin2hex(opts))
end
- # Decode a hexadecimal IP string into a dotted decimal one
+ # Decode a hexadecimal IPv4 string into a dotted decimal one
# @param opts [Hash] optional parameters
# @option opts [String] :prefix Prefix of the input. Default value is a void
# string. Example of values: `0x`, `\x`, '\\x'.
# @option opts [Symbol] :nibble Display input with high nibble first
# (`:high` default) or low nibble first (`:low`, used on Unix `/proc/net/tcp`).
# @return [String] the dotted decimal IP
# @example
- # '0100007F'.from_hexip(nibble: :low) # => "127.0.0.1"
- # '0x7f000001'.from_hexip(prefix: '0x') # => "127.0.0.1"
- # '\\x7f\\x00\\x00\\x01'.from_hexip(prefix: '\\x') # => "127.0.0.1"
- def from_hexip(opts = {})
+ # '0100007F'.from_hexipv4(nibble: :low) # => "127.0.0.1"
+ # '0x7f000001'.from_hexipv4(prefix: '0x') # => "127.0.0.1"
+ # '\\x7f\\x00\\x00\\x01'.from_hexipv4(prefix: '\\x') # => "127.0.0.1"
+ def from_hexipv4(opts = {})
opts[:prefix] ||= ''
opts[:nibble] ||= :high
# remove prefix
out = gsub(opts[:prefix], '')
# convert
out = out.scan(/.{2}/).map(&:hex2dec)
out = out.reverse if opts[:nibble] == :low
out.join('.')
end
- # Decode a hexadecimal IP string into a dotted decimal one in place as described
- # for {String#from_hexip}.
- def from_hexip!(opts = {})
- replace(from_hexip(opts))
+ alias from_hexip from_hexipv4
+
+ # Decode a hexadecimal IPv4 string into a dotted decimal one in place as described
+ # for {String#from_hexipv4}.
+ def from_hexipv4!(opts = {})
+ replace(from_hexipv4(opts))
end
- # Encode a dotted decimal IP into a hexadecimal one
+ alias from_hexip! from_hexipv4!
+
+ # Decode a hexadecimal IPv6 string into a the double-dotted hexadecimal format
# @param opts [Hash] optional parameters
+ # @option opts [String] :prefix Prefix of the input. Default value is a void
+ # string. Example of values: `0x`, `\x`, '\\x'.
+ # @return [String] the double-dotted hexadecimal format
+ # @example
+ # '000080FE00000000FF005450B6AD1DFE'.from_hexipv6 # => "[fe80::5054:ff:fe1d:adb6]"
+ # '0x000080FE00000000FF005450B6AD1DFE'.from_hexipv6(prefix: '0x') # => "[fe80::5054:ff:fe1d:adb6]"
+ # '00000000000000000000000000000000'.from_hexipv6 # => "[::]"
+ def from_hexipv6(opts = {})
+ opts[:prefix] ||= ''
+ # remove prefix
+ out = gsub(opts[:prefix], '')
+ # convert
+ out = out.scan(/.{2}/).reverse.join
+ out = out.scan(/.{8}/).reverse.join
+ out = out.scan(/.{4}/).map { |x| x.sub(/^0+/, '') }.join(':')
+ out = out.sub(/:{3,}/, '::').downcase
+ "[#{out}]"
+ end
+
+ # Decode a hexadecimal IPv6 string into a the double-dotted hexadecimal format in place as described
+ # for {String#from_hexipv6}.
+ def from_hexipv6!(opts = {})
+ replace(from_hexipv6(opts))
+ end
+
+ # Encode a dotted decimal IPv4 into a hexadecimal one
+ # @param opts [Hash] optional parameters
# @option opts [String] :prefix Prefix of the output. Default value is a void
# string. Example of values: `0x`, `\x`.
# @option opts [String] :prefixall Prefix each byte. Default value is a void
# string. Example of value: `\\x`.
# @option opts [Symbol] :case Char case of the output. Default value `:lower`.
# Other valid value `:upper`.
# @option opts [Symbol] :nibble Display output with high nibble first
# (`:high` default) or low nibble first (`:low`, used on Unix `/proc/net/tcp`).
# @return [String] the hexadecimal encoded IP
# @example
- # '127.0.0.1'.to_hexip # => "7f000001"
- # '127.0.0.1'.to_hexip(nibble: :low) # => "0100007f"
- # '127.0.0.1'.to_hexip(prefixall: '\\x') # => "\\x7f\\x00\\x00\\x01"
- def to_hexip(opts = {})
+ # '127.0.0.1'.to_hexipv4 # => "7f000001"
+ # '127.0.0.1'.to_hexipv4(nibble: :low) # => "0100007f"
+ # '127.0.0.1'.to_hexipv4(prefixall: '\\x') # => "\\x7f\\x00\\x00\\x01"
+ def to_hexipv4(opts = {})
opts[:prefix] ||= ''
opts[:prefixall] ||= ''
opts[:case] ||= :lower
opts[:nibble] ||= :high
# convert
@@ -297,11 +336,15 @@
# adding prefix must be done after case change
out = out.scan(/.{2}/).map { |x| opts[:prefixall] + x }.join
return opts[:prefix] + out
end
- # Encode a dotted decimal IP into a hexadecimal one in place as described
- # for {String#to_hexip}.
- def to_hexip!(opts = {})
- replace(to_hexip(opts))
+ alias to_hexip to_hexipv4
+
+ # Encode a dotted decimal IPv4 into a hexadecimal one in place as described
+ # for {String#to_hexipv4}.
+ def to_hexipv4!(opts = {})
+ replace(to_hexipv4(opts))
end
+
+ alias to_hexip! to_hexipv4!
end