/** * Replay resistant message Id */ public long getUniqueId() { // Lazy initialization of value if (_uniqueId < 0) { _uniqueId = _context.random().nextLong(MAX_ID_VALUE); } return _uniqueId; }
private static Hash generateRandomKey() { byte hash[] = new byte[Hash.HASH_LENGTH]; RandomSource.getInstance().nextBytes(hash); return new Hash(hash); }
protected String selectProxy() { synchronized (_proxyList) { int size = _proxyList.size(); if (size <= 0) return null; int index = _context.random().nextInt(size); return _proxyList.get(index); } }
ConnThrottler(int max, int totalMax, long period, SimpleTimer2 timer) { _max = max; _totalMax = totalMax; this.counter = new ObjectCounter<Hash>(); _currentTotal = new AtomicInteger(); // shorten the initial period by a random amount // to prevent correlation across destinations // and identification of router startup time timer.addPeriodicEvent(new Cleaner(), (period / 2) + RandomSource.getInstance().nextLong(period / 2), period); }
public I2PTunnelHTTPClientBase(int localPort, boolean ownDest, Logging l, EventDispatcher notifyThis, String handlerName, I2PTunnel tunnel) throws IllegalArgumentException { super(localPort, ownDest, l, notifyThis, handlerName, tunnel); _proxyList = new ArrayList<String>(4); _proxyNonce = new byte[PROXYNONCE_BYTES]; _context.random().nextBytes(_proxyNonce); _nonces = new ConcurrentHashMap<String, NonceInfo>(); }
public void testElGamalEngine(){ int numRuns = 10; RandomSource.getInstance().nextBoolean(); I2PAppContext context = I2PAppContext.getGlobalContext(); for (int i = 0; i < numRuns; i++) { Object pair[] = KeyGenerator.getInstance().generatePKIKeypair(); PublicKey pubkey = (PublicKey) pair[0]; PrivateKey privkey = (PrivateKey) pair[1]; byte buf[] = new byte[128]; RandomSource.getInstance().nextBytes(buf); byte encr[] = context.elGamalEngine().encrypt(buf, pubkey); byte decr[] = context.elGamalEngine().decrypt(encr, privkey); assertTrue(DataHelper.eq(decr, buf)); } }
/** * Efficiently fill with nonzero random data * Don't waste too much entropy or call random() too often. * @since 0.8.5 */ private void fillRandomNonZero(byte[] b, int off, int len) { // get about as much as we think we will need, overestimate some final int est = len + (len / 128) + 3; final byte[] tmp = new byte[est]; _context.random().nextBytes(tmp); int extra = len; for (int i = 0; i < len; i++) { while (tmp[i] == 0) { if (extra < est) tmp[i] = tmp[extra++]; // use from the extra we have at the end else tmp[i] = (byte)(_context.random().nextInt() & 0xFF); // waste 3/4 of the entropy } } System.arraycopy(tmp, 0, b, off, len); }
protected PendingGatewayMessage createPending(int size, boolean includeRouter, boolean includeTunnel) { DataMessage m = new DataMessage(_context); byte data[] = new byte[size]; _context.random().nextBytes(data); m.setData(data); m.setUniqueId(_context.random().nextLong(I2NPMessage.MAX_ID_VALUE)); m.setMessageExpiration(_context.clock().now() + 60*1000); Hash toRouter = null; TunnelId toTunnel = null; if (includeRouter) { toRouter = new Hash(new byte[Hash.HASH_LENGTH]); _context.random().nextBytes(toRouter.getData()); } if (includeTunnel) toTunnel = new TunnelId(1 + _context.random().nextLong(TunnelId.MAX_ID_VALUE)); return new PendingGatewayMessage(m, toRouter, toTunnel); }
/** @return chunk number */ public int getNextRequest() { if (!isInitialized) throw new IllegalArgumentException("not initialized"); if (complete) throw new IllegalArgumentException("complete"); int rand = RandomSource.getInstance().nextInt(totalChunks); for (int i = 0; i < totalChunks; i++) { int chk = (i + rand) % totalChunks; if (!(have.get(chk) || requested.get(chk))) { requested.set(chk); return chk; } } // all requested - end game for (int i = 0; i < totalChunks; i++) { int chk = (i + rand) % totalChunks; if (!have.get(chk)) return chk; } throw new IllegalArgumentException("complete"); }
SecureRandom rand = RandomSource.getInstance(); rand.nextBoolean(); System.out.println("DEBUG: Random number generator warmed up");
long offset = r.getContext().random().nextLong(Router.CLOCK_FUDGE_FACTOR/2); if (r.getContext().random().nextBoolean()) offset = 0 - offset; r.getContext().clock().setOffset(offset, true);
RandomSource.getInstance().nextBoolean(); try { Thread.sleep(1000); } catch (InterruptedException ie) {} int runs = 200; // warmup
int skew = settings.getLengthVariance(); if (skew > 0) length += ctx.random().nextInt(skew+1); else { skew = 1 - skew; int off = ctx.random().nextInt(skew); if (ctx.random().nextBoolean()) length += off; else
RandomSource.getInstance().harvester().feedEntropy("DH", buf, val.length, buf.length-val.length); byte remaining[] = new byte[buf.length - val.length]; System.arraycopy(buf, val.length, remaining, 0, remaining.length);
rv = (((expire270s > 0) && _context.random().nextBoolean()) ? 1 : 0); rv += expire210s; rv += 2*expire150s; rv -= expireLater; } else { rv = (((expire270s > 0) && _context.random().nextBoolean()) ? 1 : 0); rv += expire210s; rv += 2*expire150s; rv = (((expire270s > 0) && _context.random().nextBoolean()) ? 1 : 0); rv += expire210s; rv += 2*expire150s; rv = (((expire270s > 0) && _context.random().nextBoolean()) ? 1 : 0); rv += expire210s; rv += 2*expire150s; rv = (((expire270s > 0) && _context.random().nextBoolean()) ? 1 : 0); rv += expire210s; rv -= inProgress; rv = (((expire270s > 0) && _context.random().nextBoolean()) ? 1 : 0); rv -= inProgress; rv -= expireLater;
_context.random().harvester().feedEntropy("ElG/AES", elgDecr, offset, elgDecr.length - offset);
public int writeData(byte[] tgt, int off) { if (ctx != null) ctx.random().nextBytes(tgt, off, sz); else Arrays.fill(tgt, off, off + sz, (byte) 0); return off + sz; } }
public void testKeyGen(){ RandomSource.getInstance().nextBoolean(); byte src[] = new byte[200]; RandomSource.getInstance().nextBytes(src); I2PAppContext ctx = I2PAppContext.getGlobalContext(); for (int i = 0; i < 10; i++) { Object keys[] = KeyGenerator.getInstance().generatePKIKeypair(); byte ctext[] = ctx.elGamalEngine().encrypt(src, (PublicKey) keys[0]); byte ptext[] = ctx.elGamalEngine().decrypt(ctext, (PrivateKey) keys[1]); assertTrue(DataHelper.eq(ptext, src)); } Object obj[] = KeyGenerator.getInstance().generateSigningKeypair(); SigningPublicKey fake = (SigningPublicKey) obj[0]; for (int i = 0; i < 10; i++) { Object keys[] = KeyGenerator.getInstance().generateSigningKeypair(); Signature sig = DSAEngine.getInstance().sign(src, (SigningPrivateKey) keys[1]); assertTrue(DSAEngine.getInstance().verifySignature(sig, src, (SigningPublicKey) keys[0])); assertFalse(DSAEngine.getInstance().verifySignature(sig, src, fake)); } for (int i = 0; i < 1000; i++) { KeyGenerator.getInstance().generateSessionKey(); } } }
/** * Pad a random amount (not mod 16) and return new offset, * while honoring the max length. * These bytes will be included in the MAC calculation but not encrypted. * This must be called before encryption. * * @return new offset * @since 0.9.7 */ private int pad2(byte[] data, int off, int maxLen) { if (!_context.getProperty(PROP_PADDING, DEFAULT_ENABLE_PADDING)) return off; if (off >= maxLen) return off; int padSize = _context.random().nextInt(Math.min(MAX_PAD2, 1 + maxLen - off)); if (padSize == 0) return off; _context.random().nextBytes(data, off, padSize); return off + padSize; }