Module: Base45

Defined in:
lib/base45.rb,
lib/base45/version.rb,
lib/base45/encoding_table.rb

Overview

Exposes two methods to decode and encode in base 45

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.1.0"
BASE_45_TABLE =
{
  "00" => "0",
  "01" => "1",
  "02" => "2",
  "03" => "3",
  "04" => "4",
  "05" => "5",
  "06" => "6",
  "07" => "7",
  "08" => "8",
  "09" => "9",
  "10" => "A",
  "11" => "B",
  "12" => "C",
  "13" => "D",
  "14" => "E",
  "15" => "F",
  "16" => "G",
  "17" => "H",
  "18" => "I",
  "19" => "J",
  "20" => "K",
  "21" => "L",
  "22" => "M",
  "23" => "N",
  "24" => "O",
  "25" => "P",
  "26" => "Q",
  "27" => "R",
  "28" => "S",
  "29" => "T",
  "30" => "U",
  "31" => "V",
  "32" => "W",
  "33" => "X",
  "34" => "Y",
  "35" => "Z",
  "36" => " ",
  "37" => "$",
  "38" => "%",
  "39" => "*",
  "40" => "+",
  "41" => "-",
  "42" => ".",
  "43" => "/",
  "44" => ":"
}.freeze
INVERTED_BASE_45_TABLE =
BASE_45_TABLE.invert.freeze

Class Method Summary collapse

Class Method Details

.decode(payload) ⇒ Object

Returns the Base45-decoded version of payload

require 'base45'
Base45.encode(":Y8UPCAVC3/DH44M-DUJCLQE934AW6X0")

Generates:

Encoding in base 45 !


42
43
44
45
46
47
48
49
# File 'lib/base45.rb', line 42

def decode(payload)
  return if payload.length < 2

  lookup = sliced_payload(payload)
  base45_vals = lookup.map { |c, d, e| c + d * 45 + (e ? e * 45**2 : 0) }

  base45_vals.pack("S>*").gsub(/\x00/, "")
end

.encode(payload) ⇒ Object

Returns the Base45-encoded version of payload

require 'base45'
Base45.encode("Encoding in base 45 !")

Generates:

:Y8UPCAVC3/DH44M-DUJCLQE934AW6X0


26
27
28
29
30
31
32
# File 'lib/base45.rb', line 26

def encode(payload)
  return if payload.length.zero?

  return encode_for_single_char(payload) if payload.bytesize < 2

  encode_for_multipe_chars(payload)
end