org.apache.shiro.crypto
Interface RandomNumberGenerator

All Known Implementing Classes:
SecureRandomNumberGenerator

public interface RandomNumberGenerator

A component that can generate random number/byte values as needed. Useful in cryptography or security scenarios where random byte arrays are needed, such as for password salts, nonces, initialization vectors and other seeds.

This is essentially the same as a SecureRandom, and indeed implementations of this interface will probably all use SecureRandom instances, but this interface provides a few additional benefits to end-users:

For example, consider the following example generating password salts for new user accounts:
 RandomNumberGenerator saltGenerator = new SecureRandomNumberGenerator();
 User user = new User();
 user.setPasswordSalt(saltGenerator.nextBytes().toBase64());
 userDAO.save(user);
 

Since:
1.1

Method Summary
 ByteSource nextBytes()
          Generates a byte array of fixed length filled with random data, often useful for generating salts, initialization vectors or other seed data.
 ByteSource nextBytes(int numBytes)
          Generates a byte array of the specified length filled with random data.
 

Method Detail

nextBytes

ByteSource nextBytes()
Generates a byte array of fixed length filled with random data, often useful for generating salts, initialization vectors or other seed data. The length is specified as a configuration value on the underlying implementation.

If you'd like per-invocation control the number of bytes generated, use the nextBytes(int) method instead.

Returns:
a byte array of fixed length filled with random data.
See Also:
nextBytes(int)

nextBytes

ByteSource nextBytes(int numBytes)
Generates a byte array of the specified length filled with random data.

Parameters:
numBytes - the number of bytes to be populated with random data.
Returns:
a byte array of the specified length filled with random data.
See Also:
nextBytes()


Copyright © 2004-2016 The Apache Software Foundation. All Rights Reserved.