Exemplo n.º 1
0
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
Exemplo n.º 2
0
 def test_disallow_second_strand_longer(self):
     with self.assertRaises(ValueError):
         hamming.compute('ATA', 'AGTG')
Exemplo n.º 3
0
 def test_empty_strands(self):
     self.assertEqual(0, hamming.compute('', ''))
Exemplo n.º 4
0
 def test_large_compute_in_off_by_one_strand(self):
     self.assertEqual(9, hamming.compute('GGACGGATTCTG', 'AGGACGGATTCT'))
Exemplo n.º 5
0
 def test_same_nucleotides_in_different_positions(self):
     self.assertEqual(2, hamming.compute('TAG', 'GAT'))
Exemplo n.º 6
0
 def test_small_compute_in_long_strands(self):
     self.assertEqual(2, hamming.compute('ACCAGGG', 'ACTATGG'))
Exemplo n.º 7
0
 def test_small_compute_in_small_strands(self):
     self.assertEqual(1, hamming.compute('AT', 'CT'))
Exemplo n.º 8
0
 def test_complete_compute_in_single_nucleotide_strands(self):
     self.assertEqual(1, hamming.compute('A', 'G'))
Exemplo n.º 9
0
 def test_non_unique_character_in_second_strand(self):
     self.assertEqual(1, hamming.compute('AGG', 'AGA'))
Exemplo n.º 10
0
 def test_small_compute_in_long_strands(self):
     self.assertEqual(2, hamming.compute('ACCAGGG', 'ACTATGG'))
Exemplo n.º 11
0
 def test_small_compute(self):
     self.assertEqual(1, hamming.compute('GGACG', 'GGTCG'))
Exemplo n.º 12
0
 def test_small_compute_in_small_strands(self):
     self.assertEqual(1, hamming.compute('AT', 'CT'))
Exemplo n.º 13
0
 def test_complete_compute_in_small_strands(self):
     self.assertEqual(2, hamming.compute('AG', 'CT'))
Exemplo n.º 14
0
 def test_complete_compute_in_single_nucleotide_strands(self):
     self.assertEqual(1, hamming.compute('A', 'G'))
Exemplo n.º 15
0
 def test_long_identical_strands(self):
     self.assertEqual(0, hamming.compute('GGACTGA', 'GGACTGA'))
Exemplo n.º 16
0
 def test_identical_strands(self):
     self.assertEqual(0, hamming.compute('A', 'A'))
Exemplo n.º 17
0
 def test_long_identical_strands(self):
     self.assertEqual(0, hamming.compute('GGACTGA', 'GGACTGA'))
Exemplo n.º 18
0
 def test_same_nucleotides_in_different_positions(self):
     self.assertEqual(2, hamming.compute('TAG', 'GAT'))
Exemplo n.º 19
0
 def test_complete_compute_in_small_strands(self):
     self.assertEqual(2, hamming.compute('AG', 'CT'))
Exemplo n.º 20
0
 def test_large_compute(self):
     self.assertEqual(4, hamming.compute('GATACA', 'GCATAA'))
Exemplo n.º 21
0
 def test_small_compute(self):
     self.assertEqual(1, hamming.compute('GGACG', 'GGTCG'))
Exemplo n.º 22
0
 def test_large_compute_in_off_by_one_strand(self):
     self.assertEqual(9, hamming.compute('GGACGGATTCTG', 'AGGACGGATTCT'))
Exemplo n.º 23
0
 def test_non_unique_character_in_second_strand(self):
     self.assertEqual(1, hamming.compute('AGG', 'AGA'))
Exemplo n.º 24
0
 def test_very_large_compute(self):
     self.assertEqual(0, hamming.compute('CAT' * 5000, 'CAT' * 5000))
Exemplo n.º 25
0
 def test_large_compute(self):
     self.assertEqual(4, hamming.compute('GATACA', 'GCATAA'))
Exemplo n.º 26
0
 def test_empty_strands(self):
     self.assertEqual(0, hamming.compute('', ''))
Exemplo n.º 27
0
 def test_very_large_compute(self):
     self.assertEqual(0, hamming.compute('CAT' * 5000, 'CAT' * 5000))
Exemplo n.º 28
0
 def test_disallow_first_strand_longer(self):
     with self.assertRaises(ValueError):
         hamming.compute('AATG', 'AAA')
Exemplo n.º 29
0
 def test_disallow_first_strand_longer(self):
     with self.assertRaises(ValueError):
         hamming.compute('AATG', 'AAA')
Exemplo n.º 30
0
 def test_disallow_second_strand_longer(self):
     with self.assertRaises(ValueError):
         hamming.compute('ATA', 'AGTG')
Exemplo n.º 31
0
 def test_identical_strands(self):
     self.assertEqual(0, hamming.compute('A', 'A'))
Exemplo n.º 32
0
 def test_dna_strands(self):
     self.assertEqual(
         7, hamming.compute('GAGCCTACTAACGGGAT', 'CATCGTAATGACGGCCT'))