import re
#Arcor- , EasyBox- , Vodafone-
def control_mac(mac):
    mac = re.sub(r'[^a-fA-F0-9]', '', mac)
    if len(mac) != 12:
        return False
    else:
        try:
            mac.decode("hex")
            return mac
        except TypeError:
            return False
            
def calc(mac):
    mac = control_mac(mac)
    if mac == False:
        return False
    macint =  [mac[x:x+2] for x in xrange(0,len(mac),2)]
    svals = str(int(macint[4]+macint[5], 16)).zfill(5)
    s6 = svals[0]
    s7 = svals[1]
    s8 = svals[2]
    s9 = svals[3]
    s10 = svals[4]
    m7 = mac[6]
    m8 = mac[7]
    m9 = mac[8]
    m10 = mac[9]
    m11 = mac[10]
    m12 = mac[11]
    k1 = str(hex(int(s7)+int(s8)+int(m11,16)+int(m12,16)))[-1:]
    k2 = str(hex(int(s9)+int(s10)+int(m9,16)+int(m10,16)))[-1:]
    x1 = str(hex(int(k1,16) ^ int(s10,16)))[-1:]
    x2 = str(hex(int(k1,16) ^ int(s9,16)))[-1:]
    x3 = str(hex(int(k1,16) ^ int(s8,16)))[-1:]
    y1 = str(hex(int(k2,16) ^ int(m10,16)))[-1:]
    y2 = str(hex(int(k2,16) ^ int(m11,16)))[-1:]
    y3 = str(hex(int(k2,16) ^ int(m12,16)))[-1:]
    z1 = str(hex(int(m11,16) ^ int(s10,16)))[-1:]
    z2 = str(hex(int(m12,16) ^ int(s9,16)))[-1:]
    z3 = str(hex(int(k1,16) ^ int(k2,16)))[-1:]
    key = [(x1 + y1 + z1 + x2 + y2 + z2 + x3 + y3 + z3).upper()]
     
    return key


