def detect_xor(file_name):
    f = open(file_name, 'r')
    d = {}

    for line in f:
        line = line.rstrip().lstrip()
        candidate = decryptor.search_single_byte_xor_key(line)
        if candidate is None:
            continue

        freq = decryptor.analyze_char_freq(candidate)
        score = decryptor.score_char_freq(freq)
        d[score] = candidate
    
    return d[min(d)]
 def test_analyze_char_freq(self):
     self.assertEqual(decryptor.analyze_char_freq('ABCD'), {'A': 25, 'B': 25, 'C': 25, 'D': 25})
     self.assertEqual(decryptor.analyze_char_freq('abcd'), {'A': 25, 'B': 25, 'C': 25, 'D': 25})
     self.assertEqual(decryptor.analyze_char_freq('AbCd'), {'A': 25, 'B': 25, 'C': 25, 'D': 25})
     self.assertEqual(decryptor.analyze_char_freq('AABCD'), {'A': 40, 'B': 20, 'C': 20, 'D': 20})
     self.assertEqual(decryptor.analyze_char_freq('KAABCD?/DA'), {'A': 30, 'B': 10, 'C': 10, 'D': 20, 'K': 10, '?': 10, '/': 10})