public class DataSealer extends AbstractInitializableComponent
| Modifier and Type | Field and Description |
|---|---|
private SecretKey |
cipherKey
Default key used for encryption.
|
private String |
cipherKeyAlias
Keystore alias for the default encryption key.
|
private String |
cipherKeyPassword
Password for encryption key(s).
|
private String |
keystorePassword
Password for keystore.
|
private Resource |
keystoreResource
Keystore resource.
|
private String |
keystoreType
Type of keystore to use for access to keys.
|
private Logger |
log
Class logger.
|
private SecureRandom |
random
Source of secure random data.
|
| Constructor and Description |
|---|
DataSealer()
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
void |
doInitialize()
Performs the initialization of the component.
|
private String |
extractAndCheckDecryptedData(byte[] decryptedBytes)
Extract the GZIP'd data and test for expiration before returning it.
|
SecretKey |
getCipherKey()
Returns the encryption key.
|
String |
getCipherKeyAlias()
Returns the encryption key alias.
|
String |
getCipherKeyPassword()
Returns the encryption key password.
|
String |
getKeystorePassword()
Returns the keystore password.
|
Resource |
getKeystoreResource()
Returns the keystore resource.
|
String |
getKeystoreType()
Returns the keystore type.
|
SecureRandom |
getRandom()
Returns the pseudorandom generator.
|
private SecretKey |
loadKey(String alias)
Load a particular key from the keystore designated by the bean's properties.
|
void |
setCipherKeyAlias(String alias)
Sets the default encryption key alias.
|
void |
setCipherKeyPassword(String password)
Sets the encryption key password.
|
void |
setKeystorePassword(String password)
Sets the keystore password.
|
void |
setKeystoreResource(Resource resource)
Sets the keystore resource.
|
void |
setKeystoreType(String type)
Sets the keystore type.
|
void |
setRandom(SecureRandom r)
Sets the pseudorandom generator.
|
private void |
testEncryption()
Run a test over the configured bean properties.
|
String |
unwrap(String wrapped)
Decrypts and verifies an encrypted bundle created with
wrap(String, long). |
String |
wrap(String data,
long exp)
Encodes data into an AEAD-encrypted blob, gzip(exp|data)
exp = expiration time of the data; 8 bytes; Big-endian
data = the data; a UTF-8-encoded string
|
destroy, doDestroy, initialize, isDestroyed, isInitialized@NonnullAfterInit private SecretKey cipherKey
@NonnullAfterInit private SecureRandom random
@NonnullAfterInit private String keystoreType
@NonnullAfterInit private Resource keystoreResource
@NonnullAfterInit private String keystorePassword
@NonnullAfterInit private String cipherKeyAlias
@NonnullAfterInit private String cipherKeyPassword
public void doInitialize()
throws ComponentInitializationException
doInitialize in class AbstractInitializableComponentComponentInitializationException - thrown if there is a problem initializing the component@NonnullAfterInit public SecretKey getCipherKey()
@NonnullAfterInit public SecureRandom getRandom()
@NonnullAfterInit public String getKeystoreType()
@NonnullAfterInit public Resource getKeystoreResource()
@NonnullAfterInit public String getKeystorePassword()
@NonnullAfterInit public String getCipherKeyAlias()
@NonnullAfterInit public String getCipherKeyPassword()
public void setRandom(@Nonnull SecureRandom r)
r - the pseudorandom generator to setpublic void setKeystoreType(@Nonnull@NotEmpty String type)
type - the keystore type to setpublic void setKeystoreResource(@Nonnull@NotEmpty Resource resource)
resource - the keystore resource to setpublic void setKeystorePassword(@Nonnull@NotEmpty String password)
password - the keystore password to setpublic void setCipherKeyAlias(@Nonnull@NotEmpty String alias)
alias - the encryption key alias to setpublic void setCipherKeyPassword(@Nonnull@NotEmpty String password)
password - the encryption key password to set@Nonnull public String unwrap(@Nonnull@NotEmpty String wrapped) throws DataSealerException
wrap(String, long).
If the data indicates that a non-default key was used, an attempt to load this key from the keystore will be made.
wrapped - the encoded blobDataSealerException - if the data cannot be unwrapped and verified@Nonnull private String extractAndCheckDecryptedData(@Nonnull@NotEmpty byte[] decryptedBytes) throws DataSealerException
decryptedBytes - the data we are looking atDataSealerException - if the data cannot be unwrapped and verified@Nonnull public String wrap(@Nonnull@NotEmpty String data, long exp) throws DataSealerException
As part of encryption, the key alias is supplied as additional authenticated data to the cipher. Afterwards, the encrypted data is prepended by the IV and then again by the alias (in length-prefixed UTF-8 format), which identifies the key used. Finally the result is base64-encoded.
data - the data to wrapexp - expiration timeDataSealerException - if the wrapping operation failsprivate void testEncryption()
throws DataSealerException
DataSealerException - if the test fails@Nonnull private SecretKey loadKey(@Nonnull@NotEmpty String alias) throws GeneralSecurityException, IOException
alias - alias of the key to loadGeneralSecurityException - if the load fails due to a security-related issueIOException - if the load process failsCopyright © 1999–2014. All rights reserved.