package ixee.cryptopals.solvers import scala.io.Source import ixee.cryptopals.utils.ByteUtils._ import ixee.cryptopals.utils.ConversionUtils._ import ixee.cryptopals.utils.FunctionUtils._ import ixee.cryptopals.utils._ object Challenge4 { def findSingleCharacterXor(strings: Seq[Seq[Byte]]): (String, Int) = { strings .map(XorDecrypt.findBestSingleByteKey) .zip(strings) .zipWithIndex .map { pair => (pair._1._1, pair._1._2, pair._2) } .filter(_._1 != 0) .map { triplet => (XorDecrypt.decryptToAsciiSingleByteKey(triplet._2)(triplet._1), triplet._3) } .map { pair => (pair._1, TextScorer.score(pair._1.asBytes), pair._2) } .sortBy(_._2) .map { triplet => (triplet._1, triplet._3) } .head } def run(inputFile: String) = { findSingleCharacterXor( Source.fromFile(inputFile).getLines().toSeq.map(hexStr2Bytes(_)) ) } }