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.util.digest;
 
 

Utility class for creating digests without using a salt or iterating the hash function. This means that digests created by this class will be compatible (and equivalent) to the ones which could be created by the user by directly using a java.security.MessageDigest object.

This class can be thought of as convenience wrapper for java.security.MessageDigest, adding thread-safety and a more javabean-like interface to it. These two features enable a more adequate use from an IoC container like Spring.

This class internally holds a org.jasypt.digest.StandardByteDigester configured this way:

  • Algorithm: MD5 by default, but configurable.
  • Provider: Default JVM security provider, but configurable.
  • Salt size: 0 bytes, no salt used.
  • Iterations: 1, hash function will not be iterated.

This class is thread-safe

Author(s):
Daniel Fernández Garrido
Since:
1.2 (class existed as org.jasypt.util.MessageDigester since 1.1)
 
 public final class Digester {

    
    
MD5 will be the default algorithm to be used if none is specified.
 
     public static final String DEFAULT_ALGORITHM = "MD5";
 
     // The hash function will be applied only once
     private static final int ITERATIONS = 1;
     
     // No salt will be used
     private static final int SALT_SIZE_BYTES = 0;
     
     // The internal digester used
     private final StandardByteDigester digester;
    
    
    
Creates a new instance of Digester. It will use the default algorithm unless one is specified with setAlgorithm(java.lang.String).
 
     public Digester() {
         super();
         this. = new StandardByteDigester();
         this..setIterations();
     }
    
    

Creates a new instance of Digester, specifying the algorithm to be used.

 
     public Digester(String algorithm) {
         super();
         this. = new StandardByteDigester();
         this..setIterations();
        this..setAlgorithm(algorithm);
    }

    
    

Creates a new instance of Digester, specifying the algorithm to be used.

Since:
1.3
    public Digester(String algorithmString providerName) {
        super();
        this. = new StandardByteDigester();
        this..setIterations();
        this..setAlgorithm(algorithm);
        this..setProviderName(providerName);
    }

    
    

Creates a new instance of Digester, specifying the algorithm to be used.

Since:
1.3
    public Digester(String algorithmProvider provider) {
        super();
        this. = new StandardByteDigester();
        this..setIterations();
        this..setAlgorithm(algorithm);
        this..setProvider(provider);
    }
    
    
    

Sets the algorithm to be used for digesting, like MD5 or SHA-1.

This algorithm has to be supported by your security infrastructure, and it should be allowed as an algorithm for creating java.security.MessageDigest instances.

If you are specifying a security provider with setProvider(java.security.Provider) or setProviderName(java.lang.String), this algorithm should be supported by your specified provider.

If you are not specifying a provider, you will be able to use those algorithms provided by the default security provider of your JVM vendor. For valid names in the Sun JVM, see Java Cryptography Architecture API Specification & Reference.

Parameters:
algorithm the name of the algorithm to be used.
Throws:
org.jasypt.exceptions.AlreadyInitializedException if it has already been initialized, this is, if digest(byte[]) has been called at least once.
    public void setAlgorithm(String algorithm) {
        this..setAlgorithm(algorithm);
    }

    
    

Sets the name of the security provider to be asked for the digest algorithm. This security provider has to be registered beforehand at the JVM security framework.

The provider can also be set with the setProvider(java.security.Provider) method, in which case it will not be necessary neither registering the provider beforehand, nor calling this setProviderName(java.lang.String) method to specify a provider name.

Note that a call to setProvider(java.security.Provider) overrides any value set by this method.

If no provider name / provider is explicitly set, the default JVM provider will be used.

Parameters:
providerName the name of the security provider to be asked for the digest algorithm.
Throws:
org.jasypt.exceptions.AlreadyInitializedException if it has already been initialized, this is, if digest(byte[]) has been called at least once.
Since:
1.3
    public void setProviderName(String providerName) {
        this..setProviderName(providerName);
    }
    
    
    

Sets the security provider to be asked for the digest algorithm. The provider does not have to be registered at the security infrastructure beforehand, and its being used here will not result in it being registered.

If this method is called, calling setProviderName(java.lang.String) becomes unnecessary.

If no provider name / provider is explicitly set, the default JVM provider will be used.

Parameters:
provider the provider to be asked for the chosen algorithm
Throws:
org.jasypt.exceptions.AlreadyInitializedException if it has already been initialized, this is, if digest(byte[]) has been called at least once.
Since:
1.3
    public void setProvider(Provider provider) {
        this..setProvider(provider);
    }

    
    
Creates a digest.

Parameters:
binary the byte array to be digested.
Returns:
the resulting digest.
See also:
org.jasypt.digest.StandardByteDigester.digest(byte[])
    public byte[] digest(byte[] binary) {
        return this..digest(binary);
    }
    
New to GrepCode? Check out our FAQ X