import sys
import hashlib
import string
import itertools
import re

def gen_hash(e, creation_date, id):
    concatenated_string = e + creation_date + id
    h = hashlib.md5(str(concatenated_string).encode('utf-8'))
    substring_h = (h.hexdigest()[0:10])
    return substring_h

def gen_magic_hash(creation_date, id, email_prefix_lenght, email_domain):
    for word in map(''.join, itertools.product(string.ascii_letters, repeat=email_prefix_lenght)):
        hash = gen_hash(word+email_domain, creation_date, id)

        if re.match("^0[eE]\d{8}", hash):
            #print("(+) STRING FROM GENERATED: %s" % word+email_domain+creation_date+id)
            return(word, hash)

def main(argv):
    if len(argv) != 5:
        print("(+) usage: %s" % sys.argv[0])
        print('(+) eg: %s "2020-03-26 14:37:44" 1 "@test.local" 2' % sys.argv[0])
        sys.exit(-1)

    # creation_date = "2020-03-26 14:37:44"
    # id = "1"
    # email_domain = "@test.local"
    # email_prefix_lenght = 2

    creation_date = argv[1]
    id = str(argv[2])
    email_domain = argv[3]
    email_prefix_lenght = int(argv[4])

    email_prefix, magic_hash = gen_magic_hash(creation_date, id, email_prefix_lenght, email_domain)

    if email_prefix:
         print("(+) Email adress for the combination of creation date %s, id %s, prefix lenght %d IS %s (%s)" % (creation_date, id, email_prefix_lenght, email_prefix+email_domain, magic_hash))
         return email_prefix+email_domain
    else:
        print("(+) BUUUU Loooser")
        return False

if __name__ == "__main__":
    main(sys.argv)