summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authoriximeow <me@iximeow.net>2014-11-28 04:22:20 -0800
committeriximeow <me@iximeow.net>2014-11-28 04:22:20 -0800
commit47888049de29336c3f753d0372b37923d8c71121 (patch)
tree1cd7d3ee2e2d6f0ed3b1357e285f87120bc50dd8 /test
parent6268e2c446d6d54b43668d859184d6f3a779fa3a (diff)
Challenge 11
Diffstat (limited to 'test')
-rw-r--r--test/Gloria.scala33
-rw-r--r--test/Set2Spec.scala11
2 files changed, 44 insertions, 0 deletions
diff --git a/test/Gloria.scala b/test/Gloria.scala
new file mode 100644
index 0000000..3141a3a
--- /dev/null
+++ b/test/Gloria.scala
@@ -0,0 +1,33 @@
+package ixee.cryptopals.test
+
+import ixee.cryptopals.utils._
+import CryptoUtils._
+import crypto.SchemeBuilder
+
+object Gloria {
+
+ val decide = new java.util.Random()
+
+ def encryptify(input: Seq[Byte]): (String, Seq[Byte]) = {
+ val mode = if (Gloria.decide.nextBoolean) "ECB" else "CBC"
+
+ val betterText =
+ Gloria.say(atLeast = 5, atMost = 10) ++ input ++ Gloria.say(atLeast = 5, atMost = 10)
+
+ val builder = SchemeBuilder("AES", Gloria.sayExactly(16))
+
+ val encryptor =
+ if (mode == "ECB") ecbEncrypt(builder.ecb) _
+ else cbcEncrypt(builder.cbc(Gloria.sayExactly(16))) _
+
+ (mode, encryptor(betterText))
+ }
+
+ def voice: Iterator[Byte] = Stream.continually(Gloria.decide.nextInt.toByte).iterator
+
+ def sayExactly(n: Int) = voice.take(n).toSeq
+
+ def say(atLeast: Int, atMost: Int): Seq[Byte] =
+ (voice.take(atLeast) ++ voice.take(Gloria.decide.nextInt(atMost - atLeast))).toSeq
+
+}
diff --git a/test/Set2Spec.scala b/test/Set2Spec.scala
index d230a18..156949b 100644
--- a/test/Set2Spec.scala
+++ b/test/Set2Spec.scala
@@ -73,6 +73,17 @@ class Set2Spec extends IxeeSpec {
}
}
+
+ "Challenge 11: ECB/CBC detection" - {
+
+ val input = "keke" * 32
+
+ val (mode, ciphertext) = Gloria.encryptify(input.asBytes)
+
+ CryptoUtils.detectMode(ciphertext) mustBe mode // ...always.
+
+ }
+
}
}