/** * Increases the counter by one, if not null. */ public static void inc(AccessCounter counter) { if (counter != null) counter.inc(); }
public static CustomMessage read(InputStream in, int length) throws IOException { AccessCounter counter = new AccessCounter(); return new CustomMessage(varString(in, counter), bytes(in, length - counter.length())); }
public BitmessageAddress(String address) { try { this.address = address; byte[] bytes = Base58.decode(address.substring(3)); ByteArrayInputStream in = new ByteArrayInputStream(bytes); AccessCounter counter = new AccessCounter(); this.version = varInt(in, counter); this.stream = varInt(in, counter); this.ripe = Bytes.expand(bytes(in, bytes.length - counter.length() - 4), 20); // test checksum byte[] checksum = Security.doubleSha512(bytes, bytes.length - 4); byte[] expectedChecksum = bytes(in, 4); for (int i = 0; i < 4; i++) { if (expectedChecksum[i] != checksum[i]) throw new IllegalArgumentException("Checksum of address failed"); } if (version < 4) { checksum = Security.sha512(Arrays.copyOfRange(bytes, 0, counter.length()), ripe); this.tag = null; this.publicDecryptionKey = Arrays.copyOfRange(checksum, 0, 32); } else { checksum = Security.doubleSha512(Arrays.copyOfRange(bytes, 0, counter.length()), ripe); this.tag = Arrays.copyOfRange(checksum, 32, 64); this.publicDecryptionKey = Arrays.copyOfRange(checksum, 0, 32); } } catch (IOException e) { throw new RuntimeException(e); } }
public BitmessageAddress(String address) { try { this.address = address; byte[] bytes = Base58.decode(address.substring(3)); ByteArrayInputStream in = new ByteArrayInputStream(bytes); AccessCounter counter = new AccessCounter(); this.version = varInt(in, counter); this.stream = varInt(in, counter); this.ripe = Bytes.expand(bytes(in, bytes.length - counter.length() - 4), 20); // test checksum byte[] checksum = cryptography().doubleSha512(bytes, bytes.length - 4); byte[] expectedChecksum = bytes(in, 4); for (int i = 0; i < 4; i++) { if (expectedChecksum[i] != checksum[i]) throw new IllegalArgumentException("Checksum of address failed"); } if (version < 4) { checksum = cryptography().sha512(Arrays.copyOfRange(bytes, 0, counter.length()), ripe); this.tag = null; this.publicDecryptionKey = Arrays.copyOfRange(checksum, 0, 32); } else { checksum = cryptography().doubleSha512(Arrays.copyOfRange(bytes, 0, counter.length()), ripe); this.tag = Arrays.copyOfRange(checksum, 32, 64); this.publicDecryptionKey = Arrays.copyOfRange(checksum, 0, 32); } } catch (IOException e) { throw new ApplicationException(e); } }
/** * Increases the counter by length, if not null. */ public static void inc(AccessCounter counter, int length) { if (counter != null) counter.inc(length); }
public static CryptoBox read(InputStream stream, int length) throws IOException { AccessCounter counter = new AccessCounter(); return new Builder() .IV(Decode.bytes(stream, 16, counter)) .curveType(Decode.uint16(stream, counter)) .X(Decode.shortVarBytes(stream, counter)) .Y(Decode.shortVarBytes(stream, counter)) .encrypted(Decode.bytes(stream, length - counter.length() - 32)) .MAC(Decode.bytes(stream, 32)) .build(); }
/** * Increases the counter by one, if not null. */ public static void inc(AccessCounter counter) { if (counter != null) counter.inc(); }
public static CryptoBox read(InputStream stream, int length) throws IOException { AccessCounter counter = new AccessCounter(); return new Builder() .IV(Decode.bytes(stream, 16, counter)) .curveType(Decode.uint16(stream, counter)) .X(Decode.shortVarBytes(stream, counter)) .Y(Decode.shortVarBytes(stream, counter)) .encrypted(Decode.bytes(stream, length - counter.length() - 32)) .MAC(Decode.bytes(stream, 32)) .build(); }
/** * Increases the counter by length, if not null. */ public static void inc(AccessCounter counter, int length) { if (counter != null) counter.inc(length); }
public static ObjectMessage readObject(InputStream in, int length) throws IOException { AccessCounter counter = new AccessCounter(); byte nonce[] = Decode.bytes(in, 8, counter); long expiresTime = Decode.int64(in, counter); long objectType = Decode.uint32(in, counter); long version = Decode.varInt(in, counter); long stream = Decode.varInt(in, counter); byte[] data = Decode.bytes(in, length - counter.length()); ObjectPayload payload; try { ByteArrayInputStream dataStream = new ByteArrayInputStream(data); payload = Factory.getObjectPayload(objectType, version, stream, dataStream, data.length); } catch (Exception e) { LOG.trace("Could not parse object payload - using generic payload instead", e); payload = new GenericPayload(version, stream, data); } return new ObjectMessage.Builder() .nonce(nonce) .expiresTime(expiresTime) .objectType(objectType) .stream(stream) .payload(payload) .build(); }
public static void int8(long value, OutputStream stream, AccessCounter counter) throws IOException { stream.write((int) value); inc(counter); }
public static ObjectMessage readObject(InputStream in, int length) throws IOException { AccessCounter counter = new AccessCounter(); byte nonce[] = Decode.bytes(in, 8, counter); long expiresTime = Decode.int64(in, counter); long objectType = Decode.uint32(in, counter); long version = Decode.varInt(in, counter); long stream = Decode.varInt(in, counter); byte[] data = Decode.bytes(in, length - counter.length()); ObjectPayload payload; try { ByteArrayInputStream dataStream = new ByteArrayInputStream(data); payload = Factory.getObjectPayload(objectType, version, stream, dataStream, data.length); } catch (IOException e) { LOG.trace("Could not parse object payload - using generic payload instead", e); payload = new GenericPayload(version, stream, data); } return new ObjectMessage.Builder() .nonce(nonce) .expiresTime(expiresTime) .objectType(objectType) .stream(stream) .payload(payload) .build(); }
public static void int8(long value, OutputStream stream, AccessCounter counter) throws IOException { stream.write((int) value); inc(counter); }
public static int uint16(InputStream in, AccessCounter counter) throws IOException { inc(counter, 2); return in.read() << 8 | in.read(); }
public static byte[] bytes(InputStream in, int count, AccessCounter counter) throws IOException { byte[] result = new byte[count]; int off = 0; while (off < count) { int read = in.read(result, off, count - off); if (read < 0) { throw new IOException("Unexpected end of stream, wanted to read " + count + " bytes but only got " + off); } off += read; } inc(counter, count); return result; }
public static byte[] bytes(InputStream stream, int count, AccessCounter counter) throws IOException { byte[] result = new byte[count]; int off = 0; while (off < count) { int read = stream.read(result, off, count - off); if (read < 0) { throw new IOException("Unexpected end of stream, wanted to read " + count + " bytes but only got " + off); } off += read; } inc(counter, count); return result; }
public static int uint16(InputStream stream, AccessCounter counter) throws IOException { inc(counter, 2); return stream.read() * 256 + stream.read(); }
public static void int32(long value, OutputStream stream, AccessCounter counter) throws IOException { stream.write(ByteBuffer.allocate(4).putInt((int) value).array()); inc(counter, 4); }
public static void int16(long value, OutputStream stream, AccessCounter counter) throws IOException { stream.write(ByteBuffer.allocate(2).putShort((short) value).array()); inc(counter, 2); }
public static void int64(long value, OutputStream stream, AccessCounter counter) throws IOException { stream.write(ByteBuffer.allocate(8).putLong(value).array()); inc(counter, 8); }