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

╰─➤ ruby enlang.rb
01010100 01101000 01100101 00100000
01000101 01110110 01101001 01101100 00100000
01010001 01110101 01100101 01100101 01101110 00101110

---

l:	01101100
Q:	01010001
h:	01101000
e:	01100101
E:	01000101
u:	01110101
space:	00100000
n:	01101110
v:	01110110
i:	01101001
period:	00101110
T:	01010100

@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