public EntropySource get(final int bitsRequired) { return new SignallingEntropySource(bitsRequired); } })
public byte[] generateSeed(int numBytes) { synchronized (this) { byte[] data = new byte[numBytes]; int off = 0; int len; while (off != data.length && (len = privilegedRead(data, off, data.length - off)) > -1) { off += len; } if (off != data.length) { throw new InternalError("unable to fully read random source"); } return data; } }
URLSeededSecureRandom(final URL url) { super(null, new HybridRandomProvider()); this.seedStream = AccessController.doPrivileged(new PrivilegedAction<InputStream>() { public InputStream run() { try { return url.openStream(); } catch (IOException e) { throw new InternalError("unable to open random source"); } } }); }
private static SecureRandom createBaseRandom(boolean isPredictionResistant) { if (System.getProperty("org.bouncycastle.drbg.entropysource") != null) { EntropySourceProvider entropyProvider = createEntropySource(); EntropySource initSource = entropyProvider.get(16 * 8); byte[] personalisationString = isPredictionResistant ? generateDefaultPersonalizationString(initSource.getEntropy()) : generateNonceIVPersonalizationString(initSource.getEntropy()); return new SP800SecureRandomBuilder(entropyProvider) .setPersonalizationString(personalisationString) .buildHash(new SHA512Digest(), Arrays.concatenate(initSource.getEntropy(), initSource.getEntropy()), isPredictionResistant); } else { SecureRandom randomSource = new HybridSecureRandom(); // needs to be done late, can't use static byte[] personalisationString = isPredictionResistant ? generateDefaultPersonalizationString(randomSource.generateSeed(16)) : generateNonceIVPersonalizationString(randomSource.generateSeed(16)); return new SP800SecureRandomBuilder(randomSource, true) .setPersonalizationString(personalisationString) .buildHash(new SHA512Digest(), randomSource.generateSeed(32), isPredictionResistant); } }
private static SecureRandom createCoreSecureRandom() { if (initialEntropySourceAndSpi != null) { return new CoreSecureRandom(); } else { try { String source = Security.getProperty("securerandom.source"); return new URLSeededSecureRandom(new URL(source)); } catch (Exception e) { return new SecureRandom(); // we're desperate, it's worth a try. } } }
public byte[] getEntropy() { byte[] seed = (byte[])entropy.getAndSet(null); if (seed == null || seed.length != byteLength) { seed = baseRandom.generateSeed(byteLength); } else { scheduled.set(false); } if (!scheduled.getAndSet(true)) { new Thread(new EntropyGatherer(byteLength)).start(); } return seed; }
HybridSecureRandom() { super(null, new HybridRandomProvider()); drbg = new SP800SecureRandomBuilder(new EntropySourceProvider() { public EntropySource get(final int bitsRequired) { return new SignallingEntropySource(bitsRequired); } }) .setPersonalizationString(Strings.toByteArray("Bouncy Castle Hybrid Entropy Source")) .buildHMAC(new HMac(new SHA512Digest()), baseRandom.generateSeed(32), false); // 32 byte nonce }