class Base32::Chunk

Public Class Methods

new(bytes) click to toggle source
# File vendor/base32/lib/base32.rb, line 8
def initialize(bytes)
  @bytes = bytes
end

Public Instance Methods

decode() click to toggle source
# File vendor/base32/lib/base32.rb, line 12
def decode
  bytes = @bytes.take_while {|c| c != 61} # strip padding
  n = (bytes.length * 5.0 / 8.0).floor
  p = bytes.length < 8 ? 5 - (n * 8) % 5 : 0
  c = bytes.inject(0) {|m,o| (m << 5) + Base32.table.index(o.chr)} >> p
  (0..n-1).to_a.reverse.collect {|i| ((c >> i * 8) & 0xff).chr}
end
encode() click to toggle source
# File vendor/base32/lib/base32.rb, line 20
def encode
  n = (@bytes.length * 8.0 / 5.0).ceil
  p = n < 8 ? 5 - (@bytes.length * 8) % 5 : 0
  c = @bytes.inject(0) {|m,o| (m << 8) + o} << p
  [(0..n-1).to_a.reverse.collect {|i| Base32.table[(c >> i * 5) & 0x1f].chr},
   ("=" * (8-n))]
end