Created
February 23, 2009 01:08
-
-
Save postmodern/68714 to your computer and use it in GitHub Desktop.
A Yo-based communication system
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module SophSec | |
module YoSup | |
WORDS = %w{dawg sup hi yo} | |
def YoSup.encode(message) | |
encoded = [] | |
message = message.to_s | |
message.each_byte do |b| | |
encoded << WORDS[(b & 0x03)] | |
encoded << WORDS[((b & 0x0c) >> 2)] | |
encoded << WORDS[((b & 0x30) >> 4)] | |
encoded << WORDS[((b & 0xc0) >> 6)] | |
end | |
return encoded.join(' ') | |
end | |
def YoSup.decode(message) | |
decoded = '' | |
lookup = lambda { |word| | |
WORDS.index(word) || 0 | |
} | |
message.gsub(/[\(\),;:\.\?]/,'').split(' ').each_slice(4) do |words| | |
b = 0 | |
b |= lookup.call(words[0]) | |
b |= (lookup.call(words[1]) << 2) | |
b |= (lookup.call(words[2]) << 4) | |
b |= (lookup.call(words[3]) << 6) | |
decoded << b.chr | |
end | |
return decoded | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment