mirror of
https://github.com/fluencelabs/crypto
synced 2025-04-24 22:32:17 +00:00
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:
parent
1fa711960f
commit
4a966657a6
@ -1,13 +1,14 @@
|
|||||||
package fluence.crypto
|
package fluence.crypto
|
||||||
|
|
||||||
import fluence.crypto.algorithm.{ AesConfig, AesCrypt, CryptoErr }
|
import fluence.crypto.algorithm.{AesConfig, AesCrypt, CryptoErr}
|
||||||
import cats.instances.try_._
|
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 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)
|
def rndString(size: Int): String = Random.nextString(10)
|
||||||
val conf = AesConfig()
|
val conf = AesConfig()
|
||||||
@ -23,20 +24,14 @@ class AesSpec extends WordSpec with Matchers {
|
|||||||
crypt.decrypt(crypted).get shouldBe str
|
crypt.decrypt(crypted).get shouldBe str
|
||||||
|
|
||||||
val fakeAes = AesCrypt.forString[Try](ByteVector("wrong".getBytes()), withIV = true, config = conf)
|
val fakeAes = AesCrypt.forString[Try](ByteVector("wrong".getBytes()), withIV = true, config = conf)
|
||||||
fakeAes.decrypt(crypted).map(_ ⇒ false).recover {
|
checkCryptoError(fakeAes.decrypt(crypted))
|
||||||
case e: CryptoErr ⇒ true
|
|
||||||
case _ ⇒ false
|
|
||||||
}.get shouldBe true
|
|
||||||
|
|
||||||
//we cannot check if first bytes is iv or already data, but encryption goes wrong
|
//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)
|
val aesWithoutIV = AesCrypt.forString[Try](pass, withIV = false, config = conf)
|
||||||
aesWithoutIV.decrypt(crypted).get shouldNot be (str)
|
aesWithoutIV.decrypt(crypted).get shouldNot be (str)
|
||||||
|
|
||||||
val aesWrongSalt = AesCrypt.forString[Try](pass, withIV = true, config = conf.copy(salt = rndString(10)))
|
val aesWrongSalt = AesCrypt.forString[Try](pass, withIV = true, config = conf.copy(salt = rndString(10)))
|
||||||
aesWrongSalt.decrypt(crypted).map(_ ⇒ false).recover {
|
checkCryptoError(aesWrongSalt.decrypt(crypted))
|
||||||
case e: CryptoErr ⇒ true
|
|
||||||
case _ ⇒ false
|
|
||||||
}.get shouldBe true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
"work without IV" in {
|
"work without IV" in {
|
||||||
@ -48,21 +43,24 @@ class AesSpec extends WordSpec with Matchers {
|
|||||||
crypt.decrypt(crypted).get shouldBe str
|
crypt.decrypt(crypted).get shouldBe str
|
||||||
|
|
||||||
val fakeAes = AesCrypt.forString[Try](ByteVector("wrong".getBytes()), withIV = false, config = conf)
|
val fakeAes = AesCrypt.forString[Try](ByteVector("wrong".getBytes()), withIV = false, config = conf)
|
||||||
fakeAes.decrypt(crypted).map(_ ⇒ false).recover {
|
checkCryptoError(fakeAes.decrypt(crypted))
|
||||||
case e: CryptoErr ⇒ true
|
|
||||||
case _ ⇒ false
|
|
||||||
}.get shouldBe true
|
|
||||||
|
|
||||||
//we cannot check if first bytes is iv or already data, but encryption goes wrong
|
//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)
|
val aesWithIV = AesCrypt.forString[Try](pass, withIV = true, config = conf)
|
||||||
aesWithIV.decrypt(crypted).get shouldNot be (str)
|
aesWithIV.decrypt(crypted).get shouldNot be (str)
|
||||||
|
|
||||||
val aesWrongSalt = AesCrypt.forString[Try](pass, withIV = true, config = conf.copy(salt = rndString(10)))
|
val aesWrongSalt = AesCrypt.forString[Try](pass, withIV = true, config = conf.copy(salt = rndString(10)))
|
||||||
aesWrongSalt.decrypt(crypted).map(_ ⇒ false).recover {
|
checkCryptoError(aesWrongSalt.decrypt(crypted))
|
||||||
case e: CryptoErr ⇒ true
|
|
||||||
case _ ⇒ false
|
|
||||||
}.get shouldBe true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user