module Base32
shared code for formats / variants with single char alphabets
e.g. Kai, Crockford, ...
Base32
Encoding - Crockford
(by Douglas Crockford
)
see https://www.crockford.com/wrmg/base32.html
why kai?
in honor of Kaigani who deciphered the genes - thanks! see https://medium.com/@kaigani/the-cryptokitties-genome-project-on-dominance-inheritance-and-mutation-b73059dcd0a4
Constants
- BASE
- MAJOR
- MINOR
- PATCH
- VERSION
Public Class Methods
_bytes( num )
click to toggle source
(private) helper - note: leading underscore in name e.g. _bytes
# 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 )
click to toggle source
# 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 )
click to toggle source
encoding alphabet - letter-to-number by index / array
# File lib/base32-alphabets/base32.rb, line 94 def self.alphabet( klass: configuration.format ) klass.alphabet; end
binary( klass: configuration.format )
click to toggle source
decoding letter-to-binary mapping / hash
# File lib/base32-alphabets/base32.rb, line 101 def self.binary( klass: configuration.format ) klass.binary; end
bytes( num_or_str, klass: configuration.format )
click to toggle source
# 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 )
click to toggle source
decoding letter-to-code mapping / hash
# File lib/base32-alphabets/base32.rb, line 99 def self.code( klass: configuration.format ) klass.code; end
configuration()
click to toggle source
lets you use
Base32.configure do |config| config.format = :kai end
# File lib/base32-alphabets/base32.rb, line 38 def self.configuration @configuration ||= Configuration.new end
configure() { |configuration| ... }
click to toggle source
# File lib/base32-alphabets/base32.rb, line 42 def self.configure yield( configuration ) end
decode( str_or_bytes, klass: configuration.format )
click to toggle source
# 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 )
click to toggle source
# 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: ' ' )
click to toggle source
# 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()
click to toggle source
add convenience helper for format
# File lib/base32-alphabets/base32.rb, line 47 def self.format() configuration.format; end
format=(value)
click to toggle source
# File lib/base32-alphabets/base32.rb, line 48 def self.format=(value) self.configuration.format = value; end
number( klass: configuration.format )
click to toggle source
decoding letter-to-number mapping / hash
# File lib/base32-alphabets/base32.rb, line 97 def self.number( klass: configuration.format ) klass.number; end
root()
click to toggle source
# File lib/base32-alphabets/version.rb, line 17 def self.root File.expand_path( File.dirname(File.dirname(File.dirname(__FILE__))) ) end
version()
click to toggle source
# File lib/base32-alphabets/version.rb, line 9 def self.version VERSION end