Skip to content

Commit

Permalink
Merge pull request #25 from hmrc/BDOG-3112
Browse files Browse the repository at this point in the history
BDOG-3112 Scala 3
  • Loading branch information
colin-lamed authored May 14, 2024
2 parents dceb43e + cb43e70 commit 7e08bfe
Show file tree
Hide file tree
Showing 16 changed files with 32 additions and 28 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ val optValue: Option[T] = decrypter.reads(encryptedValue).asOpt.map(_.decryptedV

## Changes

### Version 8.0.0

Built for Scala 3 and Scala 2.13 - drops Scala 2.12.

### Version 7.6.0

Adds support for Play 2.9 and Play 3.0
Expand Down
10 changes: 5 additions & 5 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
val scala2_12 = "2.12.18"
val scala2_13 = "2.13.12"
val scala3 = "3.3.3"

ThisBuild / majorVersion := 7
ThisBuild / majorVersion := 8
ThisBuild / scalaVersion := scala2_13
ThisBuild / isPublicArtefact := true
ThisBuild / scalacOptions ++= Seq("-feature")
Expand All @@ -17,7 +17,7 @@ lazy val library = Project("library", file("."))

lazy val crypto = Project("crypto", file("crypto"))
.settings(
crossScalaVersions := Seq(scala2_12, scala2_13),
crossScalaVersions := Seq(scala2_13, scala3),
libraryDependencies ++= LibDependencies.cryptoCompile ++ LibDependencies.cryptoTest
)

Expand All @@ -30,7 +30,7 @@ def shareSources(location: String) = Seq(

lazy val cryptoJsonPlay28 = Project("crypto-json-play-28", file("crypto-json-play-28"))
.settings(
crossScalaVersions := Seq(scala2_12, scala2_13),
crossScalaVersions := Seq(scala2_13),
shareSources("crypto-json"),
libraryDependencies ++= LibDependencies.cryptoJsonPlay28Compile ++ LibDependencies.cryptoTest
).dependsOn(crypto)
Expand All @@ -44,7 +44,7 @@ lazy val cryptoJsonPlay29 = Project("crypto-json-play-29", file("crypto-json-pla

lazy val cryptoJsonPlay30 = Project("crypto-json-play-30", file("crypto-json-play-30"))
.settings(
crossScalaVersions := Seq(scala2_13),
crossScalaVersions := Seq(scala2_13, scala3),
shareSources("crypto-json"),
libraryDependencies ++= LibDependencies.cryptoJsonPlay30Compile ++ LibDependencies.cryptoTest
).dependsOn(crypto)
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ object CryptoFormats {
val encryptedValueFormat: Format[EncryptedValue] =
( (__ \ "value").format[String]
~ (__ \ "nonce").format[String]
)(EncryptedValue.apply, unlift(EncryptedValue.unapply))
)(EncryptedValue.apply, ev => (ev.value, ev.nonce))
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ class CryptoFormatsSpec
}

object CryptoFormatsSpec {
implicit val crypto = SymmetricCryptoFactory.aesCrypto("P5xsJ9Nt+quxGZzB4DeLfw==")
implicit val crypto: Encrypter with Decrypter =
SymmetricCryptoFactory.aesCrypto("P5xsJ9Nt+quxGZzB4DeLfw==")

case class SensitiveTestEntity(
normalString : String,
Expand All @@ -94,7 +95,7 @@ object CryptoFormatsSpec {
)

object SensitiveTestEntity {
implicit val formats = {
implicit val formats: Format[SensitiveTestEntity] = {
implicit val sensitiveStringCrypto : Format[SensitiveString] = JsonEncryption.sensitiveEncrypterDecrypter(SensitiveString.apply)
implicit val sensitiveBooleanCrypto : Format[SensitiveBoolean] = JsonEncryption.sensitiveEncrypterDecrypter(SensitiveBoolean.apply)
implicit val sensitiveBigDecimalCrypto: Format[SensitiveBigDecimal] = JsonEncryption.sensitiveEncrypterDecrypter(SensitiveBigDecimal.apply)
Expand All @@ -110,7 +111,8 @@ object CryptoFormatsSpec {
)

object TestForm {
implicit val formats = Json.format[TestForm]
implicit val formats: Format[TestForm] =
Json.format[TestForm]
}

case class SensitiveTestForm(override val decryptedValue: TestForm) extends Sensitive[TestForm]
Expand Down
1 change: 0 additions & 1 deletion crypto/src/main/scala/uk/gov/hmrc/crypto/AesCrypto.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ trait AesCrypto extends Encrypter with Decrypter {
plain match {
case PlainBytes(bytes) => Crypted(encrypter.encrypt(bytes))
case PlainText(text) => Crypted(encrypter.encrypt(text))
case _ => throw new Exception(s"Unable to encrypt unkown message type: $plain")
}

override def decrypt(encrypted: Crypted): PlainText =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ trait AesGCMCrypto extends Encrypter with Decrypter {
plain match {
case PlainBytes(bytes) => Crypted(fromEncryptedValue(crypto.encrypt(bytes , emptyAssociatedData)))
case PlainText(text) => Crypted(fromEncryptedValue(crypto.encrypt(text.getBytes, emptyAssociatedData)))
case _ => throw new RuntimeException(s"Unable to encrypt unknown message type: $plain")
}

private def fromEncryptedValue(ev: EncryptedBytes): String = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ trait CompositeSymmetricCrypto extends Encrypter with Decrypter {
decrypt(d => Try(d.decryptAsBytes(scrambled)))

private def decrypt[T <: PlainContent](tryDecryption: Decrypter => Try[T]): T = {
val decrypterStream = (currentCrypto +: previousCryptos).toStream
val decrypterStream = (currentCrypto +: previousCryptos).to(LazyList)

val message =
decrypterStream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ object SymmetricCryptoFactory {
decrypt(d => Try(d.decryptAsBytes(scrambled)))

private def decrypt[T <: PlainContent](tryDecryption: Decrypter => Try[T]): T = {
val decrypterStream = (currentCrypto +: previousDecrypters).toStream
val decrypterStream = (currentCrypto +: previousDecrypters).to(LazyList)
decrypterStream
.map(tryDecryption)
.collectFirst { case Success(msg) => msg }
Expand Down Expand Up @@ -95,7 +95,7 @@ object SymmetricCryptoFactory {
currentCrypto.encrypt(valueToEncrypt, associatedText)

override def decrypt(valueToDecrypt: EncryptedValue, associatedText: String): String = {
val decrypterStream = (currentCrypto +: previousDecrypters).toStream
val decrypterStream = (currentCrypto +: previousDecrypters).to(LazyList)
decrypterStream
.map(crypter => Try(crypter.decrypt(valueToDecrypt, associatedText)))
.collectFirst { case Success(msg) => msg }
Expand Down
2 changes: 1 addition & 1 deletion crypto/src/main/scala/uk/gov/hmrc/crypto/ValueFinder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package uk.gov.hmrc.crypto

import com.typesafe.config.Config
import collection.JavaConverters._
import scala.jdk.CollectionConverters._

import scala.util.Try

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import com.typesafe.config.ConfigFactory
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike

import collection.JavaConverters._
import scala.jdk.CollectionConverters._

class ApplicationCryptoSpec extends AnyWordSpecLike with Matchers {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
package uk.gov.hmrc.crypto

import com.typesafe.config.ConfigFactory
import org.mockito.MockitoSugar
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike
import org.scalatestplus.mockito.MockitoSugar

import java.util.Base64
import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._

@annotation.nowarn("msg=deprecated")
class CompositeOneWayCryptoSpec extends AnyWordSpecLike with Matchers with MockitoSugar {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
package uk.gov.hmrc.crypto

import com.typesafe.config.ConfigFactory
import org.mockito.MockitoSugar
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike
import org.scalatestplus.mockito.MockitoSugar

import java.security.SecureRandom
import java.util.Base64
import collection.JavaConverters._
import scala.jdk.CollectionConverters._

@annotation.nowarn("msg=deprecated")
class CryptoGCMWithKeysFromConfigSpec extends AnyWordSpecLike with Matchers with MockitoSugar {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
package uk.gov.hmrc.crypto

import com.typesafe.config.ConfigFactory
import org.mockito.MockitoSugar
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike
import org.scalatestplus.mockito.MockitoSugar

import java.util.Base64
import collection.JavaConverters._
import scala.jdk.CollectionConverters._

@annotation.nowarn("msg=deprecated")
class CryptoWithKeysFromConfigSpec extends AnyWordSpecLike with Matchers with MockitoSugar {
Expand Down
8 changes: 4 additions & 4 deletions project/LibDependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ object LibDependencies {
"org.scalatest" %% "scalatest" % "3.2.17" % Test,
"com.vladsch.flexmark" % "flexmark-all" % "0.64.8" % Test,
"org.scalatestplus" %% "scalacheck-1-17" % "3.2.17.0" % Test,
"org.mockito" %% "mockito-scala" % "1.17.29" % Test
"org.scalatestplus" %% "mockito-3-4" % "3.2.10.0" % Test
)

val cryptoJsonPlay28Compile = Seq(
"com.typesafe.play" %% "play-json" % "2.8.1" // version provided by Play 2.8
"com.typesafe.play" %% "play-json" % "2.8.2" // version provided by Play 2.8
)

val cryptoJsonPlay29Compile = Seq(
"com.typesafe.play" %% "play-json" % "2.10.2" // version provided by Play 2.9
"com.typesafe.play" %% "play-json" % "2.10.5" // version provided by Play 2.9
)

val cryptoJsonPlay30Compile = Seq(
"org.playframework" %% "play-json" % "3.0.0" // version provided by Play 3.0
"org.playframework" %% "play-json" % "3.0.3" // version provided by Play 3.0
)
}
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.9.7
sbt.version=1.9.9
2 changes: 1 addition & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
resolvers += MavenRepository("HMRC-open-artefacts-maven2", "https://open.artefacts.tax.service.gov.uk/maven2")
resolvers += Resolver.url("HMRC-open-artefacts-ivy2", url("https://open.artefacts.tax.service.gov.uk/ivy2"))(Resolver.ivyStylePatterns)

addSbtPlugin("uk.gov.hmrc" % "sbt-auto-build" % "3.15.0")
addSbtPlugin("uk.gov.hmrc" % "sbt-auto-build" % "3.21.0")

0 comments on commit 7e08bfe

Please sign in to comment.