A micro-library for all Crypto related infrastructure.
In the example below 'cookie.encryption' is the baseConfigKey that is specified when the crypto utility is created. The 'previousKeys' element is optional.
# Base 64 encoded MD5 hash of application.secret
cookie.encryption {
key="gvBoGdgzqG1AarzF1LY0zQ=="
previousKeys=["AwMDAwMDAwMDAwMDAwMDAw==","BAQEBAQEBAQEBAQEBAQEBA=="]
}
Add the following to your SBT build:
resolvers += MavenRepository("HMRC-open-artefacts-maven2", "https://open.artefacts.tax.service.gov.uk/maven2")
libraryDependencies += "uk.gov.hmrc" %% "crypto" % "[INSERT-VERSION]"Represented by the model Encrypter and Decrypter. Or AdEncrypter AdDecrypter for the associated data variant.
The different variants are provided by SymmetricCryptoFactory.
The supported types are:
-
AesCryptoAn implementation of "AES" Cipher.
It represents the encypted data as
Crypted, which contains a single base64 encoded String.Note, it is recommended to use
AesGCMCryptoinstead which uses a nonce to prevent repeatable encryptions.To create, either call
SymmetricCryptoFactory.aesCryptowith the secret key, orSymmetricCryptoFactory.aesCryptoFromConfigto look up the keys from config.SymmetricCryptoFactory.aesCryptoFromConfigadditionally supports decrypting with any available previous keys, to support key rotation. -
AesGCMCryptoSimilar to AesCrypto, but uses the GCM algorithm. This includes the use of a nonce, to prevent repeatable encryptions. Note, the associated data is always set to an empty array. Use
AesGcmAdCryptoif setting the associated data is required.It represents the encypted data as
Crypted, which contains a single base64 encoded String.To create, either call
SymmetricCryptoFactory.aesGcmCryptowith the secret key, orSymmetricCryptoFactory.aesGcmCryptoFromConfigto look up the keys from config.SymmetricCryptoFactory.aesGcmCryptoFromConfigadditionally supports decrypting with any available previous keys, to support key rotation. -
AesGcmAdCryptoIt is similar to
AesGCMCrypto, but it additionally takes some associated data when encrypting and decrypting. This can be used to prevent copying encrypted data to another context.It is a replacement to
SecreGCMCipherthat was previously included in many clients; and to simplify migration, it represents the encrypted data withEncryptedValuerather thanCrypted.Note, if you are migrating from
SecureGCMCipher, you will provide the key (and any previous keys) to the construction ofAesGcmAdCryptoand not to each call toencrypt/decrypt. You will also need to importCryptoFormats.encryptedValueFormatfromcrypto-json.To create, either call
SymmetricCryptoFactory.aesGcmAdCryptowith the secret key, orSymmetricCryptoFactory.aesGcmAdCryptoFromConfigto look up the keys from config.SymmetricCryptoFactory.aesGcmAdCryptoFromConfigadditionally supports decrypting with any available previous keys, to support key rotation.
See java docs for more details.
Represented by the model Hasher and Verifier.
The supported variants are provided by OnewayCryptoFactory.
This model identifies data which should be encrypted. It can be used in conjunction with Crypto Json to encrypt in JSON for storing in database or sending over the wire. It also overrides toString to suppress logging.
It is recommended to use Sensitive rather than Protected as provided by json-encyption since the parameterised type is not erased, which can be useful with looking up a mongo codec in runtime for example.
Provides Play json formats which encrypt the Sensitive type. See Sensitive.
This replaces the json-encryption library.
resolvers += MavenRepository("HMRC-open-artefacts-maven2", "https://open.artefacts.tax.service.gov.uk/maven2")
libraryDependencies += "uk.gov.hmrc" %% "crypto-json-play-xx" % "[INSERT-VERSION]"Where play-xx is your version of Play (e.g. play-28).
You can use a provided Sensitive implementation and encrypter/decrypter if available. E.g. for String
val value: String = ...
val encryptor = new JsonEncryptor[String]()
val encryptedValue: JsValue = encryptor.writes(Protected[String](value))becomes
val value: String = ...
val encrypter = JsonEncryption.sensitiveEncrypter[String, SensitiveString]
val encryptedValue: JsValue = encrypter.writes(SensitiveString(value))and
val encryptedValue: JsValue = ...
val decryptor = new JsonDecryptor[String]()
val optValue: Option[String] = decryptor.reads(encryptedValue).asOpt.map(_.decryptedValue)becomes
val encryptedValue: JsValue = ...
val decrypter = JsonEncryption.sensitiveDecrypter(SensitiveString.apply)
val optValue: Option[String] = decrypter.reads(encryptedValue).asOpt.map(_.decryptedValue)If there isn't a Sensitive implementation provided for your required type, you can create one.
For a generic type, the type variable can be added on your Sensitive implementation (but in this example, T will be erased - but this is the same as the deprecated Protected). E.g. using
case class SensitiveT[T](override val decryptedValue: T) extends Sensitive[T]val value: T = ...
val encryptor = new JsonEncryptor[T]()
val encryptedValue: JsValue = encryptor.writes(Protected[T](value))becomes
val value: T = ...
val encrypter = JsonEncryption.sensitiveEncrypter[T, SensitiveT[T]]
val encryptedValue: JsValue = encrypter.writes(SensitiveT(value))and
val encryptedValue: JsValue = ...
val decryptor = new JsonDecryptor[T]()
val optValue: Option[T] = decryptor.reads(encryptedValue).asOpt.map(_.decryptedValue)becomes
val encryptedValue: JsValue = ...
val decrypter = JsonEncryption.sensitiveDecrypter[T, SensitiveT[T]](SensitiveT.apply)
val optValue: Option[T] = decrypter.reads(encryptedValue).asOpt.map(_.decryptedValue)Contains utilities for working with associated data.
AdCryptoUtils.encryptWith will wrap a given json Format with AD encryption. It requires a pointer (JSPath) to the associated data field and takes a list of pointers to the fields to encrypt.
See spec for an example.
Removes support for Play 2.9
ApplicationCryptois deprecated - frontends can use the one provided bybootstrap-frontend-playinstead.JsonCryptois deprecated. The intention of the keyjson.encryption.keywas ambiguous. Clients should manage their own keys and cryptos. Usemongodb.encryption.keyfor a service's own mongo encryption.
Built for Scala 3 and Scala 2.13 - drops Scala 2.12.
Adds support for Play 2.9 and Play 3.0
- The
securelibrary has been rolled intocrypto. The package has changed fromhmrc.gov.uk.securetohmrc.gov.uk.crypto.secure. - The artefact
crypto-json-play-xxhas been added to replace thejson-encryptionlibrary. It providesSensitiverather thanProvidedto avoid erasure and doesn't leak the value intoString. AesGcmAdCryptohas been added. It is different fromAesGCMCryptoin that it supports associated data to be provided on each encrypt/decrypt. This should replace customSecureGCMCipher.SymmetricCryptoFactoryhas been added to make finding/using symetric cryptos easier.CompositeSymmetricCryptohas been deprecated. To compose cryptos, clients should useSymmetricCryptoFactory.composeCrypto. Clients should not use theCompositeSymmetricCryptoabstraction, which is implementation details of the composition of previous decrypters. Instead, they should useEncrypter with Decrypter.
This code is open source software licensed under the Apache 2.0 License