/** * @param target key to compare distances with * @param avoidZeroHop if true, zero-hop tunnels will be put last */ public TunnelInfoComparator(Hash target, boolean avoidZeroHop) { _base = target.getData(); _avoidZero = avoidZeroHop; }
public LocalHash(Hash h) { super(h.getData()); }
/** * @since 0.8.9 */ private static Integer getReverseKey(Hash hash) { byte[] hashBytes = hash.getData(); int i = (int) DataHelper.fromLong(hashBytes, 0, 4); return Integer.valueOf(i); }
/** not thread safe */ private HashComparator(Hash h) { _hash = h; tmp = new Hash(new byte[Hash.HASH_LENGTH]); data = new byte[2*Hash.HASH_LENGTH]; System.arraycopy(_hash.getData(), 0, data, Hash.HASH_LENGTH, Hash.HASH_LENGTH); }
/** * Implement a random, deterministic split into 4 groups that cannot be predicted by * others. * @return 0-3 */ private int getSubTier(Hash peer, Hash randomKey) { // input is first 64 bytes; output is last 32 bytes byte[] data = new byte[96]; System.arraycopy(peer.getData(), 0, data, 0, 32); System.arraycopy(randomKey.getData(), 0, data, 32, 32); _context.sha().calculateHash(data, 0, 64, data, 64); return data[64] & 0x03; }
/** * Generate a modified (yet consistent) hash from the origKey by generating the * SHA256 of the targetKey with the specified modData appended to it * * @throws IllegalArgumentException if origKey is null */ private static Hash getKey(Hash origKey, byte[] modData) { if (origKey == null) throw new IllegalArgumentException("Original key is null"); byte modVal[] = new byte[Hash.HASH_LENGTH + LENGTH]; System.arraycopy(origKey.getData(), 0, modVal, 0, Hash.HASH_LENGTH); System.arraycopy(modData, 0, modVal, Hash.HASH_LENGTH, LENGTH); return SHA256Generator.getInstance().calculateHash(modVal); }
public int compare(Hash l, Hash r) { System.arraycopy(l.getData(), 0, data, 0, Hash.HASH_LENGTH); byte[] tb = tmp.getData(); // don't use caching version of calculateHash() SHA256Generator.getInstance().calculateHash(data, 0, 2*Hash.HASH_LENGTH, tb, 0); BigInteger ll = HashDistance.getDistance(_hash, tmp); System.arraycopy(r.getData(), 0, data, 0, Hash.HASH_LENGTH); SHA256Generator.getInstance().calculateHash(data, 0, 2*Hash.HASH_LENGTH, tb, 0); BigInteger rr = HashDistance.getDistance(_hash, tmp); return ll.compareTo(rr); } }
/** * Creates 54-byte compact info * @throws IllegalArgumentException */ private void initialize() { if (port <= 0 || port >= 65535) throw new IllegalArgumentException("Bad port"); byte[] compactInfo = new byte[LENGTH]; System.arraycopy(nID.getData(), 0, compactInfo, 0, NID.HASH_LENGTH); System.arraycopy(hash.getData(), 0, compactInfo, NID.HASH_LENGTH, Hash.HASH_LENGTH); DataHelper.toLong(compactInfo, NID.HASH_LENGTH + Hash.HASH_LENGTH, 2, port); setData(compactInfo); }
/** * Verify the NID matches the Hash. * See generateNID() for requirements. * @throws IllegalArgumentException on mismatch */ private void verify() { if (!KRPC.SECURE_NID) return; byte[] nb = nID.getData(); byte[] hb = hash.getData(); if ((!DataHelper.eq(nb, 0, hb, 0, 4)) || ((nb[4] ^ (port >> 8)) & 0xff) != (hb[4] & 0xff) || ((nb[5] ^ port) & 0xff) != (hb[5] & 0xff)) throw new IllegalArgumentException("NID/Hash mismatch"); }
/** * For convenience. * @return "{52 chars}.b32.i2p" or null if fields not set. * @since 0.9.14 */ public String toBase32() { try { return Base32.encode(getHash().getData()) + ".b32.i2p"; } catch (IllegalStateException ise) { return null; } }
public PeerID(byte[] id, Destination address) { this.id = id; this.address = address; this.port = TrackerClient.PORT; this.destHash = address.calculateHash().getData(); hash = calculateHash(); util = null; }
/** * Generate a secure NID that matches the Hash and port. * Rules: First 4 bytes must match Hash. * Next 2 bytes must match Hash ^ port. * Remaining bytes may be random. * * @throws IllegalArgumentException */ public static NID generateNID(Hash h, int p, RandomSource random) { byte[] n = new byte[NID.HASH_LENGTH]; System.arraycopy(h.getData(), 0, n, 0, 6); n[4] ^= (byte) (p >> 8); n[5] ^= (byte) p; random.nextBytes(n, 6, NID.HASH_LENGTH - 6); return new NID(n); }
protected byte[] doWriteMessage() throws I2CPMessageException, IOException { if (_dest == null) { if (_hash == null) return new byte[0]; // null response allowed return _hash.getData(); } ByteArrayOutputStream os = new ByteArrayOutputStream(_dest.size()); try { _dest.writeBytes(os); } catch (DataFormatException dfe) { throw new I2CPMessageException("Error writing out the dest", dfe); } return os.toByteArray(); }
/** * PBE the passphrase with the salt. * Warning - SLOW */ public SessionKey generateSessionKey(byte salt[], byte passphrase[]) { byte salted[] = new byte[16+passphrase.length]; System.arraycopy(salt, 0, salted, 0, Math.min(salt.length, 16)); System.arraycopy(passphrase, 0, salted, 16, passphrase.length); byte h[] = _context.sha().calculateHash(salted).getData(); for (int i = 1; i < PBE_ROUNDS; i++) _context.sha().calculateHash(h, 0, Hash.HASH_LENGTH, h, 0); return new SessionKey(h); }
public int compare(PeerProfile left, PeerProfile right) { double lval = left.getSpeedValue(); double rval = right.getSpeedValue(); int rv = Double.compare(lval, rval); if (rv != 0) return rv; // we don't wan't to return 0 so profiles don't vanish in the TreeSet lval = left.getCapacityValue(); rval = right.getCapacityValue(); rv = Double.compare(lval, rval); if (rv != 0) return rv; return DataHelper.compareTo(right.getPeer().getData(), left.getPeer().getData()); } }
public void feedEntropy(String source, byte[] data, int offset, int len) { if ( (offset == 0) && (len == data.length) ) { setSeed(data); } else { setSeed(_context.sha().calculateHash(data, offset, len).getData()); } }
public OutboundEstablishState(RouterContext ctx, NTCPTransport transport, NTCPConnection con) { super(ctx, transport, con); _state = State.OB_INIT; ctx.sha().calculateHash(_X, 0, XY_SIZE, _hX_xor_bobIdentHash, 0); xor32(con.getRemotePeer().calculateHash().getData(), _hX_xor_bobIdentHash); // _prevEncrypted will be created later }
public void testBasicAES(){ SessionKey sessionKey = KeyGenerator.getInstance().generateSessionKey(); Hash h = SHA256Generator.getInstance().calculateHash(sessionKey.getData()); byte iv[] = new byte[16]; System.arraycopy(h.getData(), 0, iv, 0, 16); String msg = "Hello world01234012345678901234501234567890123450123456789012345"; h = SHA256Generator.getInstance().calculateHash(DataHelper.getASCII(msg)); byte aesEncr[] = new byte[DataHelper.getASCII(msg).length]; byte aesDecr[] = new byte[aesEncr.length]; _context.aes().encrypt(DataHelper.getASCII(msg), 0, aesEncr, 0, sessionKey, iv, aesEncr.length); _context.aes().decrypt(aesEncr, 0, aesDecr, 0, sessionKey, iv, aesEncr.length); h = SHA256Generator.getInstance().calculateHash(aesDecr); assertEquals(msg, new String(aesDecr)); }
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); }