Class HibernatePBEStringEncryptor
Placeholder class for PBEStringEncryptor objects which are eligible for use from Hibernate.
This class acts as a wrapper on a PBEStringEncryptor, allowing
to be set a registered name (see setRegisteredName(String))
and performing the needed registry operations against the
HibernatePBEEncryptorRegistry.
It is not mandatory that a PBEStringEncryptor be explicitly set
with setEncryptor(PBEStringEncryptor). If not, a
StandardPBEStringEncryptor object will be created internally
and it will be configurable with the
setPassword(String)/setPasswordCharArray(char[]),
setAlgorithm(String), setKeyObtentionIterations(int),
setSaltGenerator(SaltGenerator), setIvGenerator(IvGenerator),
setProviderName(String),
setProvider(Provider), setStringOutputType(String)
and setConfig(PBEConfig) methods.
This class is mainly intended for use from Spring Framework or some other
IoC container (if you are not using a container of this kind, please see
HibernatePBEEncryptorRegistry). The steps to be performed are
the following:
- Create an object of this class (declaring it).
- Set its registeredName and, either its wrapped encryptor or its password, algorithm, keyObtentionIterations, saltGenerator, ivGenerator and config properties.
- Declare a typedef in a Hibernate mapping giving its encryptorRegisteredName parameter the same value specified to this object in registeredName.
This in a Spring config file would look like:
...
<-- Optional, as the hibernateEncryptor could be directly set an -->
<-- algorithm and password. -->
<bean id="stringEncryptor"
class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
<property name="algorithm">
<value>PBEWithMD5AndDES</value>
</property>
<property name="password">
<value>XXXXX</value>
</property>
</bean>
<bean id="hibernateEncryptor"
class="org.jasypt.hibernate.encryptor.HibernatePBEStringEncryptor">
<property name="registeredName">
<value>myHibernateStringEncryptor</value>
</property>
<property name="encryptor">
<ref bean="stringEncryptor" />
</property>
</bean>
...
And then in the Hibernate mapping file:
<typedef name="encrypted" class="org.jasypt.hibernate.type.EncryptedStringType">
<param name="encryptorRegisteredName">myHibernateStringEncryptor</param>
</typedef>
An important thing to note is that, when using HibernatePBEStringEncryptor
objects this way to wrap PBEStringEncryptors, it is not
necessary to deal with HibernatePBEEncryptorRegistry,
because HibernatePBEStringEncryptor objects get automatically registered
in the encryptor registry when their setRegisteredName(String)
method is called.
- Since:
- 1.9.0
- Author:
- Chus Picos
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new instance of HibernatePBEStringEncryptor. -
Method Summary
Modifier and TypeMethodDescriptionDecypts a message, delegating to wrapped encryptorEncrypts a message, delegating to wrapped encryptor.org.jasypt.encryption.pbe.PBEStringEncryptorReturns the encryptor which this object wraps.Returns the name with which the wrapped encryptor is registered at the registry.voidsetAlgorithm(String algorithm) Sets the algorithm to be used by the internal encryptor, if a specific encryptor has not been set with setEncryptor(...).voidsetConfig(org.jasypt.encryption.pbe.config.PBEConfig config) Sets the PBEConfig to be used by the internal encryptor, if a specific encryptor has not been set with setEncryptor(...).voidsetEncryptor(org.jasypt.encryption.pbe.PBEStringEncryptor encryptor) Sets the PBEStringEncryptor to be held (wrapped) by this object.voidsetIvGenerator(org.jasypt.iv.IvGenerator ivGenerator) Sets the IV generator to be used by the internal encryptor, if a specific encryptor has not been set with setEncryptor(...).voidsetKeyObtentionIterations(int keyObtentionIterations) Sets the key obtention iterations to be used by the internal encryptor, if a specific encryptor has not been set with setEncryptor(...).voidsetPassword(String password) Sets the password to be used by the internal encryptor, if a specific encryptor has not been set with setEncryptor(...).voidsetPasswordCharArray(char[] password) Sets the password to be used by the internal encryptor (as a char[]), if a specific encryptor has not been set with setEncryptor(...).voidsetProvider(Provider provider) Sets the JCE provider to be used by the internal encryptor, if a specific encryptor has not been set with setEncryptor(...).voidsetProviderName(String providerName) Sets the name of the JCE provider to be used by the internal encryptor, if a specific encryptor has not been set with setEncryptor(...).voidsetRegisteredName(String registeredName) Sets the registered name of the encryptor and adds it to the registry.voidsetSaltGenerator(org.jasypt.salt.SaltGenerator saltGenerator) Sets the salt generator to be used by the internal encryptor, if a specific encryptor has not been set with setEncryptor(...).voidsetStringOutputType(String stringOutputType) Sets the type of String output ("base64" (default), "hexadecimal") to be used by the internal encryptor, if a specific encryptor has not been set with setEncryptor(...).
-
Constructor Details
-
HibernatePBEStringEncryptor
public HibernatePBEStringEncryptor()Creates a new instance of HibernatePBEStringEncryptor. It also creates a StandardPBEStringEncryptor for internal use, which can be overriden by calling setEncryptor(...).
-
-
Method Details
-
getEncryptor
public org.jasypt.encryption.pbe.PBEStringEncryptor getEncryptor()Returns the encryptor which this object wraps.- Returns:
- the encryptor.
-
setEncryptor
public void setEncryptor(org.jasypt.encryption.pbe.PBEStringEncryptor encryptor) Sets the PBEStringEncryptor to be held (wrapped) by this object. This method is optional and can be only called once.- Parameters:
encryptor- the encryptor.
-
setPassword
Sets the password to be used by the internal encryptor, if a specific encryptor has not been set with setEncryptor(...).- Parameters:
password- the password to be set for the internal encryptor
-
setPasswordCharArray
public void setPasswordCharArray(char[] password) Sets the password to be used by the internal encryptor (as a char[]), if a specific encryptor has not been set with setEncryptor(...).- Parameters:
password- the password to be set for the internal encryptor- Since:
- 1.8
-
setAlgorithm
Sets the algorithm to be used by the internal encryptor, if a specific encryptor has not been set with setEncryptor(...).- Parameters:
algorithm- the algorithm to be set for the internal encryptor
-
setKeyObtentionIterations
public void setKeyObtentionIterations(int keyObtentionIterations) Sets the key obtention iterations to be used by the internal encryptor, if a specific encryptor has not been set with setEncryptor(...).- Parameters:
keyObtentionIterations- to be set for the internal encryptor
-
setSaltGenerator
public void setSaltGenerator(org.jasypt.salt.SaltGenerator saltGenerator) Sets the salt generator to be used by the internal encryptor, if a specific encryptor has not been set with setEncryptor(...).- Parameters:
saltGenerator- the salt generator to be set for the internal encryptor.
-
setIvGenerator
public void setIvGenerator(org.jasypt.iv.IvGenerator ivGenerator) Sets the IV generator to be used by the internal encryptor, if a specific encryptor has not been set with setEncryptor(...).- Parameters:
ivGenerator- the IV generator to be set for the internal encryptor.
-
setProviderName
Sets the name of the JCE provider to be used by the internal encryptor, if a specific encryptor has not been set with setEncryptor(...).- Parameters:
providerName- the name of the JCE provider (already registered)- Since:
- 1.3
-
setProvider
Sets the JCE provider to be used by the internal encryptor, if a specific encryptor has not been set with setEncryptor(...).- Parameters:
provider- the JCE provider to be used- Since:
- 1.3
-
setStringOutputType
Sets the type of String output ("base64" (default), "hexadecimal") to be used by the internal encryptor, if a specific encryptor has not been set with setEncryptor(...).- Parameters:
stringOutputType- the type of String output- Since:
- 1.3
-
setConfig
public void setConfig(org.jasypt.encryption.pbe.config.PBEConfig config) Sets the PBEConfig to be used by the internal encryptor, if a specific encryptor has not been set with setEncryptor(...).- Parameters:
config- the PBEConfig to be set for the internal encryptor
-
encrypt
Encrypts a message, delegating to wrapped encryptor.- Parameters:
message- the message to be encrypted.- Returns:
- the encryption result.
-
decrypt
Decypts a message, delegating to wrapped encryptor- Parameters:
encryptedMessage- the message to be decrypted.- Returns:
- the result of decryption.
-
setRegisteredName
Sets the registered name of the encryptor and adds it to the registry.- Parameters:
registeredName- the name with which the encryptor will be registered.
-
getRegisteredName
Returns the name with which the wrapped encryptor is registered at the registry.- Returns:
- the registered name.
-