def main():
    with open(F, 'r') as f:
        b64 = f.read()
        enc = tools.fromB64(b64)

#    print(tools.toHex(enc))
    print(['# of bytes >=0x80: ', sum([(x >> 7) for x in enc])])

    bestScore = 0
    bestDist = 2**30
    for l in range(2, 40):
        print('L = ', l)
        bl = tools.transpose(enc, l)
        #       for blx in bl:print(tools.toHex(blx))

        distances = [
            hamming.compute(enc[i * l:(i + 1) * l],
                            enc[(i + 1) * l:(i + 2) * l])
            for i in range(len(enc) // l - 2)
        ]
        avgDist = sum(distances) / (len(distances) * l)
        print(['dist', l, distances[0] / l, avgDist])
        if bestDist > avgDist:
            bestDist = avgDist
            bestDistL = l


#       print([tools.toHex(x) for x in bl])
        dec = decrypt(enc, bl)
        if (dec[0] > bestScore):
            bestScore = dec[0]
            bestDec = dec[1]
            bestLen = l
            bestKey = dec[2]

    print('*' * 72)
    print(['best[dist=%.3f, keylen=%i]' % (bestDist, bestDistL)])
    print([
        'best[score,len,key,dec]', bestScore, bestLen,
        bytes(bestKey), bestDec
    ])
    return 0
 def test_disallow_second_strand_longer(self):
     with self.assertRaises(ValueError):
         hamming.compute('ATA', 'AGTG')
 def test_empty_strands(self):
     self.assertEqual(0, hamming.compute('', ''))
 def test_large_compute_in_off_by_one_strand(self):
     self.assertEqual(9, hamming.compute('GGACGGATTCTG', 'AGGACGGATTCT'))
 def test_same_nucleotides_in_different_positions(self):
     self.assertEqual(2, hamming.compute('TAG', 'GAT'))
 def test_small_compute_in_long_strands(self):
     self.assertEqual(2, hamming.compute('ACCAGGG', 'ACTATGG'))
 def test_small_compute_in_small_strands(self):
     self.assertEqual(1, hamming.compute('AT', 'CT'))
 def test_complete_compute_in_single_nucleotide_strands(self):
     self.assertEqual(1, hamming.compute('A', 'G'))
 def test_non_unique_character_in_second_strand(self):
     self.assertEqual(1, hamming.compute('AGG', 'AGA'))
Example #10
0
 def test_small_compute_in_long_strands(self):
     self.assertEqual(2, hamming.compute('ACCAGGG', 'ACTATGG'))
Example #11
0
 def test_small_compute(self):
     self.assertEqual(1, hamming.compute('GGACG', 'GGTCG'))
Example #12
0
 def test_small_compute_in_small_strands(self):
     self.assertEqual(1, hamming.compute('AT', 'CT'))
Example #13
0
 def test_complete_compute_in_small_strands(self):
     self.assertEqual(2, hamming.compute('AG', 'CT'))
Example #14
0
 def test_complete_compute_in_single_nucleotide_strands(self):
     self.assertEqual(1, hamming.compute('A', 'G'))
Example #15
0
 def test_long_identical_strands(self):
     self.assertEqual(0, hamming.compute('GGACTGA', 'GGACTGA'))
Example #16
0
 def test_identical_strands(self):
     self.assertEqual(0, hamming.compute('A', 'A'))
 def test_long_identical_strands(self):
     self.assertEqual(0, hamming.compute('GGACTGA', 'GGACTGA'))
Example #18
0
 def test_same_nucleotides_in_different_positions(self):
     self.assertEqual(2, hamming.compute('TAG', 'GAT'))
 def test_complete_compute_in_small_strands(self):
     self.assertEqual(2, hamming.compute('AG', 'CT'))
Example #20
0
 def test_large_compute(self):
     self.assertEqual(4, hamming.compute('GATACA', 'GCATAA'))
 def test_small_compute(self):
     self.assertEqual(1, hamming.compute('GGACG', 'GGTCG'))
Example #22
0
 def test_large_compute_in_off_by_one_strand(self):
     self.assertEqual(9, hamming.compute('GGACGGATTCTG', 'AGGACGGATTCT'))
 def test_non_unique_character_in_second_strand(self):
     self.assertEqual(1, hamming.compute('AGG', 'AGA'))
Example #24
0
 def test_very_large_compute(self):
     self.assertEqual(0, hamming.compute('CAT' * 5000, 'CAT' * 5000))
 def test_large_compute(self):
     self.assertEqual(4, hamming.compute('GATACA', 'GCATAA'))
Example #26
0
 def test_empty_strands(self):
     self.assertEqual(0, hamming.compute('', ''))
 def test_very_large_compute(self):
     self.assertEqual(0, hamming.compute('CAT' * 5000, 'CAT' * 5000))
Example #28
0
 def test_disallow_first_strand_longer(self):
     with self.assertRaises(ValueError):
         hamming.compute('AATG', 'AAA')
 def test_disallow_first_strand_longer(self):
     with self.assertRaises(ValueError):
         hamming.compute('AATG', 'AAA')
Example #30
0
 def test_disallow_second_strand_longer(self):
     with self.assertRaises(ValueError):
         hamming.compute('ATA', 'AGTG')
 def test_identical_strands(self):
     self.assertEqual(0, hamming.compute('A', 'A'))
Example #32
0
 def test_dna_strands(self):
     self.assertEqual(
         7, hamming.compute('GAGCCTACTAACGGGAT', 'CATCGTAATGACGGCCT'))