summaryrefslogtreecommitdiff
path: root/src/solvers/Challenge8.scala
blob: 5247075dcad0c124ca077eb26447668530096c88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package ixee.cryptopals.solvers

import scala.io.Source
import ixee.cryptopals.utils.ByteUtils._
import ixee.cryptopals.utils.StreamUtils._
import ixee.cryptopals.utils.ConversionUtils._
import ixee.cryptopals.utils.FunctionUtils._
import ixee.cryptopals.utils._
import io.github.marklister.base64.Base64._
import javax.crypto.Cipher
import javax.crypto.spec.SecretKeySpec

object Challenge8 {
  val path = "./data/8.txt"

  lazy val ciphertext =
    Source
      .fromFile(path)
      .getLines()
      .toSeq
      .map(hexStr2Bytes)
      //.mkString
      //.toByteArray

  def run = {
    ciphertext
      .zipWithIndex
      .map(x => (x._1.grouped(16).toStream, x._2 + 1)) // 
      // this might be decent, line #133 is #2 under this metric
      // .map(x => (avgHammingDistance(x._1), x._2)).sortBy(_._1)
      /*
       * but looking for actual duplicate data is better:
       * line 133 is #1 with six duplicate blocks,
       * old #1 of 177 has 0 duplicate blocks.
       */
      .map(x => (dupBlocks(x._1), x._2)).sortBy(_._1 * -1)
      .head
      ._2
  }

  def dupBlocks(xs: Seq[Seq[Byte]]) =
    pairsOf(xs).map(tup(_ == _)).count(_ == true)
}