def create(request): """ This method is used to process the challenge creation request. :param request: :return: """ regex = simplify(request.form['regex']) # Create challenge chal = HashCrackKingChallenge(name=request.form['name'], description=request.form['description'], value=request.form['value'], category=request.form['category'], type=request.form['chaltype'], hold=request.form['hold'], regex=simplify(regex), cycles=request.form['cycles'], current_hash=None) if 'hidden' in request.form: chal.hidden = True else: chal.hidden = False files = request.files.getlist('files[]') for f in files: utils.upload_file(file=f, chalid=chal.id) key = generate_key(regex, chal_id=chal.id) chal.current_hash = get_hash(key) logger.debug("Generated key '{}' for challenge '{}'".format( key, chal.name)) db.session.add(chal) db.session.commit()
def update(challenge, request): """ This method is used to update the information associated with a challenge. This should be kept strictly to the Challenges table and any child tables. :param challenge: :param request: :return: """ regex = simplify(request.form['regex']) if challenge.regex != regex: challenge.regex = regex key = generate_key(regex, challenge.id) logger.debug("Generated key '{}' for challenge '{}'".format( key, challenge.name)) challenge.current_hash = get_hash(key) challenge.name = request.form['name'] challenge.description = request.form['description'] challenge.value = int(request.form.get( 'value', 0)) if request.form.get('value', 0) else 0 challenge.cycles = int(request.form.get( 'cycles', 0)) if request.form.get('cycles', 0) else 0 challenge.hold = int(request.form.get('hold', 0)) if request.form.get( 'hold', 0) else 0 challenge.category = request.form['category'] challenge.hidden = 'hidden' in request.form db.session.commit() db.session.close()
def simplify_test(): for regex, result in RS.items(): new_regex = simplify(regex) if not IS_PY3: new_regex = new_regex.encode('utf-8') r = list(generate(new_regex)) try: assert r == result except: print('[E] Assertion error! "%s"\n\t%r != %r' % (regex, r, result)) return -1
# Obtain input strings stringSet = [] while True: try: s = raw_input() stringSet.append(s) except EOFError: break # Build DFA for accepting all given strings resDFA = DFA(alpha) for s in stringSet: resDFA += DFA(alpha, s) # Optimize using state minimization resDFA = resDFA.optimize() # Plot resultant DFA and show stats resDFA.plot() # resDFA.stats() s = resDFA.toRE() print "Equivalent Regex: ", s print "Further simplified: ", exrex.simplify(s) # Validation Test # print "\n\nValidation: " # print len(stringSet) # for s in stringSet: # print resDFA.accepts(s)
import exrex # Currently used alphabet for DFAs alpha = ["a", "b"] # Obtain input strings stringSet = [] while True: try: s = raw_input() stringSet.append(s) except EOFError: break print exrex.simplify("|".join(stringSet))
# Python3.7.3 import exrex exrex.getone('(ex)r\\1') list(exrex.generate('((hai){2}|world!)')) exrex.getone('\d{4}-\d{4}-\d{4}-[0-9]{4}') exrex.getone('(1[0-2]|0[1-9])(:[0-5]\d){2} (A|P)M') exrex.count('[01]{0,9}') print('\n'.join(exrex.generate('This is (a (secret|test|whisper)|an (code|lie|truth))\.'))) print(exrex.simplify('(ab|c|d)'))