summaryrefslogtreecommitdiff
path: root/src/utils/crypto
diff options
context:
space:
mode:
authoriximeow <me@iximeow.net>2014-11-26 23:17:51 -0800
committeriximeow <me@iximeow.net>2014-11-26 23:17:51 -0800
commit9877ccf7fe7df38f8ecc65d01e9b46a98528502f (patch)
tree328cb032b21b5bac677ad091562fbc2bfb197fae /src/utils/crypto
parent91b4cfc801b7c156c231ca60aba69a8d171e9226 (diff)
Add part of CBC mode
Diffstat (limited to 'src/utils/crypto')
-rw-r--r--src/utils/crypto/CBCCipher.scala21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/utils/crypto/CBCCipher.scala b/src/utils/crypto/CBCCipher.scala
new file mode 100644
index 0000000..e1ece8a
--- /dev/null
+++ b/src/utils/crypto/CBCCipher.scala
@@ -0,0 +1,21 @@
+package ixee.cryptopals.utils.crypto
+
+import javax.crypto.Cipher
+
+class CBCCipher(cipher: Cipher, init: Seq[Byte]) {
+ var state: Seq[Byte] = init
+ var leftover: Seq[Byte] = Seq()
+
+ def enc(data: Seq[Byte]): Seq[Byte] = {
+ (blocks, leftover) = blockized(leftover append data)
+ blocks.map(encBlock).reduce(_ append _)
+ }
+
+ def encBlock(data: Seq[Byte]): Seq[Byte] = {
+ state = cipher.doFinal((data xor state).toArray)
+ state
+ }
+
+ def blockized(data: Seq[Byte])(size: Int): (Seq[Seq[Byte]], Seq[Byte]) =
+ data.splitAt(data.length - (data.length % size))
+}