Search in sources :

Example 56 with Hash

use of net.i2p.data.Hash in project i2p.i2p by i2p.

the class DeliveryInstructions method readBytes.

/**
 * @deprecated unused
 */
@Deprecated
public void readBytes(InputStream in) throws DataFormatException, IOException {
    long flags = DataHelper.readLong(in, 1);
    // if (_log.shouldLog(Log.DEBUG))
    // _log.debug("Read flags: " + flags + " mode: " +  flagMode(flags));
    /**
     **
     *        if (flagEncrypted(flags)) {
     *            SessionKey k = new SessionKey();
     *            k.readBytes(in);
     *            setEncryptionKey(k);
     *            setEncrypted(true);
     *        } else {
     *            setEncrypted(false);
     *        }
     ***
     */
    setDeliveryMode(flagMode(flags));
    switch(flagMode(flags)) {
        case FLAG_MODE_LOCAL:
            break;
        case FLAG_MODE_DESTINATION:
            // Hash destHash = new Hash();
            // destHash.readBytes(in);
            Hash destHash = Hash.create(in);
            setDestination(destHash);
            break;
        case FLAG_MODE_ROUTER:
            // Hash routerHash = new Hash();
            // routerHash.readBytes(in);
            Hash routerHash = Hash.create(in);
            setRouter(routerHash);
            break;
        case FLAG_MODE_TUNNEL:
            // Hash tunnelRouterHash = new Hash();
            // tunnelRouterHash.readBytes(in);
            Hash tunnelRouterHash = Hash.create(in);
            setRouter(tunnelRouterHash);
            TunnelId id = new TunnelId();
            id.readBytes(in);
            setTunnelId(id);
            break;
    }
    if (flagDelay(flags)) {
        long delay = DataHelper.readLong(in, 4);
        setDelayRequested(true);
        setDelaySeconds(delay);
    } else {
        setDelayRequested(false);
    }
}
Also used : Hash(net.i2p.data.Hash) TunnelId(net.i2p.data.TunnelId)

Example 57 with Hash

use of net.i2p.data.Hash in project i2p.i2p by i2p.

the class DeliveryInstructions method readBytes.

public int readBytes(byte[] data, int offset) throws DataFormatException {
    int cur = offset;
    int flags = data[cur] & 0xff;
    cur++;
    // if (_log.shouldLog(Log.DEBUG))
    // _log.debug("Read flags: " + flags + " mode: " +  flagMode(flags));
    /**
     **
     *        if (flagEncrypted(flags)) {
     *            byte kd[] = new byte[SessionKey.KEYSIZE_BYTES];
     *            System.arraycopy(data, cur, kd, 0, SessionKey.KEYSIZE_BYTES);
     *            cur += SessionKey.KEYSIZE_BYTES;
     *            setEncryptionKey(new SessionKey(kd));
     *            setEncrypted(true);
     *        } else {
     *            setEncrypted(false);
     *        }
     ***
     */
    setDeliveryMode(flagMode(flags));
    switch(flagMode(flags)) {
        case FLAG_MODE_LOCAL:
            break;
        case FLAG_MODE_DESTINATION:
            // byte destHash[] = new byte[Hash.HASH_LENGTH];
            // System.arraycopy(data, cur, destHash, 0, Hash.HASH_LENGTH);
            Hash dh = Hash.create(data, cur);
            cur += Hash.HASH_LENGTH;
            setDestination(dh);
            break;
        case FLAG_MODE_ROUTER:
            // byte routerHash[] = new byte[Hash.HASH_LENGTH];
            // System.arraycopy(data, cur, routerHash, 0, Hash.HASH_LENGTH);
            Hash rh = Hash.create(data, cur);
            cur += Hash.HASH_LENGTH;
            setRouter(rh);
            break;
        case FLAG_MODE_TUNNEL:
            // byte tunnelRouterHash[] = new byte[Hash.HASH_LENGTH];
            // System.arraycopy(data, cur, tunnelRouterHash, 0, Hash.HASH_LENGTH);
            Hash trh = Hash.create(data, cur);
            cur += Hash.HASH_LENGTH;
            setRouter(trh);
            setTunnelId(new TunnelId(DataHelper.fromLong(data, cur, 4)));
            cur += 4;
            break;
    }
    if (flagDelay(flags)) {
        long delay = DataHelper.fromLong(data, cur, 4);
        cur += 4;
        setDelayRequested(true);
        setDelaySeconds(delay);
    } else {
        setDelayRequested(false);
    }
    return cur - offset;
}
Also used : Hash(net.i2p.data.Hash) TunnelId(net.i2p.data.TunnelId)

Example 58 with Hash

use of net.i2p.data.Hash in project i2p.i2p by i2p.

the class KBucketSetTest method setUp.

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);
}
Also used : Hash(net.i2p.data.Hash)

Example 59 with Hash

use of net.i2p.data.Hash in project i2p.i2p by i2p.

the class KBucketSetTest method testGenRandom.

/**
 * @since 0.9.10
 */
public void testGenRandom() {
    int errors = 0;
    for (KBucket<Hash> b : set.getBuckets()) {
        for (int j = 0; j < 4000; j++) {
            Hash rand = set.generateRandomKey(b);
            int range = set.getRange(rand);
            if (range < b.getRangeBegin() || range > b.getRangeEnd()) {
                log.error("Generate random key failed range=" + range + " for " + rand + " meant for bucket " + b);
                errors++;
            }
        }
    }
    assertTrue(errors == 0);
}
Also used : Hash(net.i2p.data.Hash)

Example 60 with Hash

use of net.i2p.data.Hash in project i2p.i2p by i2p.

the class KBucketSetTest method testClosest.

/**
 * @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);
    }
}
Also used : Hash(net.i2p.data.Hash)

Aggregations

Hash (net.i2p.data.Hash)235 RouterInfo (net.i2p.data.router.RouterInfo)45 ArrayList (java.util.ArrayList)29 TunnelId (net.i2p.data.TunnelId)20 Destination (net.i2p.data.Destination)18 HashSet (java.util.HashSet)17 ConvertToHash (net.i2p.util.ConvertToHash)17 IOException (java.io.IOException)16 TunnelInfo (net.i2p.router.TunnelInfo)15 DataFormatException (net.i2p.data.DataFormatException)14 Properties (java.util.Properties)13 Date (java.util.Date)12 DatabaseEntry (net.i2p.data.DatabaseEntry)11 SessionKey (net.i2p.data.SessionKey)11 RouterAddress (net.i2p.data.router.RouterAddress)11 DatabaseStoreMessage (net.i2p.data.i2np.DatabaseStoreMessage)9 I2NPMessage (net.i2p.data.i2np.I2NPMessage)9 Job (net.i2p.router.Job)9 OutNetMessage (net.i2p.router.OutNetMessage)9 TunnelPoolSettings (net.i2p.router.TunnelPoolSettings)8