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 ofCryptoEnginewhich operates using the JVM's standardProviderto 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
SecretKeyand instantiating theCipherare implemented by this class. Implementations are responsible for providing some mechanism to store and retrieve the keys. TheSecretKeyinstances being handled in this class are always of the subclassSecretKeySpecwhich 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 CiphercreateCipher()Creates an uninitializedCipherinstance for the correct transformation (AES, CBC, PKCS#5) and provider.CiphercreateDigestCipher()Creates an uninitializedCipherinstance for calculating the encrypted digest.KeyTypegetKeyType()Retrieves the currently usedKeyTypefor new keysSecretKeygetOrCreateKey(String id)Retrieves the key for a given id.protected abstract SecretKeyputKeyIfAbsent(String id, SecretKeySpec secretKeySpec)Stores the key for the given id, if no key is currently registered for this id.voidsetKeyType(KeyType keyType)Sets theKeyTypeto 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
-
-
-
-
Method Detail
-
getOrCreateKey
public SecretKey getOrCreateKey(String id)
Description copied from interface:CryptoEngineRetrieves 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:
getOrCreateKeyin interfaceCryptoEngine- Parameters:
id- the id of theSecretKey- Returns:
- the potentially new
SecretKeyassociated 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 fromConcurrentHashMap.putIfAbsent(Object, Object), which always returns the prior value belonging to key.)- Parameters:
id- the id for which to store/retrieve the secret keysecretKeySpec- theSecretKeySpecto store if none has been stored yet for id- Returns:
- the effective
SecretKeySpecfor id
-
createCipher
public Cipher createCipher()
Description copied from interface:CryptoEngineCreates an uninitializedCipherinstance for the correct transformation (AES, CBC, PKCS#5) and provider. Clients should still call theCipher.init(int, java.security.Key, AlgorithmParameterSpec)to specify operation mode (encryption or decryption), key and initialization vector.- Specified by:
createCipherin interfaceCryptoEngine- Returns:
- the
Cipher
-
createDigestCipher
public Cipher createDigestCipher()
Description copied from interface:CryptoEngineCreates an uninitializedCipherinstance 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 theCipher.init(int, java.security.Key)to specify operation mode (always encryption) and key.- Specified by:
createDigestCipherin interfaceCryptoEngine- Returns:
- the
Cipher
-
setKeyType
public void setKeyType(KeyType keyType)
Description copied from interface:CryptoEngineSets theKeyTypeto use, which determines the length of newly generated keys. Defaults toKeyType.AES_256if not set.- Specified by:
setKeyTypein interfaceCryptoEngine- Parameters:
keyType- the newKeyType
-
getKeyType
public KeyType getKeyType()
Description copied from interface:CryptoEngineRetrieves the currently usedKeyTypefor new keys- Specified by:
getKeyTypein interfaceCryptoEngine- Returns:
- the current value
-
-