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);
}
}
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;
}
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);
}
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);
}
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);
}
}
Aggregations