class String

Public Class Methods

flag() click to toggle source

Show the actual flag configuration. See {.flag=}.

# File lib/ctf_party/flag.rb, line 15
def self.flag
  @@flag
end
flag=(hash) click to toggle source

Update the flag configuration. @param hash [Hash] flag configuration @option hash [String] :prefix prefix of the flag. Default: none. @option hash [String] :suffix suffix of the flag. Default: none. @option hash [Array<String>] :enclosing the characters used to surround

the flag. Default are curly braces: +{+, +}+. The array must contain
exactly 2 elements.

@option hash [String] :digest the hash algorithm to apply on the flag.

Default: none. Allowed values: md5, sha1, sha2_256, sha2_384, sha2_512,
rmd160.

@return [Hash] hash of the updated options. @note You can provide the full hash or only the key to update. @example

String.flag # => {:prefix=>"", :suffix=>"", :enclosing=>["{", "}"], :digest=>nil}
String.flag = {prefix: 'sigsegv', digest: 'md5'}
String.flag # => {:prefix=>"sigsegv", :suffix=>"", :enclosing=>["{", "}"], :digest=>"md5"}
'this_1s_a_fl4g'.flag # => "sigsegv{a5bec9e2a86b6b70d288451eb38dfec8}"
# File lib/ctf_party/flag.rb, line 36
def self.flag=(hash)
  hash.select! { |k, _v| @@flag.key?(k) }
  @@flag.merge!(hash)
end

Public Instance Methods

alternatecase(shift = 0) click to toggle source

Change one characte on two upcase and the other downcase @param shift [Integer] 0: 1st character will be downcase, 1: 1st character

will be upcase

@return [String] the case modified string @example

'SELECT * FROM'.alternatecase # => "sElEcT * FrOm"
'SELECT * FROM'.alternatecase(1) # => "SeLeCt * fRoM"
# File lib/ctf_party/case.rb, line 26
def alternatecase(shift = 0)
  chars.each_with_index.map { |c, i| (i + shift).even? ? c.downcase : c.upcase }.join
end
alternatecase!(shift = 0) click to toggle source

Change one characte on two upcase and the other downcase in place as described for {String#alternatecase}.

# File lib/ctf_party/case.rb, line 32
def alternatecase!(shift = 0)
  replace(alternatecase(shift))
end
alxor(key) click to toggle source

ASCII XOR with key, padding left the shortest element @param key [String] the element to xor the string with @return [String] the xored string (ASCII) @example

'hello'.alxor('key') # => "he\a\t\x16"
'key'.alxor('hello') # => "he\a\t\x16"
# File lib/ctf_party/xor.rb, line 30
def alxor(key)
  b1 = force_encoding('UTF-8')
  b2 = key.force_encoding('UTF-8')
  raise 'The string is not ASCII' unless b1.ascii_only?
  raise 'The key is not ASCII' unless b2.ascii_only?

  b1 = b1.chars.map(&:ord)
  b2 = b2.chars.map(&:ord)
  longest = [b1.length, b2.length].max
  b1 = [0] * (longest - b1.length) + b1
  b2 = [0] * (longest - b2.length) + b2
  b1.zip(b2).map { |a, b| (a ^ b).chr }.join
end
alxor!(key) click to toggle source

ASCII XOR with key (padding left) in place as described for {String#alxor}.

# File lib/ctf_party/xor.rb, line 45
def alxor!(key)
  replace(alxor(key))
end
arxor(key) click to toggle source

ASCII XOR with key, padding right the shortest element @param key [String] the element to xor the string with @return [String] the xored string (ASCII) @example

'hello'.arxor('key') # => "\x03\x00\x15lo"
'key'.arxor('hello') # => "\x03\x00\x15lo"
# File lib/ctf_party/xor.rb, line 75
def arxor(key)
  b1 = force_encoding('UTF-8')
  b2 = key.force_encoding('UTF-8')
  raise 'The string is not ASCII' unless b1.ascii_only?
  raise 'The key is not ASCII' unless b2.ascii_only?

  b1 = b1.chars.map(&:ord)
  b2 = b2.chars.map(&:ord)
  longest = [b1.length, b2.length].max
  b1 += [0] * (longest - b1.length)
  b2 += [0] * (longest - b2.length)
  b1.zip(b2).map { |a, b| (a ^ b).chr }.join
end
arxor!(key) click to toggle source

ASCII XOR with key (padding right) in place as described for {String#arxor}.

# File lib/ctf_party/xor.rb, line 90
def arxor!(key)
  replace(arxor(key))
end
b64?(opts = {}) click to toggle source

Is the string encoded in base64? @param opts [Hash] optional parameters @option opts [Symbol] :mode Default value: :strict.

Other values are +:strict+ (+:rfc4648+) or +:urlsafe+.

@see ruby-doc.org/stdlib-2.6.5/libdoc/base64/rdoc/Base64.html @return [Boolean] true if the string is a valid base64 string, false

else.

@example

'SGVsbG8gd29ybGQh'.b64? # => true
'SGVsbG8g@@d29ybGQh'.b64? # => false
# File lib/ctf_party/base64.rb, line 69
def b64?(opts = {})
  opts[:mode] ||= :strict
  b64 = false
  # https://www.rexegg.com/regex-ruby.html
  reg1 = %r{\A(?:[a-zA-Z0-9+/]{4})*(?:|(?:[a-zA-Z0-9+/]{3}=)|
            (?:[a-zA-Z0-9+/]{2}==)|(?:[a-zA-Z0-9+/]{1}===))\Z}xn
  reg3 = /\A(?:[a-zA-Z0-9\-_]{4})*(?:|(?:[a-zA-Z0-9\-_]{3}=)|
          (?:[a-zA-Z0-9\-_]{2}==)|(?:[a-zA-Z0-9\-_]{1}===))\Z/xn
  case opts[:mode]
  when :strict, :rfc4648
    b64 = true if reg1.match?(self)
  when :rfc2045
    b64 = true
    split("\n").each do |s|
      b64 = false unless reg1.match?(s)
    end
  when :urlsafe
    b64 = true if reg3.match?(self)
  else
    raise ArgumentError 'Wrong mode'
  end
  return b64
end
bin2hex(opts = {}) click to toggle source

Encode an binary string to a hexadecimal string @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 [Symbol] :case Char case of the ouput. Default value :lower.

Other valid value +:upper+.

@return [String] the hexadecimal encoded string @example

'11110011'.bin2hex # => "f3"
'11110011'.bin2hex({prefix: '0x', case: :upper}) # => "0xF3"
# File lib/ctf_party/hex.rb, line 197
def bin2hex(opts = {})
  opts[:prefix] ||= ''
  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
  return opts[:prefix] + out
end
bin2hex!(opts = {}) click to toggle source

Encode an binary string to a hexadecimal string in place as described for {String#bin2hex}. @example

a = '11110011'
a.bin2hex!
a # => "f3"
# File lib/ctf_party/hex.rb, line 214
def bin2hex!(opts = {})
  replace(bin2hex(opts))
end
bin2str(opts = {}) click to toggle source

Alias for {String#from_bin}.

# File lib/ctf_party/binary.rb, line 59
def bin2str(opts = {})
  from_bin(opts)
end
bin2str!(opts = {}) click to toggle source

Alias for {String#from_bin!}.

# File lib/ctf_party/binary.rb, line 74
def bin2str!(opts = {})
  from_bin!(opts)
end
dec2hex(opts = {}) click to toggle source

Encode an decimal string to a hexadecimal string @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 [Symbol] :case Char case of the ouput. Default value :lower.

Other valid value +:upper+.

@option opts [Symbol] :padding Minimum size of the hexadecimal display

(number of characters). Eg. 10 -> 0xA or 0x0A

@return [String] the hexadecimal encoded string @example

'255'.dec2hex # => "ff"
'255'.dec2hex({prefix: '0x', case: :upper}) # => "0xFF"
# File lib/ctf_party/hex.rb, line 42
def dec2hex(opts = {})
  opts[:prefix] ||= ''
  opts[:case] ||= :lower
  opts[:padding] ||= 1
  # convert
  out = to_i.to_s(16)
  # padding
  out = ('0' * (opts[:padding] - 1)) + out if out.size < opts[:padding]
  # char case management
  out = out.upcase if opts[:case] == :upper
  # adding prefix must be done after case change
  return opts[:prefix] + out
end
dec2hex!(opts = {}) click to toggle source

Encode an decimal string to a hexadecimal string in place as described for {String#dec2hex}. @example

a = '255'
a.dec2hex!
a # => "ff"
# File lib/ctf_party/hex.rb, line 62
def dec2hex!(opts = {})
  replace(dec2hex(opts))
end
dec2str() click to toggle source

Alias for {String#from_dec}.

# File lib/ctf_party/dec.rb, line 41
def dec2str
  from_dec
end
dec2str!() click to toggle source

Alias for {String#from_dec!}.

# File lib/ctf_party/dec.rb, line 46
def dec2str!
  replace(dec2str)
end
flag() click to toggle source

Format the current string into the configured flag format. See {.flag=} example. @return [String] the format flag.

# File lib/ctf_party/flag.rb, line 44
def flag
  flag = ''
  flag += @@flag[:prefix]
  flag += @@flag[:enclosing][0]
  if @@flag[:digest].nil?
    flag += self
  else
    case @@flag[:digest]
    when 'md5'
      flag += md5
    when 'sha1'
      flag += sha1
    when 'sha2_256'
      flag += sha2_256
    when 'sha2_384'
      flag += sha2_384
    when 'sha2_512'
      flag += sha2_512
    when 'rmd160'
      flag += rmd160
    end
  end
  flag += @@flag[:enclosing][1]
  flag + @@flag[:suffix]
end
flag!() click to toggle source

Format the current string into the configured flag format in place as described for {String#flag}.

# File lib/ctf_party/flag.rb, line 72
def flag!
  replace(flag)
end
flag?() click to toggle source

Check if the string respect the defined flag format. @return [Boolean] true if it respects the configured flag format. but it

does not check digest used.

@example

String.flag = {prefix: 'flag'}
flag = 'Brav0!'
flag.flag! # => "flag{Brav0!}"
flag.flag? # => true
flag = 'ctf{Brav0!}'
flag.flag? # => false
# File lib/ctf_party/flag.rb, line 86
def flag?
  /#{@@flag[:prefix]}#{@@flag[:enclosing][0]}[[:print:]]+
    #{@@flag[:enclosing][1]}#{@@flag[:suffix]}/ox.match?(self)
end
from_b64(opts = {}) click to toggle source

Decode the string from base64 @param opts [Hash] optional parameters @option opts [Symbol] :mode Default value: :strict.

Other values are +:strict+ (+:rfc4648+) or +:urlsafe+.

@see ruby-doc.org/stdlib-2.6.5/libdoc/base64/rdoc/Base64.html @return [String] the Base64 decoded string @example

'UnVieQ=='.from_b64 # => "Ruby"
# File lib/ctf_party/base64.rb, line 41
def from_b64(opts = {})
  opts[:mode] ||= :strict
  return Base64.strict_decode64(self) if opts[:mode] == :strict ||
                                         opts[:mode] == :rfc4648
  return Base64.decode64(self) if opts[:mode] == :rfc2045
  return Base64.urlsafe_decode64(self) if opts[:mode] == :urlsafe
end
from_b64!(opts = {}) click to toggle source

Decode the string from base64 in place as described for {String#from_b64}. @return [nil] @example

a = 'SGVsbG8gd29ybGQh' # => "SGVsbG8gd29ybGQh"
a.from_b64! # => nil
a # => "Hello world!"
# File lib/ctf_party/base64.rb, line 55
def from_b64!(opts = {})
  replace(from_b64(opts))
end
from_bin(opts = {}) click to toggle source

Decode a binary string @param opts [Hash] optional parameters @option opts [Symbol] :bitnumbering Display input with most significant bit

first (+:MSB+ default) or least significant bit first (+:LSB+).

@return [String] the binary decoded string @example

'011000100110100101101110011000010111001001111001'.from_bin # => "binary"
'010001101001011001110110100001100100111010011110'.from_bin(bitnumbering: :LSB) # => "binary"
# File lib/ctf_party/binary.rb, line 49
def from_bin(opts = {})
  opts[:bitnumbering] ||= :MSB
  # convert
  return Array(self).pack('B*') if opts[:bitnumbering] == :MSB
  return Array(self).pack('b*') if opts[:bitnumbering] == :LSB

  raise ArgumentError ':bitnumbering expects :MSB or :LSB'
end
from_bin!(opts = {}) click to toggle source

Decode a binary string in place as described for {String#from_bin}. @example

a = "011000100110100101101110011000010111001001111001"
a.from_bin!
a # => "binary"
# File lib/ctf_party/binary.rb, line 69
def from_bin!(opts = {})
  replace(from_bin(opts))
end
from_dec() click to toggle source

Decode a decimal string (decimal to hexadecimal then hexadecimal to string) @return [String] the decimal decoded string @example

'1834615104613964215417'.from_dec # => "ctf-party"
# File lib/ctf_party/dec.rb, line 21
def from_dec
  dec2hex.hex2str
end
from_dec!() click to toggle source

Decode a decimal string in place as described for {String#from_dec}.

# File lib/ctf_party/dec.rb, line 26
def from_dec!
  replace(from_dec)
end
from_hex(opts = {}) click to toggle source

Decode a hexadecimal 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+.

@option opts [Symbol] :nibble Display input with high nibble first

(+:high+ default) or low nibble first (+:low+).

@return [String] the hexadecimal decoded string @example

"6e6f72616a".from_hex # => "noraj"
"0x6e6f72616a".from_hex(prefix: '0x') # => "noraj"
"e6f62716a6".from_hex(nibble: :low) # => "noraj"
# File lib/ctf_party/hex.rb, line 129
def from_hex(opts = {})
  opts[:prefix] ||= ''
  opts[:nibble] ||= :high
  # remove prefix
  out = sub(opts[:prefix], '')
  # convert
  return Array(out).pack('H*') if opts[:nibble] == :high
  return Array(out).pack('h*') if opts[:nibble] == :low

  raise ArgumentError ':nibble expects :high or :low'
end
from_hex!(opts = {}) click to toggle source

Decode a hexadecimal string in place as described for {String#from_hex}. @example

a = "6e6f72616a"
a.from_hex!
a # => "noraj"
# File lib/ctf_party/hex.rb, line 152
def from_hex!(opts = {})
  replace(from_hex(opts))
end
from_hexip(opts = {}) click to toggle source

Decode a hexadecimal IP 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+.

@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"
# File lib/ctf_party/hex.rb, line 228
def from_hexip(opts = {})
  opts[:prefix] ||= ''
  opts[:nibble] ||= :high
  # remove prefix
  out = sub(opts[:prefix], '')
  # convert
  out = out.scan(/.{2}/).map(&:hex2dec)
  out = out.reverse if opts[:nibble] == :low
  out.join('.')
end
from_hexip!(opts = {}) click to toggle source

Decode a hexadecimal IP string into a dotted decimal one in place as described for {String#from_hexip}.

# File lib/ctf_party/hex.rb, line 241
def from_hexip!(opts = {})
  replace(from_hexip(opts))
end
hex2bin(opts = {}) click to toggle source

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

@return [String] the binary encoded string @example

'ab'.hex2bin # => "10101011"
'\xf3'.hex2bin(prefix: '\x') # => "11110011"
# File lib/ctf_party/hex.rb, line 169
def hex2bin(opts = {})
  opts[:prefix] ||= ''
  # remove prefix
  out = sub(opts[:prefix], '')
  # convert
  return out.to_i(16).to_s(2)
end
hex2bin!(opts = {}) click to toggle source

Encode an hexadecimal string to a binary string in place as described for {String#hex2bin}. @example

a = 'ff'
a.hex2bin!
a # => => "11111111"
# File lib/ctf_party/hex.rb, line 183
def hex2bin!(opts = {})
  replace(hex2bin(opts))
end
hex2dec(opts = {}) click to toggle source

Encode an hexadecimal string to a decimal 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+.

@return [String] the decimal encoded string @example

'ff'.hex2dec # => "255"
'\xf3'.hex2dec(prefix: '\x') # => "243"
# File lib/ctf_party/hex.rb, line 12
def hex2dec(opts = {})
  opts[:prefix] ||= ''
  # remove prefix
  out = sub(opts[:prefix], '')
  # convert
  return out.hex.to_s
end
hex2dec!(opts = {}) click to toggle source

Encode an hexadecimal string to a decimal string in place as described for {String#hex2dec}. @example

a = 'ff'
a.hex2dec!
a # => "255"
# File lib/ctf_party/hex.rb, line 26
def hex2dec!(opts = {})
  replace(hex2dec(opts))
end
hex2str(opts = {}) click to toggle source

Alias for {String#from_hex}.

# File lib/ctf_party/hex.rb, line 142
def hex2str(opts = {})
  from_hex(opts)
end
hex2str!(opts = {}) click to toggle source

Alias for {String#from_hex!}.

# File lib/ctf_party/hex.rb, line 157
def hex2str!(opts = {})
  from_hex!(opts)
end
htmlescape() click to toggle source

HTML escape the string @return [String] the HTML escaped string @example

'Usage: foo "bar" <baz>'.htmlescape # => "Usage: foo &quot;bar&quot; &lt;baz&gt;"
# File lib/ctf_party/cgi.rb, line 37
def htmlescape
  CGI.escapeHTML self
end
htmlescape!() click to toggle source

HTML escape the string in place as described for {String#htmlescape}.

# File lib/ctf_party/cgi.rb, line 42
def htmlescape!
  replace(htmlescape)
end
htmlunescape() click to toggle source

HTML unescape the string @return [String] the HTML unescaped string @example

"Usage: foo &quot;bar&quot; &lt;baz&gt;".htmlunescape # => "Usage: foo \"bar\" <baz>"
# File lib/ctf_party/cgi.rb, line 50
def htmlunescape
  CGI.unescapeHTML self
end
htmlunescape!() click to toggle source

HTML unescape the string in place as described for {String#htmlunescape}.

# File lib/ctf_party/cgi.rb, line 55
def htmlunescape!
  replace(htmlunescape)
end
istrip() click to toggle source

Remove leading and trailing whitespace (like {String#strip}) but also all inner whitespace. @return [String] the whitespace-free string @example

"\t\n\v\f\r Hello \t\n\v\f\r World !\t\n\v\f\r ".istrip # => "HelloWorld!"
'73 74 72 69 70'.istrip # => "7374726970"
# File lib/ctf_party/misc.rb, line 10
def istrip
  strip.gsub(/\s/, '')
end
istrip!() click to toggle source

Remove all whitespace in place as described for {String#istrip}.

# File lib/ctf_party/misc.rb, line 15
def istrip!
  replace(innerstrip)
end
leet() click to toggle source

Transform into leet speak (l337 5p34k) @example

'The quick brown fox jumps over the lazy dog'.leet # => "7h3 qu1ck 8r0wn f0x jump5 0v3r 7h3 14zy d06"
'leet speak'.leet # => "1337 5p34k"
# File lib/ctf_party/leet.rb, line 8
def leet
  tr = {
    'T' => '7',
    'E' => '3',
    'I' => '1',
    'L' => '1',
    'O' => '0',
    'S' => '5',
    'A' => '4',
    'G' => '6',
    'B' => '8'
  }
  tr.merge! tr.transform_keys(&:downcase)
  gsub(/[#{tr.keys.join}]/i, **tr)
end
leet!() click to toggle source

Transform into leet speak (l337 5p34k) in place as described for {String#leet}.

# File lib/ctf_party/leet.rb, line 26
def leet!
  replace(leet)
end
md5() click to toggle source

Calculate the md5 hash of the string. @see ruby-doc.org/stdlib-2.6.1/libdoc/digest/rdoc/Digest/MD5.html @return [String] md5 hash @example

'noraj'.md5 # => "556cc23863fef20fab5c456db166bc6e"
# File lib/ctf_party/digest.rb, line 12
def md5
  Digest::MD5.hexdigest self
end
md5!() click to toggle source

Calculate the md5 hash of the string in place as described for {String#md5}. @example

a = '\o/' # => "\\o/"
a.md5! # => "881419964e480e66162da521ccc25ebf"
a # => "881419964e480e66162da521ccc25ebf"
# File lib/ctf_party/digest.rb, line 21
def md5!
  replace(md5)
end
randomcase() click to toggle source

Change the case of characters randomly @return [String] the case modified string @example

'SELECT * FROM'.randomcase # => "SElECt * frOm"
'SELECT * FROM'.randomcase # => "selECT * FROm"
# File lib/ctf_party/case.rb, line 9
def randomcase
  chars.map { |c| rand(0..1).zero? ? c.downcase : c.upcase }.join
end
randomcase!() click to toggle source

Change the case of characters randomly in place as described for {String#randomcase}.

# File lib/ctf_party/case.rb, line 15
def randomcase!
  replace(randomcase)
end
rmd160() click to toggle source

Calculate the RIPEMD-160 hash of the string. @see ruby-doc.org/stdlib-2.6.1/libdoc/digest/rdoc/Digest/RMD160.html @return [String] RIPEMD-160 hash @example

'payload'.rmd160 # => "3c6255c112d409dafdb84d5b0edba98dfd27b44f"
# File lib/ctf_party/digest.rb, line 104
def rmd160
  Digest::RMD160.hexdigest self
end
rmd160!() click to toggle source

Calculate the RIPEMD-160 hash of the string in place as described for {String#rmd160}. @example

pl = 'payload' # => "payload"
pl.rmd160! # => "3c6255c112d409dafdb84d5b0edba98dfd27b44f"
pl # => "3c6255c112d409dafdb84d5b0edba98dfd27b44f"
# File lib/ctf_party/digest.rb, line 114
def rmd160!
  replace(rmd160)
end
rot(opts = {}) click to toggle source

“Encrypt / Decrypt” the string with Caesar cipher. This will shift the alphabet letters by n, where n is the integer key. The same function is used for encryption / decryption. @param opts [Hash] optional parameters @option opts [Integer] :shift The shift key. Default value: 13. @see en.wikipedia.org/wiki/Caesar_cipher @return [String] the (de)ciphered string @example

'Hello world!'.rot # => "Uryyb jbeyq!"
'Hello world!'.rot(shift: 11) # => "Spwwz hzcwo!"
'Uryyb jbeyq!'.rot # => "Hello world!"
'Spwwz hzcwo!'.rot(shift: 26-11) # => "Hello world!"
# File lib/ctf_party/rot.rb, line 16
def rot(opts = {})
  opts[:shift] ||= 13
  alphabet = Array('a'..'z')
  lowercase = alphabet.zip(alphabet.rotate(opts[:shift])).to_h
  alphabet = Array('A'..'Z')
  uppercasecase = alphabet.zip(alphabet.rotate(opts[:shift])).to_h
  encrypter = lowercase.merge(uppercasecase)
  chars.map { |c| encrypter.fetch(c, c) }.join
end
rot!(opts = {}) click to toggle source

“Encrypt / Decrypt” the string with Caesar cipher in place as described for {String#rot}. @return [String] the (de)ciphered string as well as changing changing the

object in place.

@example

a = 'Bonjour le monde !' # => "Bonjour le monde !"
a.rot! # => "Obawbhe yr zbaqr !"
a # => "Obawbhe yr zbaqr !"
# File lib/ctf_party/rot.rb, line 34
def rot!(opts = {})
  replace(rot(opts))
end
rot13() click to toggle source

Alias for {String#rot} with default value ( +rot(shift: 13)+ ).

# File lib/ctf_party/rot.rb, line 39
def rot13
  rot
end
rot13!() click to toggle source

Alias for {String#rot!} with default value ( +rot!(shift: 13)+ ).

# File lib/ctf_party/rot.rb, line 44
def rot13!
  rot!
end
rot_all() click to toggle source

Compute all possibilities with {String#rot} @return [Hash] All possibilities with the shift index as key and the

(de)ciphered text as value

@example

'noraj'.rot_all # => {1=>"opsbk", 2=>"pqtcl", 3=>"qrudm", ... }
# File lib/ctf_party/rot.rb, line 53
def rot_all
  (1..26).each_with_object({}) { |i, h| h[i] = rot(shift: i) }
end
sha1() click to toggle source

Calculate the sha1 hash of the string. @see ruby-doc.org/stdlib-2.6.1/libdoc/digest/rdoc/Digest/SHA1.html @return [String] sha1 hash @example

'ctf-party'.sha1 # => "5a64f3bc491d0977e1e3578a48c65a89a16a5fe8"
# File lib/ctf_party/digest.rb, line 30
def sha1
  Digest::SHA1.hexdigest self
end
sha1!() click to toggle source

Calculate the sha1 hash of the string in place as described for

{String#sha1}.

@example

bob = 'alice' # => "alice"
bob.sha1! # => "522b276a356bdf39013dfabea2cd43e141ecc9e8"
bob # => "522b276a356bdf39013dfabea2cd43e141ecc9e8"
# File lib/ctf_party/digest.rb, line 40
def sha1!
  replace(sha1)
end
sha2(opts = {}) click to toggle source

Calculate the sha2 hash of the string. @param opts [Hash] optional parameters @option opts [Integer] :bitlen Defines the bit lenght of the digest.

Default value: 256 (SHA2-256). other valid vales are 384 (SHA2-384)
and 512 (SHA2-512).

@see ruby-doc.org/stdlib-2.6.1/libdoc/digest/rdoc/Digest/SHA2.html @return [String] sha hash @example

'try harder'.sha2 # => "5321ff2d4b1389b3a350dfe8ca77e3889dc6259bb233ad..."
'try harder'.sha2(bitlen: 512) # => "a7b73a98c095b22e25407b15c4dec128c..."
# File lib/ctf_party/digest.rb, line 54
def sha2(opts = {})
  opts[:bitlen] ||= 256
  Digest::SHA2.new(opts[:bitlen]).hexdigest self
end
sha2!(opts = {}) click to toggle source

Calculate the sha2 hash of the string in place as described for

{String#sha2}.

@example

th = 'try harder' # => "try harder"
th.sha2!(bitlen: 384) # => "bb7f60b9562a19c3a83c23791440af11591c42ede9..."
th # => "bb7f60b9562a19c3a83c23791440af11591c42ede9988334cdfd7efa4261a..."
# File lib/ctf_party/digest.rb, line 65
def sha2!(opts = {})
  replace(sha2(opts))
end
sha2_256() click to toggle source

Alias for {String#sha2} with default value ( +sha2(bitlen: 256)+ ).

# File lib/ctf_party/digest.rb, line 70
def sha2_256
  sha2
end
sha2_256!() click to toggle source

Alias for {String#sha2!} with default value ( +sha2!(bitlen: 256)+ ).

# File lib/ctf_party/digest.rb, line 75
def sha2_256!
  replace(sha2)
end
sha2_384() click to toggle source

Alias for {String#sha2} with default value ( +sha2(bitlen: 384)+ ).

# File lib/ctf_party/digest.rb, line 80
def sha2_384
  sha2(bitlen: 384)
end
sha2_384!() click to toggle source

Alias for {String#sha2!} with default value ( +sha2!(bitlen: 384)+ ).

# File lib/ctf_party/digest.rb, line 85
def sha2_384!
  replace(sha2(bitlen: 384))
end
sha2_512() click to toggle source

Alias for {String#sha2} with default value ( +sha2(bitlen: 512)+ ).

# File lib/ctf_party/digest.rb, line 90
def sha2_512
  sha2(bitlen: 512)
end
sha2_512!() click to toggle source

Alias for {String#sha2!} with default value ( +sha2!(bitlen: 512)+ ).

# File lib/ctf_party/digest.rb, line 95
def sha2_512!
  replace(sha2(bitlen: 512))
end
str2bin(opts = {}) click to toggle source

Alias for {String#to_bin}.

# File lib/ctf_party/binary.rb, line 22
def str2bin(opts = {})
  to_bin(opts)
end
str2bin!(opts = {}) click to toggle source

Alias for {String#to_bin!}.

# File lib/ctf_party/binary.rb, line 37
def str2bin!(opts = {})
  to_bin!(opts)
end
str2dec() click to toggle source

Alias for {String#to_dec}.

# File lib/ctf_party/dec.rb, line 31
def str2dec
  to_dec
end
str2dec!() click to toggle source

Alias for {String#to_dec!}.

# File lib/ctf_party/dec.rb, line 36
def str2dec!
  replace(str2dec)
end
str2hex(opts = {}) click to toggle source

Alias for {String#to_hex}.

# File lib/ctf_party/hex.rb, line 99
def str2hex(opts = {})
  to_hex(opts)
end
str2hex!(opts = {}) click to toggle source

Alias for {String#to_hex!}.

# File lib/ctf_party/hex.rb, line 114
def str2hex!(opts = {})
  to_hex!(opts)
end
to_b64(opts = {}) click to toggle source

Encode the string into base64 @param opts [Hash] optional parameters @option opts [Symbol] :mode Default value: :strict.

Other values are +:strict+ (+:rfc4648+) or +:urlsafe+.

@see ruby-doc.org/stdlib-2.6.5/libdoc/base64/rdoc/Base64.html @return [String] the Base64 encoded string @example

'Super lib!'.to_b64 # => "U3VwZXIgbGliIQ=="
# File lib/ctf_party/base64.rb, line 15
def to_b64(opts = {})
  opts[:mode] ||= :strict
  return Base64.strict_encode64(self) if opts[:mode] == :strict ||
                                         opts[:mode] == :rfc4648
  return Base64.encode64(self) if opts[:mode] == :rfc2045
  return Base64.urlsafe_encode64(self) if opts[:mode] == :urlsafe
end
to_b64!(opts = {}) click to toggle source

Encode the string into base64 in place as described for {String#to_b64}. @return [nil] @example

myStr = 'Ruby' # => "Ruby"
myStr.to_b64! # => nil
myStr # => "UnVieQ=="
# File lib/ctf_party/base64.rb, line 29
def to_b64!(opts = {})
  replace(to_b64(opts))
end
to_bin(opts = {}) click to toggle source

Encode a string into binary @param opts [Hash] optional parameters @option opts [Symbol] :bitnumbering Display output with most significant bit

first (+:MSB+ default) or least significant bit first (+:LSB+).

@return [String] the binary encoded string @example

'binary'.to_bin # => "011000100110100101101110011000010111001001111001"
'binary'.to_bin(bitnumbering: :LSB) # => "010001101001011001110110100001100100111010011110"
# File lib/ctf_party/binary.rb, line 12
def to_bin(opts = {})
  opts[:bitnumbering] ||= :MSB
  # convert
  return unpack1('B*') if opts[:bitnumbering] == :MSB
  return unpack1('b*') if opts[:bitnumbering] == :LSB

  raise ArgumentError ':bitnumbering expects :MSB or :LSB'
end
to_bin!(opts = {}) click to toggle source

Encode a string into binary in place as described for {String#to_bin}. @example

a = 'binary'
a.to_bin!
a # => "011000100110100101101110011000010111001001111001"
# File lib/ctf_party/binary.rb, line 32
def to_bin!(opts = {})
  replace(to_bin(opts))
end
to_dec() click to toggle source

Encode a string into decimal (string to hexadecimal then hexadecimal to decimal) @return [String] the decimal encoded string @example

'noraj'.to_dec # => "474316169578"
# File lib/ctf_party/dec.rb, line 8
def to_dec
  str2hex.hex2dec
end
to_dec!() click to toggle source

Encode a string into decimal in place as described for {String#to_dec}.

# File lib/ctf_party/dec.rb, line 13
def to_dec!
  replace(to_dec)
end
to_hex(opts = {}) click to toggle source

Encode a string into hexadecimal @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 [Symbol] :case Char case of the ouput. Default value :lower.

Other valid value +:upper+.

@option opts [Symbol] :nibble Display output with high nibble first

(+:high+ default) or low nibble first (+:low+).

@return [String] the hexadecimal encoded string @example

'noraj'.to_hex # => "6e6f72616a"
'noraj'.to_hex(prefix: '0x') # => "0x6e6f72616a"
'noraj'.to_hex(case: :upper) # => "6E6F72616A"
'noraj'.to_hex(nibble: :low) # => "e6f62716a6"
# File lib/ctf_party/hex.rb, line 80
def to_hex(opts = {})
  opts[:prefix] ||= ''
  opts[:case] ||= :lower
  opts[:nibble] ||= :high
  # convert
  out = ''
  case opts[:nibble]
  when :high
    out = unpack1('H*')
  when :low
    out = unpack1('h*')
  end
  # char case management
  out = out.upcase if opts[:case] == :upper
  # adding prefix must be done after case change
  return opts[:prefix] + out
end
to_hex!(opts = {}) click to toggle source

Encode a string into hexadecimal in place as described for {String#to_hex}. @example

a = 'noraj'
a.to_hex!
a # => "6e6f72616a"
# File lib/ctf_party/hex.rb, line 109
def to_hex!(opts = {})
  replace(to_hex(opts))
end
to_hexip(opts = {}) click to toggle source

Encode a dotted decimal IP 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 [Symbol] :case Char case of the ouput. 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"
# File lib/ctf_party/hex.rb, line 257
def to_hexip(opts = {})
  opts[:prefix] ||= ''
  opts[:case] ||= :lower
  opts[:nibble] ||= :high
  # convert
  out = split('.').map { |x| x.dec2hex(padding: 2) }
  out = out.reverse if opts[:nibble] == :low
  out = out.join
  # char case management
  out = out.upcase if opts[:case] == :upper
  # adding prefix must be done after case change
  return opts[:prefix] + out
end
to_hexip!(opts = {}) click to toggle source

Encode a dotted decimal IP into a hexadecimal one in place as described for {String#to_hexip}.

# File lib/ctf_party/hex.rb, line 273
def to_hexip!(opts = {})
  replace(to_hexip(opts))
end
ulxor(key) click to toggle source

UTF-8 XOR with key, padding left the shortest element @param key [String] the element to xor the string with @return [String] the xored string (UTF-8) @example

'hello'.ulxor('key') # => "he\a\t\u0016"
'key'.ulxor('hello') # => "he\a\t\u0016"
# File lib/ctf_party/xor.rb, line 10
def ulxor(key)
  b1 = unpack('U*')
  b2 = key.unpack('U*')
  longest = [b1.length, b2.length].max
  b1 = [0] * (longest - b1.length) + b1
  b2 = [0] * (longest - b2.length) + b2
  b1.zip(b2).map { |a, b| a ^ b }.pack('U*')
end
ulxor!(key) click to toggle source

UTF-8 XOR with key (padding left) in place as described for {String#ulxor}.

# File lib/ctf_party/xor.rb, line 20
def ulxor!(key)
  replace(ulxor(key))
end
urldecode() click to toggle source

URL-decode the string @return [String] the URL-decoded string @example

"%27Stop%21%27+said+Fred".urldecode # => "'Stop!' said Fred"
# File lib/ctf_party/cgi.rb, line 24
def urldecode
  CGI.unescape self
end
urldecode!() click to toggle source

URL-decode the string in place as described for {String#urldecode}.

# File lib/ctf_party/cgi.rb, line 29
def urldecode!
  replace(urldecode)
end
urlencode() click to toggle source

URL-encode the string @return [String] the URL-encoded string @example

"'Stop!' said Fred".urlencode # => "%27Stop%21%27+said+Fred"
# File lib/ctf_party/cgi.rb, line 11
def urlencode
  CGI.escape self
end
urlencode!() click to toggle source

URL-encode the string in place as described for {String#urlencode}.

# File lib/ctf_party/cgi.rb, line 16
def urlencode!
  replace(urlencode)
end
urxor(key) click to toggle source

UTF-8 XOR with key, padding right the shortest element @param key [String] the element to xor the string with @return [String] the xored string (UTF-8) @example

'hello'.urxor('key') # => "\u0003\u0000\u0015lo"
'key'.urxor('hello') # => "\u0003\u0000\u0015lo"
# File lib/ctf_party/xor.rb, line 55
def urxor(key)
  b1 = unpack('U*')
  b2 = key.unpack('U*')
  longest = [b1.length, b2.length].max
  b1 += [0] * (longest - b1.length)
  b2 += [0] * (longest - b2.length)
  b1.zip(b2).map { |a, b| a ^ b }.pack('U*')
end
urxor!(key) click to toggle source

UTF-8 XOR with key (padding right) in place as described for {String#urxor}.

# File lib/ctf_party/xor.rb, line 65
def urxor!(key)
  replace(urxor(key))
end