Class: Scl::Words

Inherits:
Format show all
Defined in:
lib/scl/formats/words.rb

Constant Summary collapse

WORDS =
IO.read(File.expand_path(__FILE__+"/../wordlist.txt")).split("\n")
INDICES =
Hash[WORDS.map.with_index{|w, i|[w,i]

Constants inherited from Format

Format::AUTO, Format::BASE64, Format::BINARY, Format::HEX, Format::QRCODE, Format::STDOUT

Instance Method Summary collapse

Methods inherited from Format

#name, #output, #read

Instance Method Details

#decode(data) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/scl/formats/words.rb', line 13

def decode(data)
  bytes = data.split(' ').each.map do |word|
    bytes = INDICES[word]
    b2 = bytes & 255
    b1 = (bytes >> 8) & 255
    [b1, b2]
  end.flatten
  bytes.map(&:chr).join.gsub(/\x00$/,'')
end

#encode(data) ⇒ Object



7
8
9
10
11
# File 'lib/scl/formats/words.rb', line 7

def encode(data)
  data.bytes.each_slice(2).map do |b1, b2|
    WORDS[(b1 << 8) + (b2 || 0)]
  end.join(' ')
end