Module: Base32
- Defined in:
- lib/base32-alphabets/base.rb,
lib/base32-alphabets/kai.rb,
lib/base32-alphabets/base32.rb,
lib/base32-alphabets/version.rb,
lib/base32-alphabets/crockford.rb,
lib/base32-alphabets/electrologica.rb
Overview
Base32 (2^5 - 5-bits)
Defined Under Namespace
Classes: Base, Configuration, Crockford, Electrologica, Kai
Constant Summary collapse
- BASE =
ALPHABET.length == 32 ## 32 chars/letters/digits
32- MAJOR =
1- MINOR =
1- PATCH =
0- VERSION =
[MAJOR,MINOR,PATCH].join('.')
Class Method Summary collapse
-
._bytes(num) ⇒ Object
(private) helper - note: leading underscore in name e.g.
- ._pack(bytes) ⇒ Object
-
.alphabet(klass: configuration.format) ⇒ Object
encoding alphabet - letter-to-number by index / array.
- .banner ⇒ Object
-
.binary(klass: configuration.format) ⇒ Object
decoding letter-to-binary mapping / hash.
- .bytes(num_or_str, klass: configuration.format) ⇒ Object
-
.code(klass: configuration.format) ⇒ Object
decoding letter-to-code mapping / hash.
-
.configuration ⇒ Object
lets you use Base32.configure do |config| config.format = :kai end.
- .configure {|configuration| ... } ⇒ Object
- .decode(str_or_bytes, klass: configuration.format) ⇒ Object
- .encode(num_or_bytes, klass: configuration.format) ⇒ Object
- .fmt(str_or_num_or_bytes, klass: configuration.format, group: 4, sep: ' ') ⇒ Object
-
.format ⇒ Object
add convenience helper for format.
- .format=(value) ⇒ Object
-
.number(klass: configuration.format) ⇒ Object
decoding letter-to-number mapping / hash.
- .root ⇒ Object
- .version ⇒ Object
Class Method Details
._bytes(num) ⇒ Object
(private) helper - note: leading underscore in name e.g. _bytes
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/base32-alphabets/base32.rb', line 71 def self._bytes( num ) b = [] while num >= BASE mod = num % BASE b << mod num = (num - mod) / BASE end b << num b = b.reverse b end |
._pack(bytes) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/base32-alphabets/base32.rb', line 83 def self._pack( bytes ) num = 0 bytes.reverse.each_with_index do |byte,index| num += byte * (BASE**(index)) end num end |
.alphabet(klass: configuration.format) ⇒ Object
encoding alphabet - letter-to-number by index / array
94 |
# File 'lib/base32-alphabets/base32.rb', line 94 def self.alphabet( klass: configuration.format ) klass.alphabet; end |
.banner ⇒ Object
15 16 17 |
# File 'lib/base32-alphabets/version.rb', line 15 def self. "base32-alphabets/#{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]" end |
.binary(klass: configuration.format) ⇒ Object
decoding letter-to-binary mapping / hash
101 |
# File 'lib/base32-alphabets/base32.rb', line 101 def self.binary( klass: configuration.format ) klass.binary; end |
.bytes(num_or_str, klass: configuration.format) ⇒ Object
64 65 66 |
# File 'lib/base32-alphabets/base32.rb', line 64 def self.bytes( num_or_str, klass: configuration.format ) klass.bytes( num_or_str ) end |
.code(klass: configuration.format) ⇒ Object
decoding letter-to-code mapping / hash
99 |
# File 'lib/base32-alphabets/base32.rb', line 99 def self.code( klass: configuration.format ) klass.code; end |
.configuration ⇒ Object
lets you use
Base32.configure do |config|
config.format = :kai
end
38 39 40 |
# File 'lib/base32-alphabets/base32.rb', line 38 def self.configuration @configuration ||= Configuration.new end |
.configure {|configuration| ... } ⇒ Object
42 43 44 |
# File 'lib/base32-alphabets/base32.rb', line 42 def self.configure yield( configuration ) end |
.decode(str_or_bytes, klass: configuration.format) ⇒ Object
56 57 58 |
# File 'lib/base32-alphabets/base32.rb', line 56 def self.decode( str_or_bytes, klass: configuration.format ) klass.decode( str_or_bytes ) end |
.encode(num_or_bytes, klass: configuration.format) ⇒ Object
52 53 54 |
# File 'lib/base32-alphabets/base32.rb', line 52 def self.encode( num_or_bytes, klass: configuration.format ) klass.encode( num_or_bytes ) end |
.fmt(str_or_num_or_bytes, klass: configuration.format, group: 4, sep: ' ') ⇒ Object
60 61 62 |
# File 'lib/base32-alphabets/base32.rb', line 60 def self.fmt( str_or_num_or_bytes, klass: configuration.format, group: 4, sep: ' ' ) klass.fmt( str_or_num_or_bytes, group: group, sep: sep ) end |
.format ⇒ Object
add convenience helper for format
47 |
# File 'lib/base32-alphabets/base32.rb', line 47 def self.format() configuration.format; end |
.format=(value) ⇒ Object
48 |
# File 'lib/base32-alphabets/base32.rb', line 48 def self.format=(value) self.configuration.format = value; end |
.number(klass: configuration.format) ⇒ Object
decoding letter-to-number mapping / hash
97 |
# File 'lib/base32-alphabets/base32.rb', line 97 def self.number( klass: configuration.format ) klass.number; end |
.root ⇒ Object
19 20 21 |
# File 'lib/base32-alphabets/version.rb', line 19 def self.root "#{File.expand_path( File.dirname(File.dirname(File.dirname(__FILE__))) )}" end |
.version ⇒ Object
11 12 13 |
# File 'lib/base32-alphabets/version.rb', line 11 def self.version VERSION end |