import json
import base64


def decode_hash(hash_encode):
    ddata = {}
    for key, value in hash_encode.items():
        ddata[key] = base64.b64decode(value).hex()
    return ddata


def search_password(target_pass, rainbow_dict):
    for key, value in rainbow_dict.items():
        if value == target_pass:
            return key
    return None


# Original passwords
ori_pwd = ["was123", "gil123", "wgp123", "vut123", "lrz123"]

# Stored hashes
hashp = [
    "e0ac26da8755c76127cc12356577f11e2e6ab72071c27678fd2fb1b19fb6190307587de24f7b00d4e1d0115733453a5310f3968d151b13106a45bbde8e76558e",
    "da8a0ac4a5577d2bbe5c605597c43339db0a6516ebb942b09510eb8a52d78c764744db60f9fcd8c98db0ddef8fda71bcf9a4454847382475b9efafc65ccf74a7",
    "bbe347565bd9f575f3d48dce800f73d1e7e70cd5f103b335ab487271645fe4ee399cd1c7af6334ddeaae0c1e561ecb85770961a4b73e5092788a7d6665ec9f35",
    "b3e5879bf0f340793eb2a3be23d64afd5c75d2318e7a4a74b9dd06e97c01ce2564ea57b97bad17273c0e82787d19b07f5619323b011c93066d8dc6d0951bf89f",
    "39e8a47bdd6a67340269c0fcffedc38786abcdbfd748bb6419fb3697c3d1cbdf1c7fe9a5bc9972b0931a5511fd6b1cdfb5e28f922b2525dc8d1b0d51ecfdc2a3",
]


rainbow_dict = None
with open("rainbow_table.json", "r") as f:
    rainbow_dict = json.load(f)
hash_dict = decode_hash(rainbow_dict)
for key, value in hash_dict.items():
    print(key, value)
    break
print(search_password(hashp[0], hash_dict))
