Advanced Attribute Encryption
The attribute encryption strategy defines how secure attributes get encrypted before being persisted in the data store. If you would like to customize the encryption or key retrieval strategy, you can do so by extending the relevant interfaces in the Data Hub SDK.
The default AttributeEncryptionStrategy uses AES/ECB/PKCS5Padding symmetric key encryption for encryption and decryption of secure attributes. The class is called com.hybris.datahub.security.impl.AES128AttributeEncryptionStrategy. The default strategy uses a 128-bit AES key, which can be retrieved using the DefaultEncryptionKeyRetrievalStrategy detailed in the following content.
import com.hybris.datahub.security.AttributeEncryptionStrategy;
public class MockedEnabledAttributeEncryptionStrategy implements AttributeEncryptionStrategy
{
/**
* This is an example of how to mock the encryption strategy. It mocks the encryption process
* by returning the same string it receives. It always returns true for isEnabled().
*/
@Override
public String encrypt(final String s) throws Exception
{
return s;
}
@Override
public String decrypt(final String c) throws Exception
{
return c;
}
@Override
public boolean isEnabled()
{
return true;
}
}
Encryption Key Retrieval Strategy
The default implementation of the encryption key retrieval strategy is called com.hybris.datahub.security.impl.DefaultEncryptionKeyRetrievalStrategy. It retrieves a key from a file in the /opt/datahub/config/. Specify the path to the file that contains the key by setting the datahub.encryption.key.path property in a local.properties file. This step is necessary to load any extensions that contain secure attributes or TargetSystem passwords when using the DefaultEncryptionKeyRetrievalStrategy. An example of how to generate an AES 128-bit key is provided in the tutorial that follows, Tutorial: Using Attribute Encryption.
import com.hybris.datahub.security.EncryptionKeyRetrievalStrategy;
import java.security.KeyStoreException;
import java.security.KeyStore;
public class MyCustomEncryptionKeyRetrievalStrategy implements EncryptionKeyRetrievalStrategy<PrivateKey>
{
/**
* Loads an encryption key from Mac OS X's Keychain
*/
@Override
public PrivateKey getKey() throws KeyStoreException
{
KeyStore keyStore = KeyStore.getInstance("KeychainStore", "Apple");
KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry) keyStore.getEntry("privateKey", getPassword());
return privateKeyEntry.getPrivateKey();
}
private char[] getPassword()
{
// retrieve password
}
}
Injecting Your Custom Beans
<alias name="myCustomAttributeEncryptionStrategy" alias="attributeEncryptionStrategy" /> <bean id="attributeEncryptionStrategy" class="com.hybris.datahub.security.impl.MockedEnabledAttributeEncryptionStrategy"> <property name="keyRetrievalStrategy" ref="keyRetrievalStrategy" /> </bean> <alias name="myCustomKeyRetrievalStrategy" alias="keyRetrievalStrategy" /> <bean id="keyRetrievalStrategy" class="com.hybris.datahub.security.impl.MyCustomEncryptionKeyRetrievalStrategy" />