private int GuessKeySize(byte[] data) { var results = new List <KeySizeDistance>(); for (int keySizeGuess = MinKeySize; keySizeGuess <= MaxKeySize; keySizeGuess++) { var blocks = data.Chunks(keySizeGuess).ToArray(); var distances = new List <double>(); for (int block = 0; block < NumBlocksToAverage; block++) { var b1 = blocks[block].ToArray(); var b2 = blocks[block + 1].ToArray(); var distance = HammingCalculator.CalculateHammingDistance(b1, b2); var normalized = (double)distance / keySizeGuess; distances.Add(normalized); } if (distances.Count > 0) { var average = distances.Average(); results.Add(new KeySizeDistance(keySizeGuess, average)); } } var ordered = results.OrderBy(x => x.Distance).ToArray(); return(ordered.First().KeySize); }
public void Test() { var a = "this is a test"; var b = "wokka wokka!!!"; var expectedDistance = 37; var actualDistance = HammingCalculator.CalculateHammingDistance(a, b); Assert.AreEqual(expectedDistance, actualDistance); }
public void Test1() { var a = "a"; var b = "b"; var expectedDistance = 2; var actualDistance = HammingCalculator.CalculateHammingDistance(a, b); Assert.AreEqual(expectedDistance, actualDistance); a = "c"; b = "d"; expectedDistance = 3; actualDistance = HammingCalculator.CalculateHammingDistance(a, b); Assert.AreEqual(expectedDistance, actualDistance); }
public void Test2() { var a = "Ram"; var b = "Rom"; var expectedDistance = 3; var actualDistance = HammingCalculator.CalculateHammingDistance(a, b); Assert.AreEqual(expectedDistance, actualDistance); a = "mam"; b = "man"; expectedDistance = 2; actualDistance = HammingCalculator.CalculateHammingDistance(a, b); Assert.AreEqual(expectedDistance, actualDistance); }