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.salt;

This implementation of SaltGenerator holds a secure random generator which can be used for generating random salts for encryption or digesting.

This class is thread-safe.

Author(s):
Daniel Fernández Garrido
Since:
1.2
public final class RandomSaltGenerator implements SaltGenerator {
    
    private static final String SECURE_RANDOM_ALGORITHM = "SHA1PRNG";
    
    private SecureRandom random = null;
    
    
    
Creates a new instance of RandomSaltGenerator
    public RandomSaltGenerator() {
        super();
        try {
             = SecureRandom.getInstance();
            .setSeed(System.currentTimeMillis());
        } catch (NoSuchAlgorithmException e) {
            throw new EncryptionInitializationException(e);
        }
    }
    

    
Generate a random salt of the specified length in bytes.

Parameters:
lengthBytes length in bytes.
Returns:
the generated salt.
    public byte[] generateSalt(int lengthBytes) {
        byte[] salt = new byte[lengthBytes];
        synchronized () {
            .nextBytes(salt);
        }
        return salt;
    }


    
This salt generator needs the salt to be included unencrypted in encryption results, because of its being random. This method will always return true.

Returns:
true
    public boolean includePlainSaltInEncryptionResults() {
        return true;
    }
    
New to GrepCode? Check out our FAQ X