summaryrefslogtreecommitdiff
path: root/src/solvers/Challenge4.scala
diff options
context:
space:
mode:
Diffstat (limited to 'src/solvers/Challenge4.scala')
-rw-r--r--src/solvers/Challenge4.scala29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/solvers/Challenge4.scala b/src/solvers/Challenge4.scala
new file mode 100644
index 0000000..f71f6c0
--- /dev/null
+++ b/src/solvers/Challenge4.scala
@@ -0,0 +1,29 @@
+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.decryptToAscii(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(_))
+ )
+ }
+}