Class: Multibases::Base32

Inherits:
Object
  • Object
show all
Defined in:
lib/multibases/base32.rb

Overview

RFC 3548

Defined Under Namespace

Classes: Chunk, Table

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(alphabet, strict: false, encoding: nil) ⇒ Base32

Returns a new instance of Base32.



82
83
84
# File 'lib/multibases/base32.rb', line 82

def initialize(alphabet, strict: false, encoding: nil)
  @table = Table.from(alphabet, strict: strict, encoding: encoding)
end

Class Method Details

.decode(plain) ⇒ Object



20
21
22
# File 'lib/multibases/base32.rb', line 20

def self.decode(plain)
  Default.decode(plain)
end

.encode(plain) ⇒ Object



16
17
18
# File 'lib/multibases/base32.rb', line 16

def self.encode(plain)
  Default.encode(plain)
end

Instance Method Details

#decode(encoded) ⇒ Object



95
96
97
98
99
# File 'lib/multibases/base32.rb', line 95

def decode(encoded)
  return DecodedByteArray::EMPTY if encoded.empty?

  DecodedByteArray.new(chunks(encoded, 8).collect(&:decode).flatten)
end

#encode(plain) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/multibases/base32.rb', line 86

def encode(plain)
  return EncodedByteArray::EMPTY if plain.empty?

  EncodedByteArray.new(
    chunks(plain, 5).collect(&:encode).flatten,
    encoding: @table.encoding
  )
end

#inspectObject



9
10
11
12
13
14
# File 'lib/multibases/base32.rb', line 9

def inspect
  '[Multibases::Base32 ' \
    "alphabet=\"#{@table.chars.join}\"" \
    "#{@table.strict? ? ' strict' : ''}" \
  ']'
end