Created
June 24, 2021 07:47
-
-
Save TheSkorm/6b838e363de057614a2e28530b2c8990 to your computer and use it in GitHub Desktop.
encodes hash to words
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
# 235886 words | |
text_file = open("/usr/share/dict/words", "r") | |
lines = text_file.readlines() | |
words = [] | |
for line in lines: | |
words.append(line.strip()) | |
import struct | |
# takes 12 hex chars | |
def encode(hash): | |
binary = bytes.fromhex(hash) | |
first_word = struct.unpack('>H',bytes([binary[0], (binary[1] & 0b111111)]))[0] | |
second_word = struct.unpack('>H',bytes([(binary[1] & 0b11000000), binary[2]]))[0] | |
third_word = struct.unpack('>H',bytes([binary[3], (binary[4] & 0b111111)]))[0] | |
forth_word = struct.unpack('>H',bytes([(binary[4] & 0b11000000), binary[5]]))[0] | |
return f"{words[first_word]}-{words[second_word]}-{words[third_word]}-{words[forth_word]}" | |
def decode(word1, word2, word3, word4): | |
word1 = words.index(word1) | |
word2 = words.index(word2) | |
word3 = words.index(word3) | |
word4 = words.index(word4) | |
raw_bytes = struct.pack('>HHHH', word1, word2, word3, word4) | |
output = bytes([raw_bytes[0]]) | |
output += bytes([(raw_bytes[1] & 0b111111) + (raw_bytes[2] & 0b11000000)]) | |
output += bytes([raw_bytes[3]]) | |
output += bytes([raw_bytes[4]]) | |
output += bytes([(raw_bytes[5] & 0b111111) + (raw_bytes[6] & 0b11000000)]) | |
output += bytes([raw_bytes[7]]) | |
return output.hex() | |
def test(example): | |
words = encode(example) | |
print(f"encoded {example} to {words}") | |
print(f"decoded {words} to {decode(*words.split('-'))}") | |
print("====") | |
test("abcef1dbcec4") | |
print("====") | |
test("feedcafe1234") | |
print("====") | |
test("ffffffffffff") | |
# ==== | |
# encoded abcef1dbcec4 to costocentral-decemstriate-disulphuret-decay | |
# decoded costocentral-decemstriate-disulphuret-decay to abcef1dbcec4 | |
# ==== | |
# encoded feedcafe1234 to ethanamide-decease-etacist-Abanic | |
# decoded ethanamide-decease-etacist-Abanic to feedcafe1234 | |
# ==== | |
# encoded ffffffffffff to euchromatin-decennial-euchromatin-decennial | |
# decoded euchromatin-decennial-euchromatin-decennial to ffffffffffff |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment