{@code SecureRandom} is an engine class which is capable of generating
cryptographically secure pseudo-random numbers.
| Method from java.security.SecureRandom Detail: |
public byte[] generateSeed(int numBytes) {
return secureRandomSpi.engineGenerateSeed(numBytes);
}
Generates and returns the specified number of seed bytes, computed using
the seed generation algorithm used by this {@code SecureRandom}. |
public String getAlgorithm() {
return algorithm;
}
Returns the name of the algorithm of this {@code SecureRandom}. |
public static SecureRandom getInstance(String algorithm) throws NoSuchAlgorithmException {
if (algorithm == null) {
throw new NullPointerException(Messages.getString("security.01")); //$NON-NLS-1$
}
synchronized (engine) {
engine.getInstance(algorithm, null);
return new SecureRandom((SecureRandomSpi)engine.spi, engine.provider, algorithm);
}
}
Returns a new instance of {@code SecureRandom} that utilizes the
specified algorithm. |
public static SecureRandom getInstance(String algorithm,
String provider) throws NoSuchAlgorithmException, NoSuchProviderException {
if ((provider == null) || (provider.length() == 0)) {
throw new IllegalArgumentException(
Messages.getString("security.02")); //$NON-NLS-1$
}
Provider p = Security.getProvider(provider);
if (p == null) {
throw new NoSuchProviderException(Messages.getString("security.03", provider)); //$NON-NLS-1$
}
return getInstance(algorithm, p);
}
Returns a new instance of {@code SecureRandom} that utilizes the
specified algorithm from the specified provider. |
public static SecureRandom getInstance(String algorithm,
Provider provider) throws NoSuchAlgorithmException {
if (provider == null) {
throw new IllegalArgumentException(Messages.getString("security.04")); //$NON-NLS-1$
}
if (algorithm == null) {
throw new NullPointerException(Messages.getString("security.01")); //$NON-NLS-1$
}
synchronized (engine) {
engine.getInstance(algorithm, provider, null);
return new SecureRandom((SecureRandomSpi)engine.spi, provider, algorithm);
}
}
Returns a new instance of {@code SecureRandom} that utilizes the
specified algorithm from the specified provider. |
public final Provider getProvider() {
return provider;
}
Returns the provider associated with this {@code SecureRandom}. |
public static byte[] getSeed(int numBytes) {
if (internalSecureRandom == null) {
internalSecureRandom = new SecureRandom();
}
return internalSecureRandom.generateSeed(numBytes);
}
Generates and returns the specified number of seed bytes, computed using
the seed generation algorithm used by this {@code SecureRandom}. |
protected final int next(int numBits) {
if (numBits < 0) {
numBits = 0;
} else {
if (numBits > 32) {
numBits = 32;
}
}
int bytes = (numBits+7)/8;
byte[] next = new byte[bytes];
int ret = 0;
nextBytes(next);
for (int i = 0; i < bytes; i++) {
ret = (next[i] & 0xFF) | (ret < < 8);
}
ret = ret > > > (bytes*8 - numBits);
return ret;
}
Generates and returns an {@code int} containing the specified number of
random bits (right justified, with leading zeros). |
public synchronized void nextBytes(byte[] bytes) {
secureRandomSpi.engineNextBytes(bytes);
}
Generates and stores random bytes in the given {@code byte[]} for each
array element. |
public synchronized void setSeed(byte[] seed) {
secureRandomSpi.engineSetSeed(seed);
}
Reseeds this {@code SecureRandom} instance with the specified {@code
seed}. The seed of this {@code SecureRandom} instance is supplemented,
not replaced. |
public void setSeed(long seed) {
if (seed == 0) { // skip call from Random
return;
}
byte[] byteSeed = {
(byte)((seed > > 56) & 0xFF),
(byte)((seed > > 48) & 0xFF),
(byte)((seed > > 40) & 0xFF),
(byte)((seed > > 32) & 0xFF),
(byte)((seed > > 24) & 0xFF),
(byte)((seed > > 16) & 0xFF),
(byte)((seed > > 8) & 0xFF),
(byte)((seed) & 0xFF)
};
setSeed(byteSeed);
}
Reseeds this this {@code SecureRandom} instance with the eight bytes
described by the representation of the given {@code long seed}. The seed
of this {@code SecureRandom} instance is supplemented, not replaced. |