Sbt js modules refactoring (#70)

* replace codec/core and rename jvm-js modules

* add files to git

* add info into test
This commit is contained in:
Dima 2018-02-27 14:46:09 +03:00 committed by Dmitry Kurinskiy
parent 1fa711960f
commit 4a966657a6

View File

@ -1,13 +1,14 @@
package fluence.crypto
import fluence.crypto.algorithm.{ AesConfig, AesCrypt, CryptoErr }
import fluence.crypto.algorithm.{AesConfig, AesCrypt, CryptoErr}
import cats.instances.try_._
import org.scalatest.{ Matchers, WordSpec }
import org.scalactic.source.Position
import org.scalatest.{Assertion, Matchers, WordSpec}
import scodec.bits.ByteVector
import scala.util.{ Random, Try }
import scala.util.{Random, Try}
class AesSpec extends WordSpec with Matchers {
class AesSpec extends WordSpec with Matchers with slogging.LazyLogging {
def rndString(size: Int): String = Random.nextString(10)
val conf = AesConfig()
@ -23,20 +24,14 @@ class AesSpec extends WordSpec with Matchers {
crypt.decrypt(crypted).get shouldBe str
val fakeAes = AesCrypt.forString[Try](ByteVector("wrong".getBytes()), withIV = true, config = conf)
fakeAes.decrypt(crypted).map(_ false).recover {
case e: CryptoErr true
case _ false
}.get shouldBe true
checkCryptoError(fakeAes.decrypt(crypted))
//we cannot check if first bytes is iv or already data, but encryption goes wrong
val aesWithoutIV = AesCrypt.forString[Try](pass, withIV = false, config = conf)
aesWithoutIV.decrypt(crypted).get shouldNot be (str)
val aesWrongSalt = AesCrypt.forString[Try](pass, withIV = true, config = conf.copy(salt = rndString(10)))
aesWrongSalt.decrypt(crypted).map(_ false).recover {
case e: CryptoErr true
case _ false
}.get shouldBe true
checkCryptoError(aesWrongSalt.decrypt(crypted))
}
"work without IV" in {
@ -48,21 +43,24 @@ class AesSpec extends WordSpec with Matchers {
crypt.decrypt(crypted).get shouldBe str
val fakeAes = AesCrypt.forString[Try](ByteVector("wrong".getBytes()), withIV = false, config = conf)
fakeAes.decrypt(crypted).map(_ false).recover {
case e: CryptoErr true
case _ false
}.get shouldBe true
checkCryptoError(fakeAes.decrypt(crypted))
//we cannot check if first bytes is iv or already data, but encryption goes wrong
val aesWithIV = AesCrypt.forString[Try](pass, withIV = true, config = conf)
aesWithIV.decrypt(crypted).get shouldNot be (str)
val aesWrongSalt = AesCrypt.forString[Try](pass, withIV = true, config = conf.copy(salt = rndString(10)))
aesWrongSalt.decrypt(crypted).map(_ false).recover {
case e: CryptoErr true
case _ false
}.get shouldBe true
checkCryptoError(aesWrongSalt.decrypt(crypted))
}
}
def checkCryptoError(tr: Try[String])(implicit pos: Position): Assertion = {
tr.map(_ false).recover {
case e: CryptoErr true
case e
logger.error("Unexpected error", e)
false
}.get shouldBe true
}
}