/** reseed the fortuna */ @Override public void feedEntropy(String source, long data, int bitoffset, int bits) { synchronized(_fortuna) { _fortuna.addRandomByte((byte)(data & 0xFF)); } }
@Override public void setSeed(byte buf[]) { synchronized(_fortuna) { _fortuna.addRandomBytes(buf); } }
@Override public boolean nextBoolean() { byte val; synchronized(_fortuna) { val = _fortuna.nextByte(); } return ((val & 0x01) != 0); }
/** * May block up to 10 seconds or forever */ public FortunaRandomSource(I2PAppContext context) { super(context); _fortuna = new AsyncFortunaStandalone(context); byte seed[] = new byte[1024]; // may block for 10 seconds if (initSeed(seed)) { _fortuna.seed(seed); } else { // may block forever //SecureRandom sr = new SecureRandom(); // SecureRandom already failed in initSeed(), so try Random Random sr = new Random(); sr.nextBytes(seed); _fortuna.seed(seed); } _fortuna.startup(); // kickstart it _fortuna.nextBytes(seed); _haveNextGaussian = false; }
/** * Not part of java.util.SecureRandom, but added for efficiency, since Fortuna supports it. * * @since 0.8.12 */ @Override public void nextBytes(byte buf[], int offset, int length) { synchronized(_fortuna) { _fortuna.nextBytes(buf, offset, length); } }
/** * Note - methods may hang or NPE or throw IllegalStateExceptions after this * @since 0.8.8 */ public void shutdown() { synchronized(_fortuna) { _fortuna.shutdown(); } }
@Override public void fillBlock() { rotateBuffer(); }
/** the seed is only propogated once the prng is started with startup() */ @Override public void seed(byte val[]) { Map<String, byte[]> props = Collections.singletonMap(SEED, val); init(props); //fillBlock(); }
/** * The refiller thread */ public void run() { while (_isRunning) { AsyncBuffer aBuff = null; try { aBuff = _emptyBuffers.take(); } catch (InterruptedException ie) { continue; } long before = System.currentTimeMillis(); doFill(aBuff.buffer); long after = System.currentTimeMillis(); boolean shouldWait = _fullBuffers.size() > 1; _fullBuffers.offer(aBuff); _context.statManager().addRateData("prng.bufferFillTime", after - before, 0); if (shouldWait) { Thread.yield(); long waitTime = (after-before)*5; if (waitTime <= 0) // somehow postman saw waitTime show up as negative waitTime = 50; else if (waitTime > 5000) waitTime = 5000; try { Thread.sleep(waitTime); } catch (InterruptedException ie) {} } } }
@Override public void nextBytes(byte buf[]) { synchronized(_fortuna) { _fortuna.nextBytes(buf); } }
/** * Not part of java.util.SecureRandom, but added for efficiency, since Fortuna supports it. * * @since 0.9.24 */ public byte nextByte() { synchronized(_fortuna) { return _fortuna.nextByte(); } }
/** reseed the fortuna */ @Override public void feedEntropy(String source, byte[] data, int offset, int len) { try { synchronized(_fortuna) { _fortuna.addRandomBytes(data, offset, len); } } catch (RuntimeException e) { // AIOOBE seen, root cause unknown, ticket #1576 Log log = _context.logManager().getLog(FortunaRandomSource.class); log.warn("feedEntropy()", e); } }
/** * Pull the next numBits of random data off the fortuna instance (returning 0 * through 2^numBits-1 * * Caller must synchronize! */ protected int nextBits(int numBits) { long rv = 0; int bytes = (numBits + 7) / 8; for (int i = 0; i < bytes; i++) rv += ((_fortuna.nextByte() & 0xFF) << i*8); //rv >>>= (64-numBits); if (rv < 0) rv = 0 - rv; int off = 8*bytes - numBits; rv >>>= off; return (int)rv; }