Skip to content

Instantly share code, notes, and snippets.

@stephancom
Last active December 5, 2021 06:32
Show Gist options
  • Save stephancom/5fcdfa27f33930f5cbb7d76d2f476c07 to your computer and use it in GitHub Desktop.
Save stephancom/5fcdfa27f33930f5cbb7d76d2f476c07 to your computer and use it in GitHub Desktop.
a langfeldian binary encoding tool
#! /usr/bin/ruby
# full of sound and fury
class Hippie
def initialize(name)
@name = name
end
def smoke(this)
ciphertext = encode(this)
key = enkey(this)
[ciphertext, nil, '---', nil, key].join("\n")
end
private
def encode(that)
that.split(/(?<=\s)/).map { |row| asciify(row) }.join("\n")
end
def asciify(row)
row.each_byte.map { |byte| binarize(byte) }.join(' ')
end
def binarize(byte)
'%08b' % byte
end
def describe(byte)
case byte.chr
when ' '
'space'
when '.'
'period'
else
byte.chr
end
end
def key_row(byte)
"#{describe byte}:\t#{binarize byte}"
end
def enkey(that)
that.bytes.uniq.shuffle.map { |byte| key_row byte }.join("\n")
end
end
langfeld = Hippie.new('john')
puts langfeld.smoke('The Evil Queen.')
@stephancom
Copy link
Author

the sneaky part is not excluding the spaces, accomplished here via a regular expression lookback .split(/(?<=\s)/)

stolen from https://stackoverflow.com/a/18089658

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment