Package io.axoniq.dataprotection.cryptoengine

## Class DatabaseBackedCryptoEngine

- java.lang.Object
- - io.axoniq.dataprotection.cryptoengine.DatabaseBackedCryptoEngine

- All Implemented Interfaces:
    `CryptoEngine`
  Direct Known Subclasses:
    `InMemoryCryptoEngine`, `JdbcCryptoEngine`, `JpaCryptoEngine`, `VaultCryptoEngine`

      public abstract class DatabaseBackedCryptoEngine
      extends Object
      implements CryptoEngine

  Partial implementation of `CryptoEngine` which operates using the JVM's standard `Provider` to perform encryption and decryption, while assuming some separate storage facility for keys. This could a SQL or NoSQL database, or a simple in memory structure for testing purposes.

  The core cryptographic functions of generating the `SecretKey` and instantiating the `Cipher` are implemented by this class. Implementations are responsible for providing some mechanism to store and retrieve the keys. The `SecretKey` instances being handled in this class are always of the subclass `SecretKeySpec` which means that it is possible to access their byte encoding. Type key type is currently always AES en the length is always 256 bits, but this may change in future versions and implementations may already take this into account.

- - ### Constructor Summary

    Constructors
    Constructor | Description
    --- | ---
    `DatabaseBackedCryptoEngine()` |

  - ### Method Summary

    All Methods Instance Methods Abstract Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `Cipher` | `createCipher()` | Creates an uninitialized `Cipher` instance for the correct transformation (AES, CBC, PKCS#5) and provider.
    `Cipher` | `createDigestCipher()` | Creates an uninitialized `Cipher` instance for calculating the encrypted digest.
    `KeyType` | `getKeyType()` | Retrieves the currently used `KeyType` for new keys
    `SecretKey` | `getOrCreateKey​(String id)` | Retrieves the key for a given id.
    `protected abstract SecretKey` | `putKeyIfAbsent​(String id, SecretKeySpec secretKeySpec)` | Stores the key for the given id, if no key is currently registered for this id.
    `void` | `setKeyType​(KeyType keyType)` | Sets the `KeyType` to use, which determines the length of newly generated keys.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

    - ### Methods inherited from interface io.axoniq.dataprotection.cryptoengine.CryptoEngine

      `deleteKey, getKey`

- - ### Constructor Detail

    - #### DatabaseBackedCryptoEngine

          public DatabaseBackedCryptoEngine()

  - ### Method Detail

    - #### getOrCreateKey

          public SecretKey getOrCreateKey​(String id)

      Description copied from interface: `CryptoEngine`
      Retrieves the key for a given id. If no such key is registered, generates a new random key and stores it under the alias before returning it.
      Specified by:
        `getOrCreateKey` in interface `CryptoEngine`
      Parameters:
        `id` - the id of the `SecretKey`
      Returns:
        the potentially new `SecretKey` associated with the id

    - #### putKeyIfAbsent

          protected abstract SecretKey putKeyIfAbsent​(String id,
                                                      SecretKeySpec secretKeySpec)

      Stores the key for the given id, if no key is currently registered for this id. Returns the new key belonging to id, which is either the key that was already registered or the key provided as the 2nd argument if no key was registered yet. (Please note that this is different behaviour from `ConcurrentHashMap.putIfAbsent(Object, Object)`, which always returns the prior value belonging to key.)
      Parameters:
        `id` - the id for which to store/retrieve the secret key
        `secretKeySpec` - the `SecretKeySpec` to store if none has been stored yet for id
      Returns:
        the effective `SecretKeySpec` for id

    - #### createCipher

          public Cipher createCipher()

      Description copied from interface: `CryptoEngine`
      Creates an uninitialized `Cipher` instance for the correct transformation (AES, CBC, PKCS#5) and provider. Clients should still call the `Cipher.init(int, java.security.Key, AlgorithmParameterSpec)` to specify operation mode (encryption or decryption), key and initialization vector.
      Specified by:
        `createCipher` in interface `CryptoEngine`
      Returns:
        the `Cipher`

    - #### createDigestCipher

          public Cipher createDigestCipher()

      Description copied from interface: `CryptoEngine`
      Creates an uninitialized `Cipher` instance for calculating the encrypted digest. For this specific purpose, it should use AES, EBC and no padding, and the same provider as for the other operations. Clients should still call the `Cipher.init(int, java.security.Key)` to specify operation mode (always encryption) and key.
      Specified by:
        `createDigestCipher` in interface `CryptoEngine`
      Returns:
        the `Cipher`

    - #### setKeyType

          public void setKeyType​(KeyType keyType)

      Description copied from interface: `CryptoEngine`
      Sets the `KeyType` to use, which determines the length of newly generated keys. Defaults to `KeyType.AES_256` if not set.
      Specified by:
        `setKeyType` in interface `CryptoEngine`
      Parameters:
        `keyType` - the new `KeyType`

    - #### getKeyType

          public KeyType getKeyType()

      Description copied from interface: `CryptoEngine`
      Retrieves the currently used `KeyType` for new keys
      Specified by:
        `getKeyType` in interface `CryptoEngine`
      Returns:
        the current value