Module: Base32

Defined in:
lib/base32.rb,
lib/base32/version.rb

Overview

Module for encoding and decoding in Base32 per RFC 3548

Defined Under Namespace

Classes: Chunk

Constant Summary collapse

TABLE =
'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'.freeze
VERSION =
'0.3.4'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.tableObject

Returns the value of attribute table.



9
10
11
# File 'lib/base32.rb', line 9

def table
  @table
end

Class Method Details

.chunks(str, size) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/base32.rb', line 38

def self.chunks(str, size)
  result = []
  bytes = str.bytes
  while bytes.any? do
    result << Chunk.new(bytes.take(size))
    bytes = bytes.drop(size)
  end
  result
end

.decode(str) ⇒ Object



52
53
54
# File 'lib/base32.rb', line 52

def self.decode(str)
  chunks(str, 8).collect(&:decode).flatten.join
end

.encode(str) ⇒ Object



48
49
50
# File 'lib/base32.rb', line 48

def self.encode(str)
  chunks(str, 5).collect(&:encode).flatten.join
end

.random_base32(length = 16, padding = true) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/base32.rb', line 56

def self.random_base32(length=16, padding=true)
  random = ''
  OpenSSL::Random.random_bytes(length).each_byte do |b|
    random << self.table[b % 32]
  end
  padding ? random.ljust((length / 8.0).ceil * 8, '=') : random
end

.table_valid?(table) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/base32.rb', line 69

def self.table_valid?(table)
  table.bytes.to_a.size == 32 && table.bytes.to_a.uniq.size == 32
end