summaryrefslogtreecommitdiff
path: root/src/utils/crypto/CipherGenerator.scala
blob: 391710b206f22a6d13aba6e15a3c67632678d311 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package ixee.cryptopals.utils.crypto

import javax.crypto.Cipher

sealed trait CipherGenerator {
  def encrypt: IxeeCipher // feels kinda gross to type this
  def decrypt: IxeeCipher // writing crypto feels wrong
}

case class EcbBuilder(primitives: SchemeBuilder) extends CipherGenerator {
  // lol
  def encrypt =
    setup(Cipher.ENCRYPT_MODE)
  def decrypt =
    setup(Cipher.DECRYPT_MODE)

  private[this] def setup(cipherMode: Int) = {
    val cipher = primitives.cipher
    cipher.init(cipherMode, primitives.key)
    new EcbCipher(cipher, cipherMode)
  }
}

case class CbcBuilder(primitives: SchemeBuilder, iv: Seq[Byte]) extends CipherGenerator {
  def encrypt =
    setup(Cipher.ENCRYPT_MODE)
  def decrypt =
    setup(Cipher.DECRYPT_MODE)

  private[this] def setup(cipherMode: Int) = {
    val cipher = primitives.cipher
    cipher.init(cipherMode, primitives.key)
    new CbcCipher(cipher, iv, cipherMode)
  }
}