module Bitter

Constants

NUMBERS
VERSION
WORDS

Public Class Methods

decode(bitter_string) click to toggle source

Decode a String of bytes using the bitter word list.

# File lib/bitter.rb, line 27
def self.decode bitter_string
  words = bitter_string.split
  number = words.pop if NUMBERS.has_key? words.last
  bytes = words.collect { |word|
    twobyte = WORDS.index(word)
    second = twobyte % (2**8)
    first = twobyte / 2**8
    [first, second]
  }.flatten
  if number
    if number == 'eight'
      bytes.pop
    else
      raise RuntimeError 'Cannot decode bit-by-bit'
    end
  end
  bytes.pack('c*')
end
encode(byte_string) click to toggle source

Encode a String of bytes using the bitter word list.

# File lib/bitter.rb, line 10
def self.encode byte_string
  footer = nil
  words = byte_string.each_byte.each_slice(2).collect do |pair|
    first = pair.first
    second = if pair.size == 2
               pair[1]
             else
               footer = 'eight'
               0
             end
    WORDS[first * (2**8) + second]
  end
  words << footer if not footer.nil?
  words.join ' '
end