module Base45
Exposes two methods to decode and encode in base 45
Constants
- BASE_45_TABLE
- INVERTED_BASE_45_TABLE
- VERSION
Public Class Methods
decode(payload)
click to toggle source
Returns the Base45-decoded version of payload
require 'base45' Base45.encode(":Y8UPCAVC3/DH44M-DUJCLQE934AW6X0")
Generates:
Encoding in base 45 !
# File lib/base45.rb, line 44 def decode(payload) map45_chars(payload).each_slice(3).flat_map do |c, d, e| c && d or raise ForbiddenLengthError v = c + d * 45 bytes_from_base45(e, v) end.pack("C*") end
encode(payload)
click to toggle source
Returns the Base45-encoded version of payload
require 'base45' Base45.encode("Encoding in base 45 !")
Generates:
:Y8UPCAVC3/DH44M-DUJCLQE934AW6X0
# File lib/base45.rb, line 25 def encode(payload) sliced_bytes = payload.each_byte.each_slice(2) base45_bytes = sliced_bytes.flat_map do |byte_a, byte_b| byte_b ? encode_two_bytes(byte_a, byte_b) : encode_one_byte(byte_a) end base45_bytes.map do |byte45| BASE_45_TABLE[byte45.to_s.rjust(2, "0")] end.join end
Private Class Methods
bytes_from_base45(last_triplet_byte, factor45)
click to toggle source
# File lib/base45.rb, line 54 def bytes_from_base45(last_triplet_byte, factor45) return [factor45] unless last_triplet_byte factor45 += last_triplet_byte * (45**2) x, y = factor45.divmod(256) raise OverflowError unless x < 256 [x, y] end
encode_one_byte(byte)
click to toggle source
# File lib/base45.rb, line 64 def encode_one_byte(byte) byte.divmod(45).reverse end
encode_two_bytes(first_byte, second_byte)
click to toggle source
# File lib/base45.rb, line 68 def encode_two_bytes(first_byte, second_byte) x = (first_byte << 8) + second_byte e, x = x.divmod(45**2) d, c = x.divmod(45) [c, d, e] end
map45_chars(string)
click to toggle source
# File lib/base45.rb, line 76 def map45_chars(string) string.upcase.each_char.map do |c| char_byte = INVERTED_BASE_45_TABLE[c] raise(IllegalCharacterError, c.inspect) unless char_byte char_byte.to_i end end