#!/bin/python3

import sys
import socket
import base64

# s = socket.socket()
# s.bind(('127.0.0.1', 3002))
# s.listen(5)

# conn, addr = s.accept()
# conn.send(b'Hello world')
# conn.close()

def keyValid(key):
    # format needs to be SHTP-AAAA-BBBB-CCCC
    # not allowed a < 256 || b < 256
    if len(key) != 19: return False
    
    parts = key.split('-') 
    
    if len(parts) != 4: return False
    for p in parts: 
        if len(p) != 4: return False

    if parts[0] != 'SHTP': return False

    try:
        parts = [int(p, 16) for p in parts[1:]]
    except ValueError:
        return False

    if parts[0] < 256 or parts[1] < 256: return False
    if (parts[0] ^ parts[1]) != parts[2]: return False

    return True

print("Do you have a key for me???")
key = input("> ")

if keyValid(key):
    print("Omg thanks so much, do you want to see the drawing I made with it???")
    ans = input("> ")
    if ans.lower() != "yes":
        print("Okay, bye :(")
        exit(0)

    with open("key.png", "rb") as f:
        sys.stdout.buffer.write(base64.b64encode(f.read()))
else:
    print("This key doesn't work!")
