Name

SecureRandom

Synopsis

This class generates cryptographic-quality pseudorandom bytes. Although SecureRandom defines public constructors, the preferred technique for obtaining a SecureRandom object is to call one of the static getInstance( ) factory methods, specifying the desired pseudorandom number-generation algorithm, and, optionally, the desired provider of that algorithm. Sun’s implementation of Java ships with an algorithm named “SHA1PRNG” in the “SUN” provider.

Once you have obtained a SecureRandom object, call nextBytes( ) to fill an array with pseudorandom bytes. You can also call any of the methods defined by the Random superclass to obtain random numbers. The first time one of these methods is called, the SecureRandom( ) method uses its generateSeed( ) method to seed itself. If you have a source of random or very high-quality pseudorandom bytes, you may provide your own seed by calling setSeed( ). Repeated calls to setSeed( ) augment the existing seed instead of replacing it. You can also call generateSeed( ) to generate seeds for use with other pseudorandom generators. generateSeed( ) may use a different algorithm than nextBytes( ) and may produce higher-quality randomness, usually at the expense of increased computation time.

java.security.SecureRandom

Figure 14-36. java.security.SecureRandom

public class SecureRandom extends java.util.Random {
// Public Constructors
     public SecureRandom

Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.