Last active
December 5, 2021 06:32
-
-
Save stephancom/5fcdfa27f33930f5cbb7d76d2f476c07 to your computer and use it in GitHub Desktop.
a langfeldian binary encoding tool
This file contains 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
#! /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.') |
Author
stephancom
commented
Dec 5, 2021
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