Class RandomSaltGenerator

  • All Implemented Interfaces:
    SaltGenerator

    public class RandomSaltGenerator
    extends java.lang.Object
    implements SaltGenerator

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

    The algorithm used for random number generation can be configured at instantiation time. If not, the default algorithm will be used.

    This class is thread-safe.

    Since:
    1.2
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String DEFAULT_SECURE_RANDOM_ALGORITHM
      The default algorithm to be used for secure random number generation: set to SHA1PRNG.
      private java.security.SecureRandom random  
    • Constructor Summary

      Constructors 
      Constructor Description
      RandomSaltGenerator()
      Creates a new instance of RandomSaltGenerator using the default secure random number generation algorithm.
      RandomSaltGenerator​(java.lang.String secureRandomAlgorithm)
      Creates a new instance of RandomSaltGenerator specifying a secure random number generation algorithm.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      byte[] generateSalt​(int lengthBytes)
      Generate a random salt of the specified length in bytes.
      boolean includePlainSaltInEncryptionResults()
      This salt generator needs the salt to be included unencrypted in encryption results, because of its being random.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_SECURE_RANDOM_ALGORITHM

        public static final java.lang.String DEFAULT_SECURE_RANDOM_ALGORITHM
        The default algorithm to be used for secure random number generation: set to SHA1PRNG.
        See Also:
        Constant Field Values
      • random

        private final java.security.SecureRandom random
    • Constructor Detail

      • RandomSaltGenerator

        public RandomSaltGenerator()
        Creates a new instance of RandomSaltGenerator using the default secure random number generation algorithm.
      • RandomSaltGenerator

        public RandomSaltGenerator​(java.lang.String secureRandomAlgorithm)
        Creates a new instance of RandomSaltGenerator specifying a secure random number generation algorithm.
        Since:
        1.5
    • Method Detail

      • generateSalt

        public byte[] generateSalt​(int lengthBytes)
        Generate a random salt of the specified length in bytes.
        Specified by:
        generateSalt in interface SaltGenerator
        Parameters:
        lengthBytes - length in bytes.
        Returns:
        the generated salt.
      • includePlainSaltInEncryptionResults

        public boolean includePlainSaltInEncryptionResults()
        This salt generator needs the salt to be included unencrypted in encryption results, because of its being random. This method will always return true.
        Specified by:
        includePlainSaltInEncryptionResults in interface SaltGenerator
        Returns:
        true