Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * =============================================================================
   * 
   *   Copyright (c) 2007, The JASYPT team (http://www.jasypt.org)
   * 
   *   Licensed under the Apache License, Version 2.0 (the "License");
   *   you may not use this file except in compliance with the License.
   *   You may obtain a copy of the License at
   * 
  *       http://www.apache.org/licenses/LICENSE-2.0
  * 
  *   Unless required by applicable law or agreed to in writing, software
  *   distributed under the License is distributed on an "AS IS" BASIS,
  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *   See the License for the specific language governing permissions and
  *   limitations under the License.
  * 
  * =============================================================================
  */
 package org.jasypt.hibernate.encryptor;
 

Placeholder class for PBEByteEncryptor objects which are eligible for use from Hibernate.

This class acts as a wrapper on a PBEByteEncryptor, allowing to be set a registered name (see setRegisteredName(java.lang.String)) and performing the needed registry operations against the HibernatePBEEncryptorRegistry.

It is not mandatory that a PBEByteEncryptor be explicitly set with setEncryptor(org.jasypt.encryption.pbe.PBEByteEncryptor). If not, a StandardPBEByteEncryptor object will be created internally and it will be configurable with the setPassword(java.lang.String), setAlgorithm(java.lang.String), setKeyObtentionIterations(int), setSaltGenerator(org.jasypt.salt.SaltGenerator) and setConfig(org.jasypt.encryption.pbe.config.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:

  1. Create an object of this class (declaring it).
  2. Set its registeredName and, either its wrapped encryptor or its password, algorithm, keyObtentionIterations, saltGenerator and config properties.
  3. 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="byteEncryptor"
    class="org.jasypt.encryption.pbe.StandardPBEByteEncryptor">
    <property name="algorithm">
        <value>PBEWithMD5AndDES</value>
    </property>
    <property name="password">
        <value>XXXXX</value>
    </property>
  </bean>
  
  <bean id="hibernateEncryptor"
    class="org.jasypt.hibernate.encryptor.HibernatePBEByteEncryptor">
    <property name="registeredName">
        <value>myHibernateByteEncryptor</value>
    </property>
    <property name="encryptor">
        <ref bean="byteEncryptor" />
    </property>
  </bean>
  ...
 

And then in the Hibernate mapping file:

    <typedef name="encrypted" class="org.jasypt.hibernate.type.EncryptedBinaryType">
      <param name="encryptorRegisteredName">myHibernateByteEncryptor</param>
    </typedef>
 

An important thing to note is that, when using HibernatePBEByteEncryptor objects this way to wrap PBEByteEncryptors, it is not necessary to deal with HibernatePBEEncryptorRegistry, because HibernatePBEByteEncryptor objects get automatically registered in the encryptor registry when their setRegisteredName(java.lang.String) method is called.

Author(s):
Daniel Fernández Garrido
Since:
1.2
public final class HibernatePBEByteEncryptor {
    private String registeredName = null;
    private PBEByteEncryptor encryptor = null;
    private boolean encryptorSet = false;
    
    
    
    
Creates a new instance of HibernatePBEByteEncryptor It also creates a StandardPBEByteEncryptor for internal use, which can be overriden by calling setEncryptor(...).
    public HibernatePBEByteEncryptor() {
        super();
        this. = new StandardPBEByteEncryptor();
        this. = false;
    }
    /*
     * For internal use only, by the Registry, when a PBEByteEncryptor
     * is registered programmatically.
     */
    HibernatePBEByteEncryptor(String registeredName
            PBEByteEncryptor encryptor) {
        this. = encryptor;
        this. = registeredName;
        this. = true;
    }


    
Returns the encryptor which this object wraps.

Returns:
the encryptor.
    public PBEByteEncryptor getEncryptor() {
        return ;
    }
    
    
    
Sets the PBEByteEncryptor to be held (wrapped) by this object. This method is optional and can be only called once.

Parameters:
encryptor the encryptor.
    public void setEncryptor(PBEByteEncryptor encryptor) {
        if (this.) {
            throw new EncryptionInitializationException(
                    "An encryptor has been already set: no " +
                    "further configuration possible on hibernate wrapper");
        }
        this. = encryptor;
        this. = true;
    }


    
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
    public void setPassword(String password) {
        if (this.) {
            throw new EncryptionInitializationException(
                    "An encryptor has been already set: no " +
                    "further configuration possible on hibernate wrapper");
        }
        StandardPBEByteEncryptor standardPBEByteEncryptor =
            (StandardPBEByteEncryptorthis.;
        standardPBEByteEncryptor.setPassword(password);
    }


    
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
    public void setAlgorithm(String algorithm) {
        if (this.) {
            throw new EncryptionInitializationException(
                    "An encryptor has been already set: no " +
                    "further configuration possible on hibernate wrapper");
        }
        StandardPBEByteEncryptor standardPBEByteEncryptor =
            (StandardPBEByteEncryptorthis.;
        standardPBEByteEncryptor.setAlgorithm(algorithm);
    }
    

    
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
    public void setKeyObtentionIterations(int keyObtentionIterations) {
        if (this.) {
            throw new EncryptionInitializationException(
                    "An encryptor has been already set: no " +
                    "further configuration possible on hibernate wrapper");
        }
        StandardPBEByteEncryptor standardPBEByteEncryptor =
            (StandardPBEByteEncryptorthis.;
        standardPBEByteEncryptor.setKeyObtentionIterations(
                keyObtentionIterations);
    }
    

    
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.
    public void setSaltGenerator(SaltGenerator saltGenerator) {
        if (this.) {
            throw new EncryptionInitializationException(
                    "An encryptor has been already set: no " +
                    "further configuration possible on hibernate wrapper");
        }
        StandardPBEByteEncryptor standardPBEByteEncryptor =
            (StandardPBEByteEncryptorthis.;
        standardPBEByteEncryptor.setSaltGenerator(saltGenerator);
    }


    
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
    public void setConfig(PBEConfig config) {
        if (this.) {
            throw new EncryptionInitializationException(
                    "An encryptor has been already set: no " +
                    "further configuration possible on hibernate wrapper");
        }
        StandardPBEByteEncryptor standardPBEByteEncryptor =
            (StandardPBEByteEncryptorthis.;
        standardPBEByteEncryptor.setConfig(config);
    }


    
Encrypts a message, delegating to wrapped encryptor.

Parameters:
message the message to be encrypted.
Returns:
the encryption result.
    public byte[] encrypt(byte[] message) {
        if (this. == null) {
            throw new EncryptionInitializationException(
                    "Encryptor has not been set into Hibernate wrapper");
        }
        return .encrypt(message);
    }

    
    
Decypts a message, delegating to wrapped encryptor

Parameters:
encryptedMessage the message to be decrypted.
Returns:
the result of decryption.
    public byte[] decrypt(byte[] encryptedMessage) {
        if (this. == null) {
            throw new EncryptionInitializationException(
                    "Encryptor has not been set into Hibernate wrapper");
        }
        return .decrypt(encryptedMessage);
    }
    

    
    
Sets the registered name of the encryptor and adds it to the registry.

Parameters:
registeredName the name with which the encryptor will be registered.
    public void setRegisteredName(String registeredName) {
        if (this. != null) {
            // It had another name before, we have to clean
            HibernatePBEEncryptorRegistry.getInstance().
                    unregisterHibernatePBEByteEncryptor(this.);
        }
        this. = registeredName;
        HibernatePBEEncryptorRegistry.getInstance().
                registerHibernatePBEByteEncryptor(this);
    }

    
Returns the name with which the wrapped encryptor is registered at the registry.

Returns:
the registered name.
    public String getRegisteredName() {
        return ;
    }
    
New to GrepCode? Check out our FAQ X