Example #1
0
        public void Challenge08_DetectEcb()
        {
            var lines      = File.ReadAllLines("8.txt");
            var candidates = new Dictionary <int, double>(lines.Length);

            for (var i = 0; i < lines.Length; i++)
            {
                var line   = lines[i];
                var cipher = Hex.ToBytes(line);

                var same = new HashSet <int>(cipher.Length / 16);
                for (int segment1 = 0; segment1 < cipher.Length / 16; ++segment1)
                {
                    if (same.Contains(segment1))
                    {
                        continue;
                    }

                    for (int segment2 = segment1 + 1; segment2 < cipher.Length / 16; ++segment2)
                    {
                        if (same.Contains(segment2))
                        {
                            continue;
                        }

                        if (0 == Hamming.GetDistance(cipher.AsSpan(segment1 * 16, 16), cipher.AsSpan((segment2) * 16, 16)))
                        {
                            same.Add(segment1);
                            same.Add(segment2);
                        }
                    }
                }

                candidates[i + 1] = same.Count;
            }

            var lnNr = candidates.OrderByDescending(x => x.Value).First().Key;

            Assert.Equal(133, lnNr);
        }
Example #2
0
 public void GetHammingDistance_EmptyStrands_DistanceIsZero()
 {
     Assert.AreEqual(0, Hamming.GetDistance(string.Empty, string.Empty));
 }
Example #3
0
 public void GetHammingDistance_SecondStrandLonger_ThrowsArgumentException()
 {
     Assert.Throws <ArgumentException>(() => Hamming.GetDistance("ATA", "AGTG"));
 }
Example #4
0
 public void GetHammingDistance_FirstStrandLonger_ThrowsArgumentException()
 {
     Assert.Throws <ArgumentException>(() => Hamming.GetDistance("AATG", "AAA"));
 }
Example #5
0
 public void GetHammingDistance_OneNonUniqueCharacterInStrand_DistanceIsOne()
 {
     Assert.AreEqual(1, Hamming.GetDistance("AAA", "AAG"));
 }
Example #6
0
 public void GetHammingDistance_LongStrandsWithTwoDifference_DistanceIsTwo()
 {
     Assert.AreEqual(2, Hamming.GetDistance("ACCAGGG", "ACTATGG"));
 }
Example #7
0
 public void GetHammingDistance_SmallStrandsWithTwoDifference_DistanceIsTwo()
 {
     Assert.AreEqual(2, Hamming.GetDistance("AG", "CT"));
 }
Example #8
0
 public void GetHammingDistance_SmallStrandsWithOneDifference_DistanceIsOne()
 {
     Assert.AreEqual(1, Hamming.GetDistance("AT", "CT"));
 }
Example #9
0
 public void GetHammingDistance_OneDifference_DistanceOne()
 {
     Assert.AreEqual(1, Hamming.GetDistance("A", "G"));
 }
Example #10
0
 public void GetHammingDistance_IdenticalStrands_DistanceIsZero(string strand)
 {
     Assert.AreEqual(0, Hamming.GetDistance(strand, strand));
 }