Skip to content

Instantly share code, notes, and snippets.

@numberoverzero
Created March 29, 2018 01:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save numberoverzero/06f8e58055871d805d885b6a05ba8768 to your computer and use it in GitHub Desktop.
Save numberoverzero/06f8e58055871d805d885b6a05ba8768 to your computer and use it in GitHub Desktop.
bank file loading
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