Class: Base32::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/base32-alphabets/base.rb

Direct Known Subclasses

Crockford, Electrologica, Kai

Class Method Summary collapse

Class Method Details

._build_binaryObject

(private) helpers



90
91
92
93
94
95
96
# File 'lib/base32-alphabets/base.rb', line 90

def self._build_binary
  ## e.g. '00000', '00001', '00010', '00011', etc.
  number.reduce({}) do |h, (char,index)|
    h[char]        = '%05b' % index
    h
  end
end

._build_codeObject



98
99
100
101
102
103
104
# File 'lib/base32-alphabets/base.rb', line 98

def self._build_code
  ## e.g. '00', '01', '02', '03', '04', etc.
  number.reduce({}) do |h, (char,index)|
    h[char]        = '%02d' % index
    h
  end
end

._clean(str) ⇒ Object



81
82
83
84
# File 'lib/base32-alphabets/base.rb', line 81

def self._clean( str )
  ## note: remove space ( ), dash (-), slash (/) for now as "allowed / supported" separators
  str.tr( ' -/', '' )
end

._decode(str) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/base32-alphabets/base.rb', line 71

def self._decode( str )
  str = _clean( str )
  str.each_char.reduce([]) do |bytes,char|
    byte = number[char]
    raise ArgumentError, "Value passed not a valid base32 string - >#{char}< not found in alphabet"  if byte.nil?
    bytes << byte
    bytes
  end
end

._encode(bytes) ⇒ Object



32
33
34
35
36
37
# File 'lib/base32-alphabets/base.rb', line 32

def self._encode( bytes )
  bytes.reduce( String.new ) do |buf, byte|
    buf << alphabet[byte]
    buf
  end
end

._fmt(str, group: 4, sep: ' ') ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/base32-alphabets/base.rb', line 49

def self._fmt( str, group: 4, sep: ' ' )
  str = _clean( str )

  ## format in groups of four (4) separated by space
  ##  e.g.  ccac7787fa7fafaa16467755f9ee444467667366cccceede
  ##     :  ccac 7787 fa7f afaa 1646 7755 f9ee 4444 6766 7366 cccc eede
  str.reverse.gsub( /(.{#{group}})/, "\\1#{sep}" ).reverse.sub( /^#{sep}/, '' )
end

.bytes(num_or_str) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/base32-alphabets/base.rb', line 10

def self.bytes( num_or_str )
  if num_or_str.is_a? String
    str   = num_or_str
    bytes = _decode( str )
  else  # assume number
    num   = num_or_str
    bytes = Base32._bytes( num )
  end
end

.decode(str_or_bytes) ⇒ Object

Converts a base32 string to a base10 integer.



60
61
62
63
64
65
66
67
68
# File 'lib/base32-alphabets/base.rb', line 60

def self.decode( str_or_bytes )
  if str_or_bytes.is_a? Array
    bytes = str_or_bytes
  else  ## assume string
    str   = str_or_bytes
    bytes = _decode( str )
  end
  Base32._pack( bytes )
end

.encode(num_or_bytes) ⇒ Object

Converts a base10 integer to a base32 string.



22
23
24
25
26
27
28
29
30
# File 'lib/base32-alphabets/base.rb', line 22

def self.encode( num_or_bytes )
  if num_or_bytes.is_a? Array
    bytes = num_or_bytes
  else
    num = num_or_bytes
    bytes = Base32._bytes( num )
  end
  _encode( bytes )
end

.fmt(str_or_num_or_bytes, group: 4, sep: ' ') ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/base32-alphabets/base.rb', line 39

def self.fmt( str_or_num_or_bytes, group: 4, sep: ' ' )
  if str_or_num_or_bytes.is_a? String
    str = str_or_num_or_bytes
  else  ## assume number (Integer) or bytes (Array)
    num_or_bytes = str_or_num_or_bytes
    str = encode( num_or_bytes )   ## auto-encode (shortcut)
  end
  _fmt( str, group: group, sep: sep )
end