Class: Base32::Base
- Inherits:
-
Object
show all
- Defined in:
- lib/base32-alphabets/base.rb
Class Method Summary
collapse
Class Method Details
._build_binary ⇒ Object
92
93
94
95
96
97
98
|
# File 'lib/base32-alphabets/base.rb', line 92
def self._build_binary
number.each.reduce({}) do |h, (char,index)|
h[char] = '%05b' % index
h
end
end
|
._build_code ⇒ Object
100
101
102
103
104
105
106
|
# File 'lib/base32-alphabets/base.rb', line 100
def self._build_code
number.each.reduce({}) do |h, (char,index)|
h[char] = '%02d' % index
h
end
end
|
._clean(str) ⇒ Object
83
84
85
86
|
# File 'lib/base32-alphabets/base.rb', line 83
def self._clean( str )
str.tr( ' -/', '' )
end
|
._decode(str) ⇒ Object
73
74
75
76
77
78
79
80
81
|
# File 'lib/base32-alphabets/base.rb', line 73
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
31
32
33
34
35
36
|
# File 'lib/base32-alphabets/base.rb', line 31
def self._encode( bytes )
bytes.reduce( String.new ) do |buf, byte|
buf << alphabet[byte]
buf
end
end
|
._fmt(str, group: 4, sep: ' ') ⇒ Object
51
52
53
54
55
56
57
58
|
# File 'lib/base32-alphabets/base.rb', line 51
def self._fmt( str, group: 4, sep: ' ' )
str = _clean( str )
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
num = decode( str )
else
num = num_or_str
end
Base32._bytes( num )
end
|
.decode(str_or_bytes) ⇒ Object
Converts a base32 string to a base10 integer.
62
63
64
65
66
67
68
69
70
|
# File 'lib/base32-alphabets/base.rb', line 62
def self.decode( str_or_bytes )
if str_or_bytes.is_a? Array
bytes = str_or_bytes
else
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.
21
22
23
24
25
26
27
28
29
|
# File 'lib/base32-alphabets/base.rb', line 21
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
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/base32-alphabets/base.rb', line 38
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
num_or_bytes = str_or_num_or_bytes
str = encode( num_or_bytes )
end
_fmt( str, group: group, sep: sep )
end
|