From 510dc673e1402ed84945c4be8454bda8ecb8512a Mon Sep 17 00:00:00 2001 From: iximeow Date: Wed, 3 Dec 2014 09:22:29 -0800 Subject: Refactoring... --- src/utils/CryptoUtils.scala | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/utils/CryptoUtils.scala b/src/utils/CryptoUtils.scala index 658786a..c64c597 100644 --- a/src/utils/CryptoUtils.scala +++ b/src/utils/CryptoUtils.scala @@ -68,7 +68,7 @@ object CryptoUtils { val baseCiphert = new Ciphertext(encrypt) - val cipherts = Seq(baseCiphert) ++ + val ciphertexts = Seq(baseCiphert) ++ (1 to 15) .map( x => ("@" * x).asBytes ) .map(new Ciphertext(encrypt, _, overrideBlockSize = Some(baseCiphert.blockSize))) @@ -94,13 +94,13 @@ object CryptoUtils { def rainbowPrefix(suffix: Seq[Byte]): Map[Seq[Byte], Byte] = rainbow(suffix, (byte, seq) => byte +: seq) - def probeFirstBlockAndPaddings: (Seq[Byte], Map[Int, Seq[Byte]]) = { + def probeFirstBlock: Seq[Byte] = { def prefix(known: Seq[Byte]) = ("@" * (blockSize - 1 - known.length)).asBytes def genRainbow(known: Seq[Byte]) = rainbowSuffix(prefix(known) ++ known) def firstCryptedBlock(known: Seq[Byte]) = encrypt(prefix(known)).take(blockSize).toSeq - def nextByte(known: Seq[Byte]) = genRainbow(known)(firstCryptedBlock(known)) - (0 until 16).foldLeft((Seq[Byte](), Map[Int, Seq[Byte]]())) { (ac, idx) => - (ac._1 :+ nextByte(ac._1), ac._2 + (idx -> encrypt(prefix(ac._1)))) + def nextByte(known: Seq[Byte]) = known :+ genRainbow(known)(firstCryptedBlock(known)) + (0 until 16).foldLeft(Seq[Byte]()) { (ac, idx) => + nextByte(ac) } } @@ -115,24 +115,20 @@ object CryptoUtils { blockSize - firstLargerCiphertext.get._2 } - val (firstBlock, ciphertexts) = probeFirstBlockAndPaddings + val firstBlock = probeFirstBlock /* * zip together cipher blocks so that they look like * rot0b0, rot1b0, rot2b0, rot3b0, rot4b0, rot5b0, rot6b0, rot7b0 * rot0b1, rot1b1, rot2b1, ... * */ - val cipherBlocks = ciphertexts.toSeq.sortBy(_._1).map(_ :-> { x => - x.grouped(16).toSeq - }) - val (middleBlocks, lastBlocks) = - cipherBlocks.foldLeft((Seq[Seq[Seq[Byte]]](), Seq[Option[Seq[Byte]]]()))( (ac, curr) => { + ciphertexts.foldRight((Seq[Seq[Seq[Byte]]](), Seq[Option[Seq[Byte]]]()))( (curr, ac) => { val maybeLastBlock = - if (curr._2.length > baseBlockCount) Some(curr._2.last) + if (curr.blockCount > baseBlockCount) Some(curr.blocks.last) else None - val middleBlocks = curr._2.take(baseBlockCount).tail + val middleBlocks = curr.blocks.take(baseBlockCount).tail (ac._1 :+ middleBlocks, maybeLastBlock +: ac._2) }) :-> { _.flatten :+ baseCiphert.blocks.last } //baseCiphertext.takeRight(16)} -- cgit v1.1