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.

If you prefer to use a custom encryption strategy, the com.hybris.datahub.security.AttributeEncryptionStrategy interface can be extended to implement a custom encryption strategy of your choice. For example, the following code shows how you would extend the AttributeEncryptionStrategy to create an encryption strategy that would mock the actual encryption process:
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.

If you would like to retrieve the secret key in a different way, you can extend the com.hybris.datahub.security.EncryptionKeyRetrievalStrategy interface to create a custom key retrieval implementation. One example would be to use Mac OS X's Keychain as shown in the following:
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

If you wish to inject your custom attribute encryption strategy and key retrieval strategy beans, you do these tasks in your Spring extension XML file. The Spring extension XML file is loaded with the application context. For example:
<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" />