public String getHost() { int addressType = this.header[3]; if (addressType != 3) return null; int namelen = (this.header[4] & 0xff); byte[] nameBytes = new byte[namelen]; System.arraycopy(this.header, 5, nameBytes, 0, namelen); return DataHelper.getUTF8(nameBytes); }
/** what IP Alice is reachable on */ public int readPort() { int offset = readBodyOffset() + NONCE_LENGTH; int size = _message[offset] & 0xff; offset++; offset += size; // skip the IP return (int)DataHelper.fromLong(_message, offset, 2); }
/** * This is different than InputStream.read(target), in that it * does repeated reads until the full data is received. * * As of 0.9.27, throws EOFException if the full length is not read. * * @return target.length * @throws EOFException if the full length is not read (since 0.9.27) */ public static int read(InputStream in, byte target[]) throws IOException { return read(in, target, 0, target.length); }
/** * As of 0.9.3, expiration MUST be all zeros as it is ignored on * readin and the signature will fail. */ public void writeBytes(OutputStream out) throws DataFormatException, IOException { if (_transportStyle == null) throw new DataFormatException("uninitialized"); out.write((byte) _cost); DataHelper.writeLong(out, 8, _expiration); DataHelper.writeString(out, _transportStyle); DataHelper.writeProperties(out, _options); }
/** * Read ahead to the infohash, throwing an exception if there isn't enough data. * Also check the first 20 bytes for the correct protocol here and throw IOE if bad, * so we don't hang waiting for 48 bytes if it's not a bittorrent client. * The 20 bytes are checked again in Peer.handshake(). */ private static byte[] readHash(InputStream in) throws IOException { for (int i = 0; i < PROTO_LEN; i++) { int b = in.read(); if (b != PROTO[i]) throw new IOException("Bad protocol 0x" + Integer.toHexString(b) + " at byte " + i); } DataHelper.skip(in, 8); byte buf[] = new byte[20]; int read = DataHelper.read(in, buf); if (read != buf.length) throw new IOException("Unable to read the hash (read " + read + ")"); return buf; }
/** * Overridden for efficiency. */ @Override public byte[] toByteArray() { byte[] rv = DataHelper.toLong(8, _date); rv[0] = (byte) ((_flags >> 8) & 0xff); rv[1] = (byte) (_flags & 0xff); return rv; }
@Test public void testRandomOrder() throws IOException { byte orig[] = new byte[256*1024]; _context.random().nextBytes(orig); ArrayList<Integer> order = new ArrayList<Integer>(32); for (int i = 0; i < orig.length / 1024; i++) order.add(new Integer(i)); Collections.shuffle(order); for (int i = 0; i < orig.length / 1024; i++) { byte msg[] = new byte[1024]; Integer cur = (Integer)order.get(i); System.arraycopy(orig, cur.intValue()*1024, msg, 0, 1024); in.messageReceived(cur.intValue(), new ByteArray(msg)); _log.debug("Injecting " + cur); } byte read[] = new byte[orig.length]; int howMany = DataHelper.read(in, read); if (howMany != orig.length) fail("not enough bytes read [" + howMany + "]"); if (!DataHelper.eq(orig, read)) fail("data read is not equal"); _log.info("Passed test: random order"); }
public void testVerifySelf(){ Object keypair[] = _context.keyGenerator().generatePKIKeypair(); PublicKey pub = (PublicKey)keypair[0]; PrivateKey priv = (PrivateKey)keypair[1]; for (int i = 0; i < UNENCRYPTED.length; i++) { byte orig[] = DataHelper.getASCII(UNENCRYPTED[i]); byte encrypted[] = _context.elGamalEngine().encrypt(orig, pub); byte decrypted[] = _context.elGamalEngine().decrypt(encrypted, priv); assertTrue(DataHelper.eq(decrypted, orig)); } }
/** @since 0.9.24 */ public void saveConfig() throws IOException { DataHelper.storeProps(i2cpProps, _configFile); } }
public static void loadProps(Properties props, InputStream inStr) throws IOException { loadProps(props, inStr, false); }
/** * Debug info, HTML formatted */ public void renderStatusHTML(StringBuilder buf) { buf.append("<b>DHT tracker:</b> ").append(_torrentCount).append(" torrents ") .append(_peerCount).append(" peers ") .append(DataHelper.formatDuration(_expireTime)).append(" expiration<br>"); }
/** * Make a dummy header from a dest, * for those cases where we want to receive unsolicited datagrams. * Unused for now. */ public SOCKSHeader(Destination dest) { this.header = new byte[beg.length + 60 + end.length]; System.arraycopy(beg, 0, this.header, 0, beg.length); String b32 = dest.toBase32(); System.arraycopy(DataHelper.getASCII(b32), 0, this.header, beg.length, 60); System.arraycopy(end, 0, this.header, beg.length + 60, end.length); }
public void writeBytes(OutputStream out) throws DataFormatException, IOException { if (_type < 0) throw new DataFormatException("Invalid certificate type: " + _type); //if ((_type != 0) && (_payload == null)) throw new DataFormatException("Payload is required for non null type"); out.write((byte) _type); if (_payload != null) { DataHelper.writeLong(out, 2, _payload.length); out.write(_payload); } else { DataHelper.writeLong(out, 2, 0L); } }
@Test public void testCompress() throws Exception{ Random r = new Random(); for (int size = 0; size < 32*1024; size+=32){ // Original had size++, changed value because // speed was a problem. -Comwiz byte data[] = new byte[size]; r.nextBytes(data); byte compressed[] = DataHelper.compress(data); byte decompressed[] = DataHelper.decompress(compressed); assertTrue(DataHelper.eq(data, decompressed)); } }
/** * This does NOT validate the signature * * @throws IllegalStateException if called more than once or Destination already set */ @Override public void readBytes(InputStream in) throws DataFormatException, IOException { if (_signingKey != null) throw new IllegalStateException(); // LS2 header readHeader(in); // Encrypted LS2 part int encryptedSize = (int) DataHelper.readLong(in, 2); if (encryptedSize < MIN_ENCRYPTED_SIZE || encryptedSize > MAX_ENCRYPTED_SIZE) throw new DataFormatException("bad LS size: " + encryptedSize); _encryptedData = new byte[encryptedSize]; DataHelper.read(in, _encryptedData); // signature type depends on offline or not SigType type = isOffline() ? _transientSigningPublicKey.getType() : _signingKey.getType(); _signature = new Signature(type); _signature.readBytes(in); }
@Test public void testLong() throws Exception{ byte[] temp = null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataHelper.writeLong(baos, 4, 42); temp = baos.toByteArray(); long l; ByteArrayInputStream bais = new ByteArrayInputStream(temp); l = DataHelper.readLong(bais, 4); assertEquals(42, l); } }
/** * Big endian. * * @param numBytes 1-8 * @param value non-negative * @return an array of length numBytes */ public static byte[] toLong(int numBytes, long value) throws IllegalArgumentException { byte val[] = new byte[numBytes]; toLong(val, 0, numBytes, value); return val; }
@Test public void testRandomDups() throws IOException { byte orig[] = new byte[256*1024]; _context.random().nextBytes(orig); for (int n = 0; n < 3; n++) { ArrayList<Integer> order = new ArrayList<Integer>(32); for (int i = 0; i < orig.length / 1024; i++) order.add(new Integer(i)); Collections.shuffle(order); for (int i = 0; i < orig.length / 1024; i++) { byte msg[] = new byte[1024]; Integer cur = (Integer)order.get(i); System.arraycopy(orig, cur.intValue()*1024, msg, 0, 1024); in.messageReceived(cur.intValue(), new ByteArray(msg)); _log.debug("Injecting " + cur); } } byte read[] = new byte[orig.length]; int howMany = DataHelper.read(in, read); if (howMany != orig.length) fail("not enough bytes read [" + howMany + "]"); if (!DataHelper.eq(orig, read)) fail("data read is not equal"); _log.info("Passed test: random dups"); }
public void testNoSessions1() throws Exception{ Object keys[] = KeyGenerator.getInstance().generatePKIKeypair(); PublicKey pubKey = (PublicKey)keys[0]; PrivateKey privKey = (PrivateKey)keys[1]; SessionKeyManager skm = new TransientSessionKeyManager(_context); SessionKey curKey = skm.createSession(pubKey); byte[] msg = DataHelper.getASCII("msg 1"); ElGamalAESEngine e = new ElGamalAESEngine(_context); byte emsg[] = e.encrypt(msg, pubKey, curKey, null, null, 64); byte dmsg[] = e.decrypt(emsg, privKey, skm); assertTrue(DataHelper.eq(dmsg, msg)); }