Module: Ronin::Support::Encoding::Base62

Defined in:
lib/ronin/support/encoding/base62.rb

Overview

Since:

  • 1.1.0

Constant Summary collapse

TABLE =

Base62 table

Since:

  • 1.1.0

'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'

Class Method Summary collapse

Class Method Details

.decode(string) ⇒ Integer

Base62 decodes a String back into an Integer.

Parameters:

  • string (String)

    The string to Base62 decode.

Returns:

  • (Integer)

    The Base62 decoded integer.

Since:

  • 1.1.0



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ronin/support/encoding/base62.rb', line 76

def self.decode(string)
  decoded    = 0
  multiplier = 1
  table_size = TABLE.length

  string.each_char.reverse_each.with_index do |char,index|
    decoded    += TABLE.index(char) * multiplier
    multiplier *= table_size
  end

  return decoded
end

.encode_int(int) ⇒ String

Base62 encodes an integer.

Parameters:

  • int (Integer)

    The integer to Base62 encode.

Returns:

  • (String)

    The Base62 encoded string.

Since:

  • 1.1.0



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ronin/support/encoding/base62.rb', line 49

def self.encode_int(int)
  if int == 0
    String.new('0')
  elsif int < 0
    raise(ArgumentError,"cannot Base62 encode negative numbers: #{int.inspect}")
  else
    encoded    = String.new
    table_size = TABLE.length

    while int > 0
      encoded.prepend(TABLE[int % table_size])
      int /= table_size
    end

    return encoded
  end
end