summaryrefslogtreecommitdiff
path: root/test/Gloria.scala
blob: 3141a3ad2fe603cd07dc1f1789c5b91a78345c01 (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
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

}