Last active
July 15, 2019 20:23
-
-
Save zachmullen/c347f39b9ca963889425b0f3004eceb1 to your computer and use it in GitHub Desktop.
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
import struct | |
import typing | |
def humanize(val: bytes, words: typing.List[str]) -> typing.List[str]: | |
return [words[struct.unpack_from("H", val, i)[0]] for i in range(0, len(val), 2)] | |
def dehumanize(val: typing.List[str], words_map: typing.Dict[str, int]) -> bytes: | |
return b"".join((struct.pack("H", words_map[word]) for word in val)) | |
if __name__ == "__main__": | |
# Bring your own word list with length=65536 | |
with open("words.txt") as f: | |
words = [w.strip() for w in f.readlines()] | |
words_map = {word: i for i, word in enumerate(words)} | |
# Warning: assumes buffers to encode have an even number of bytes | |
encoded = "_".join(humanize(b"123456", words)) | |
decoded = dehumanize(encoded.split("_"), words_map) | |
print(encoded) | |
print(decoded) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment