Skip to content

Instantly share code, notes, and snippets.

@asfaltboy
Created November 23, 2016 18:12
Show Gist options
  • Save asfaltboy/68fb49389677b43643120af51ef27bb2 to your computer and use it in GitHub Desktop.
Save asfaltboy/68fb49389677b43643120af51ef27bb2 to your computer and use it in GitHub Desktop.
anagram phrase comparison
from hashlib import md5
from collections import Counter
import itertools
import sys
import time
MATCH_MSG = '>>> We have a match!!! This phrase matches the mt5 hash'
def get_relevant_words(target):
# get a set of unique dictionary words
dictionary = set([w.strip() for w in open('wordlist').readlines()])
target_counter = Counter(target)
relevant_words = []
# a single iteration to reduce the number of words to ~ 1.5k
for word in dictionary:
c = Counter(word)
if set(c) - set(target_counter):
continue # contains letters that are not in our target
if any(1 for k in c if c[k] > target_counter[k]):
continue # contains letters that repeat more than in our target
relevant_words.append(word)
def main(phrase):
target = sorted(''.join(phrase.split()))
md5_match = md5(target).hexdigest()
relevant_words = get_relevant_words(target)
anagrams = 0
start = time.time()
# in increasing difficulty, attempt to
for n in range(1, 10):
print('Looking for anagram of "%s" with %s word/s' % (phrase, n))
for i, words in enumerate(itertools.combinations(relevant_words, n)):
# check if this word combination is Anagram of target
if sorted(''.join(words)) == target:
# look for all permutations of these words
for anagram_words in itertools.permutations(words):
anagrams += 1
sentence = ' '.join(anagram_words)
if md5(sentence).hexdigest() == md5_match:
print('[+] Target anagram found:', sentence,
md5(sentence).hexdigest(), MATCH_MSG)
return sentence
# else:
# print('[-] Anagram found:', sentence,
# md5(sentence).hexdigest())
if anagrams % 200 == 0:
if anagrams == 0:
continue
print('processed {} combinations, {} anagrams found, '
'in {} seconds ({}/ps, {}/as)'.format(
i, anagrams, int(time.time() - start),
round(i / (time.time() - start)),
round(anagrams / (time.time() - start), 1)))
if __name__ == '__main__':
main(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment