/** * Replay resistant message Id */ public long getUniqueId() { // Lazy initialization of value if (_uniqueId < 0) { _uniqueId = _context.random().nextLong(MAX_ID_VALUE); } return _uniqueId; }
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; } }
protected String selectProxy() { synchronized (_proxyList) { int size = _proxyList.size(); if (size <= 0) return null; int index = _context.random().nextInt(size); return _proxyList.get(index); } }
/** * Singleton for whatever PRNG i2p uses. * Same as I2PAppContext.getGlobalContext().random(); * use context.random() if you have a context already. * @return I2PAppContext.getGlobalContext().random() */ public static RandomSource getInstance() { return I2PAppContext.getGlobalContext().random(); }
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>(); }
/** add a random nonce to the head of the queue @since 0.8.1 */ private void addNonce() { String nextNonce = Long.toString(_context.random().nextLong()); synchronized (_nonces) { _nonces.add(0, nextNonce); int sz = _nonces.size(); if (sz > MAX_NONCES) _nonces.remove(sz - 1); } }
public boolean trim(KBucket<T> kbucket, T toAdd) { List<T> e = new ArrayList<T>(kbucket.getEntries()); int sz = e.size(); // concurrency if (sz < _max) return true; T toRemove = e.get(_ctx.random().nextInt(sz)); kbucket.remove(toRemove); return true; } }
/** outgoing - generate a random ID */ public MsgID(I2PAppContext ctx) { super(null); byte[] data = new byte[MY_TOK_LEN]; ctx.random().nextBytes(data); setData(data); setValid(MY_TOK_LEN); }
/** Generate a private 256 bit session key * @return session key */ public SessionKey generateSessionKey() { // 256bit random # as a session key SessionKey key = new SessionKey(); byte data[] = new byte[SessionKey.KEYSIZE_BYTES]; _context.random().nextBytes(data); key.setData(data); return key; }
/** 48 char b32 string (30 bytes of entropy) */ public static String randomString() { I2PAppContext ctx = I2PAppContext.getGlobalContext(); // make a random 48 character password (30 * 8 / 5) byte[] rand = new byte[30]; ctx.random().nextBytes(rand); return Base32.encode(rand); }
PeerCheckerTask(I2PSnarkUtil util, PeerCoordinator coordinator) { _util = util; _log = util.getContext().logManager().getLog(PeerCheckerTask.class); random = util.getContext().random(); this.coordinator = coordinator; }
/** * Create a new public/private value pair for the DH exchange. * Only for internal use and unit tests. * Others should get instances from PrecalcRunner.getBuilder() */ DHSessionKeyBuilder(I2PAppContext ctx) { _myPrivateValue = new NativeBigInteger(ctx.keyGenerator().getElGamalExponentSize(), ctx.random()); _myPublicValue = CryptoConstants.elgg.modPow(_myPrivateValue, CryptoConstants.elgp); _extraExchangedBytes = new ByteArray(); }
/** outgoing - generate a random token */ public Token(I2PAppContext ctx) { super(null); byte[] data = new byte[MY_TOK_LEN]; ctx.random().nextBytes(data); setData(data); setValid(MY_TOK_LEN); lastSeen = ctx.clock().now(); }
public void testMultiple(){ int size = 1; for(int i = 0; i < 16; i++){ byte[] message = new byte[size]; size*=2; _context.random().nextBytes(message); SHA256Generator.getInstance().calculateHash(message); } }
/** @since 0.9.10 */ public void testClosest() { byte val[] = new byte[Hash.HASH_LENGTH]; for (int i = 0; i < 23; i++) { context.random().nextBytes(val); Hash h = new Hash(val); List<Hash> c = set.getClosest(h, i); assertTrue(c.size() == i); } } }
public void testMultiple(){ int size = 1; for(int i = 0; i < 16; i++){ SessionKey key = _context.keyGenerator().generateSessionKey(); byte[] message = new byte[size]; size*=2; _context.random().nextBytes(message); byte[] output = new byte[32]; _context.hmac().calculate(key, message, 0, message.length, output, 0); } } }
private void addRandom(int count) { for (int i = 0; i < count; i++) { byte val[] = new byte[Hash.HASH_LENGTH]; context.random().nextBytes(val); Hash h = new Hash(val); // in the highly unlikely chance we randomly generate a hash equal to us assertTrue(set.add(h) || h.equals(usHash)); } }
public static void testED2() { I2PAppContext ctx = I2PAppContext.getGlobalContext(); SessionKey key = ctx.keyGenerator().generateSessionKey(); byte iv[] = new byte[16]; byte orig[] = new byte[128]; byte data[] = new byte[128]; ctx.random().nextBytes(iv); ctx.random().nextBytes(orig); CryptixAESEngine aes = new CryptixAESEngine(ctx); aes.encrypt(orig, 0, data, 0, key, iv, data.length); aes.decrypt(data, 0, data, 0, key, iv, data.length); assertTrue(DataHelper.eq(data,orig)); }
public void setUp(){ context = I2PAppContext.getGlobalContext(); log = context.logManager().getLog(KBucketSet.class); byte[] us = new byte[Hash.HASH_LENGTH]; context.random().nextBytes(us); usHash = new Hash(us); // We use the default RandomTrimmer so add() will never fail set = new KBucketSet<Hash>(context, usHash, K, B); // tests may be run in any order so prime it addRandom(1000); }
public void testMultiple(){ Object[] keys = KeyGenerator.getInstance().generatePKIKeypair(); byte[] message = new byte[222]; for (int x = 0; x < 25; x++) { _context.random().nextBytes(message); keys = KeyGenerator.getInstance().generatePKIKeypair(); PublicKey pubkey = (PublicKey)keys[0]; PrivateKey privkey = (PrivateKey)keys[1]; byte[] e = _context.elGamalEngine().encrypt(message, pubkey); byte[] d = _context.elGamalEngine().decrypt(e, privkey); assertTrue(DataHelper.eq(d, message)); } }