Created
March 29, 2018 01:13
-
-
Save numberoverzero/06f8e58055871d805d885b6a05ba8768 to your computer and use it in GitHub Desktop.
bank file loading
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 hashlib | |
import untangle | |
def load(filename): | |
with open(filename) as f: | |
return untangle.parse(f.read()) | |
def canonicalize_section(section): | |
keys = section.Key | |
if not isinstance(keys, list): | |
keys = [keys] | |
keys = sorted(keys, key=lambda k: k["name"]) | |
string = section["name"] | |
for key in keys: | |
# for entry_name in sorted(dir(key)): | |
string += key["name"] | |
for entry in sorted(key.children, key=lambda k: k._name): | |
name = entry._name | |
attrs = list(entry._attributes.items()) | |
assert len(attrs) == 1, "Basic unpacking only!" | |
type, value = attrs[0] | |
string += name + type | |
if type != "text": | |
string += value | |
# attrs = list(key.Value._attributes.items()) | |
# assert len(attrs) == 1, "Basic unpacking only!" | |
# type, value = attrs[0] | |
# string += key["name"] + "Value" + type + value | |
return string | |
def canonicalize_bank(bank): | |
sections = bank.Section | |
if not isinstance(sections, list): | |
sections = [sections] | |
sections = sorted(sections, key=lambda s: s["name"]) | |
string = "" | |
for section in sections: | |
string += canonicalize_section(section) | |
return string | |
def canonicalize_bank_file(author, player, name, bank): | |
return author + player + name + canonicalize_bank(bank) | |
def check_signature(author, player, name, bank): | |
string_to_sign = canonicalize_bank_file(author, player, name, bank) | |
print(string_to_sign) | |
expected = hashlib.sha1(string_to_sign.encode("utf-8")).hexdigest().upper() | |
actual = bank.Signature["value"] | |
print(f"expected {expected}, was actual {actual}") | |
return expected == actual | |
if __name__ == '__main__': | |
#test = "SIMPLE" | |
test = "COMPLEX" | |
print(test) | |
if test == "SIMPLE": | |
author = "1-S2-1-7042791" | |
player = "1-S2-1-3638286" | |
name = "crappatrol2" | |
filename = f"{name}.SC2Bank" | |
bank = load(filename).Bank | |
print(check_signature(author, player, name, bank)) | |
elif test == "COMPLEX": | |
author = "1-S2-1-1728572" | |
player = "1-S2-1-3638286" | |
name = "adsocobx" | |
filename = f"{name}.SC2Bank" | |
bank = load(filename).Bank | |
print(check_signature(author, player, name, bank)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment