diff options
Diffstat (limited to 'src/utils/crypto')
-rw-r--r-- | src/utils/crypto/CBCCipher.scala | 21 |
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)) +} |