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;
 

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(java.lang.String)) and performing the needed registry operations against the HibernatePBEEncryptorRegistry.

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 encryptor 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:

 
  <bean id="strongEncryptor"
    class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
    <property name="algorithm">
        <value>PBEWithMD5AndTripleDES</value>
    </property>
    <property name="password">
        <value>jasypt</value>
    </property>
  </bean>
  
  <bean id="hibernateEncryptor"
    class="org.jasypt.hibernate.HibernatePBEEncryptor">
    <property name="registeredName">
        <value>strongHibernateEncryptor</value>
    </property>
    <property name="encryptor">
        <ref bean="strongEncryptor" />
    </property>
  </bean>
 

And then in the Hibernate mapping file:

    <typedef name="encrypted" class="org.jasypt.hibernate.EncryptedTextType">
      <param name="encryptorRegisteredName">strongHibernateEncryptor</param>
    </typedef>
 

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

Deprecated:
Replaced by org.jasypt.hibernate.encryptor.HibernatePBEStringEncryptor and will be removed in version 1.3.
Author(s):
Daniel Fernández Garrido
Since:
1.0
public class HibernatePBEEncryptor {
    private String registeredName = null;
    private PBEStringEncryptor encryptor = null;
    
    
    
    
Creates a new instance of HibernatePBEEncryptor
    public HibernatePBEEncryptor() {
        super();
    }
    /*
     * For internal use only, by the Registry, when a PBEStringEncryptor
     * is registered programmatically.
     */
    HibernatePBEEncryptor(String registeredNamePBEStringEncryptor encryptor) {
        this. = registeredName;
        this. = encryptor;
    }


    
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().
                    unregisterHibernatePBEEncryptor(this.);
        }
        this. = registeredName;
        HibernatePBEEncryptorRegistry.getInstance().
                registerHibernatePBEEncryptor(this);
        
    }
    

    
Returns the encryptor which this object wraps.

Returns:
the encryptor.
    public PBEStringEncryptor getEncryptor() {
        return ;
    }
    

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

Returns:
the registered name.
    public String getRegisteredName() {
        return ;
    }
    
    
    
Sets the PBEStringEncryptor to be held (wrapped) by this object.

Parameters:
encryptor the encryptor.
    public void setEncryptor(PBEStringEncryptor encryptor) {
        this. = encryptor;
    }


    
Encrypts a message, delegating to wrapped encryptor.

Parameters:
message the message to be encrypted.
Returns:
the encryption result.
    public String encrypt(String 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 String decrypt(String encryptedMessage) {
        if (this. == null) {
            throw new EncryptionInitializationException(
                    "Encryptor has not been set into Hibernate wrapper");
        }
        return .decrypt(encryptedMessage);
    }
    
    
New to GrepCode? Check out our FAQ X