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;
 
 

A Hibernate 3 UserType implementation which allows transparent encryption of text values during persistence of entities.

This class is intended only for declarative use from a Hibernate mapping file. Do not use it directly from your .java files.

To use this Hibernate type in one of your Hibernate mappings, you can add it like this:

  <hibernate-mapping package="myapp">
    ...
    <typedef name="encrypted" class="org.jasypt.hibernate.EncryptedTextType">
      <param name="encryptorRegisteredName">strongHibernateEncryptor</param>
    </typedef>
    ...
    <class name="UserData" table="USER_DATA">
      ...
      <property name="address" column="ADDRESS" type="encrypted" />
      ...
    <class>
    ...
  <hibernate-mapping>
 

...where an encryptor should have been previously registered to be used from Hibernate with name strongHibernateEncryptor (see HibernatePBEEncryptor and HibernatePBEEncryptorRegistry).

Or, if you prefer to avoid registration of encryptors, you can configure your encryptor directly in the mapping file (although not recommended), like this:

  <hibernate-mapping package="myapp">
    ...
    <typedef name="encrypted" class="org.jasypt.hibernate.EncryptedTextType">
      <param name="algorithm">PBEWithMD5AndTripleDES</param>
      <param name="password">jasypt</param>
      <param name="keyObtentionIterations">1000</param>
    </typedef>
    ...
    <class name="UserData" table="USER_DATA">
      ...
      <property name="address" column="ADDRESS" type="encrypted" />
      ...
    <class>
    ...
  <hibernate-mapping>
 

To learn more about usage of user-defined types, please refer to the Hibernate Reference Documentation.

Deprecated:
Replaced by org.jasypt.hibernate.type.EncryptedStringType and will be removed in version 1.3.
Author(s):
Daniel Fernández Garrido
Since:
1.0
public final class EncryptedTextType implements UserTypeParameterizedType {
    private static NullableType nullableType = .;
    private static int sqlType = .sqlType();
    private static int[] sqlTypes = new int[]{  };
    
    private boolean initialized = false;
    private boolean useEncryptorName = false;
    
    private String encryptorName = null;
    private String algorithm = null;
    private String password = null;
    private Integer keyObtentionIterations = null;
    
    private PBEStringEncryptor encryptor = null;
    
    public int[] sqlTypes() {
        return ;
    }
    
    public Class returnedClass() {
        return String.class;
    }
    
    public boolean equals(Object xObject y
            throws HibernateException {
        return EqualsHelper.equals(xy);
    }
    
    
    public Object deepCopy(Object value)
            throws HibernateException {
        return value;
    }
    
    
    public Object assemble(Serializable cachedObject owner)
            throws HibernateException {
        if (cached == null) {
            return null;
        } else {
            return deepCopy(cached);
        }
    }
    
    public Serializable disassemble(Object value
            throws HibernateException {
        if (value == null) {
            return null;
        } else {
            return (SerializabledeepCopy(value);
        }
    }
    
    public boolean isMutable() {
        return false;
    }
    public int hashCode(Object x)
            throws HibernateException {
        return x.hashCode();
    }
    
    public Object replace(Object originalObject targetObject owner
            throws HibernateException {
        return original;
    }
    
    public Object nullSafeGet(ResultSet rsString[] namesObject owner)
            throws HibernateExceptionSQLException {
        checkInitialization();
        String message = rs.getString(names[0]);
        return rs.wasNull() ? null : this..decrypt(message);
    }
    
    public void nullSafeSet(PreparedStatement stObject valueint index)
            throws HibernateExceptionSQLException {
        checkInitialization();
        if (value == null) {
            st.setNull(index);
        } else {
            st.setString(indexthis..encrypt((Stringvalue));
        }
    }
    
    public synchronized void setParameterValues(Properties parameters) {
        
        String paramEncryptorName =
            parameters.getProperty(.);
        String paramAlgorithm =
            parameters.getProperty(.);
        String paramPassword =
            parameters.getProperty(.);
        String paramKeyObtentionIterations =
            parameters.getProperty(.);
        
        this. = false;
        if (paramEncryptorName != null) {
            
            if ((paramAlgorithm != null) ||
                (paramPassword != null) ||
                (paramKeyObtentionIterations != null)) {
                
                throw new EncryptionInitializationException(
                        "If \"" + . + 
                        "\" is specified, none of \"" +
                        . + "\", \"" +
                        . + "\" or \"" + 
                        . + "\" " +
                        "can be specified");
                
            }
            this. = paramEncryptorName;
            this. = true;
            
        } else if ((paramPassword != null)) {
            this. = paramPassword;
            
            if (paramAlgorithm != null) {
                this. = paramAlgorithm;
            }
            
            if (paramKeyObtentionIterations != null) {
                try {
                    this. = 
                        new Integer(
                                Integer.parseInt(paramKeyObtentionIterations));
                } catch (NumberFormatException e) {
                    throw new EncryptionInitializationException(
                            "Value specified for \"" + 
                            . + 
                            "\" is not a valid integer");
                }
                
            }
            
        } else {
            
            throw new EncryptionInitializationException(
                    "If \"" + . + 
                    "\" is not specified, then \"" +
                    . + "\" (and optionally \"" +
                    . + "\" and \"" + 
                    . + "\") " +
                    "must be specified");
            
        }
    }
    
    
    private synchronized void checkInitialization() {
        
        if (!this.) {
            
            if (this.) {
                HibernatePBEEncryptorRegistry registry = 
                    HibernatePBEEncryptorRegistry.getInstance();
                HibernatePBEEncryptor hibernateEncryptor = 
                    registry.getHibernatePBEEncryptor();
                if (hibernateEncryptor == null) {
                    throw new EncryptionInitializationException(
                            "No encryptor registered for hibernate with " +
                            "name \"" +  + "\"");
                }
                this. = hibernateEncryptor.getEncryptor();
                
            } else {
                
                StandardPBEStringEncryptor newEncryptor = 
                    new StandardPBEStringEncryptor();
                
                newEncryptor.setPassword(this.);
                
                if (this. != null) {
                    newEncryptor.setAlgorithm(this.);
                }
                
                if (this. != null) {
                    newEncryptor.setKeyObtentionIterations(
                            this..intValue());
                }
                
                newEncryptor.initialize();
                
                this. = newEncryptor;
                
            }
            
            this. = true;
        }
        
    }
    
    
New to GrepCode? Check out our FAQ X