summaryrefslogtreecommitdiff
path: root/src/utils/CryptoUtils.scala
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/CryptoUtils.scala')
-rw-r--r--src/utils/CryptoUtils.scala22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/utils/CryptoUtils.scala b/src/utils/CryptoUtils.scala
index 54e5034..7a31002 100644
--- a/src/utils/CryptoUtils.scala
+++ b/src/utils/CryptoUtils.scala
@@ -1,6 +1,8 @@
package ixee.cryptopals.utils
import ixee.cryptopals.utils.crypto._
+import ixee.cryptopals.utils.StreamUtils._
+import ixee.cryptopals.utils.FunctionUtils._
import javax.crypto.Cipher
import javax.crypto.spec.SecretKeySpec
@@ -19,4 +21,24 @@ object CryptoUtils {
def cbcDecrypt(builder: CbcBuilder)(data: Seq[Byte]) =
stripPkcs7Pad(builder.decrypt.end(data))
+
+ def ecbEncrypt(builder: EcbBuilder)(data: Seq[Byte]) =
+ builder.encrypt.end(data)
+
+ def ecbDecrypt(builder: EcbBuilder)(data: Seq[Byte]) =
+ stripPkcs7Pad(builder.decrypt.end(data))
+
+ def detectMode(xs: Seq[Byte]): String = {
+ def dupBlocks(xs: Seq[Seq[Byte]]) =
+ pairsOf(xs).map(tup(_ == _)).count(_ == true)
+
+ def countDupBlocks(xs: Seq[Byte]): Int =
+ dupBlocks(xs.grouped(16).toSeq.init.toStream)
+
+ //.... well very probably.
+ if (countDupBlocks(xs) > 0) "ECB"
+ else "CBC"
+
+ }
+
}