/** * * @param messageIdString Must be a 44-character Base64-encoded string. */ public void setMessageID(String messageIdString) { this.messageId = new UniqueId(messageIdString); }
protected CommunicationPacket() { this(new UniqueId()); }
/** * Creates an <code>UnencryptedEmailPacket</code> using data from an <code>InputStream</code> * for the content. The number of bytes read is limited so the byte array representation of the * packet does not exceed <code>maxPacketSize</code>. * @throws IOException */ public UnencryptedEmailPacket(InputStream inputStream, UniqueId messageId, int fragmentIndex, int maxPacketSize) throws IOException { this.messageId = messageId; delAuthorization = new UniqueId(); this.fragmentIndex = fragmentIndex; // read content maxPacketSize -= OVERHEAD; content = new byte[maxPacketSize]; int bytesRead = inputStream.read(content); if (bytesRead < maxPacketSize) content = Arrays.copyOf(content, bytesRead); }
/** * @param includeSendTime Whether to add a "Date" header. */ public Email(boolean includeSendTime) { super(Session.getDefaultInstance(new Properties())); messageId = new UniqueId(); metadata = new EmailMetadata(); this.includeSendTime = includeSendTime; }
/** * Creates an <code>Email</code> from a byte array containing a <strong>compressed</strong> MIME email. * @param bytes * @throws MessagingException * @throws IOException */ public Email(byte[] bytes) throws MessagingException, IOException { this(new ByteArrayInputStream(bytes), true); messageId = new UniqueId(); metadata = new EmailMetadata(); }
/** * Creates an <code>UnencryptedEmailPacket</code> from a <code>byte</code> array that contains MIME data. * @param data */ public UnencryptedEmailPacket(byte[] data) { super(data); ByteBuffer buffer = ByteBuffer.wrap(data, HEADER_LENGTH, data.length-HEADER_LENGTH); messageId = new UniqueId(buffer); delAuthorization = new UniqueId(buffer); fragmentIndex = buffer.getShort() & 0xFFFF; numFragments = buffer.getShort() & 0xFFFF; int contentLength = buffer.getShort() & 0xFFFF; content = new byte[contentLength]; buffer.get(content); verify(); }
public EmailPacketDeleteRequest(byte[] data) { super(data); ByteBuffer buffer = ByteBuffer.wrap(data, HEADER_LENGTH, data.length-HEADER_LENGTH); dhtKey = readHash(buffer); authorization = new UniqueId(buffer); if (buffer.hasRemaining()) log.debug("Email Packet Delete Request has " + buffer.remaining() + " extra bytes."); }
public IndexPacketDeleteRequest(byte[] data) { super(data); ByteBuffer buffer = ByteBuffer.wrap(data, HEADER_LENGTH, data.length-HEADER_LENGTH); emailDestHash = readHash(buffer); entries = new HashMap<Hash, UniqueId>(); int numEntries = buffer.getShort() & 0xFFFF; for (int i=0; i<numEntries; i++) { Hash dhtKey = readHash(buffer); UniqueId delAuthorization = new UniqueId(buffer); entries.put(dhtKey, delAuthorization); } if (buffer.hasRemaining()) log.debug("Index Packet Delete Request has " + buffer.remaining() + " extra bytes."); }
/** * Creates an <code>Email</code> from an InputStream containing a compressed or uncompressed MIME email. * @param inputStream * @param compressed <code>true</code> if the stream contains compressed data * @throws MessagingException * @throws IOException */ public Email(InputStream inputStream, boolean compressed) throws MessagingException, IOException { super(Session.getDefaultInstance(new Properties()), compressed?Email.decompress(inputStream):inputStream); messageId = new UniqueId(); includeSendTime = getSentDate() != null; metadata = new EmailMetadata(); }
/** * Creates a <code>CommunicationPacket</code> from raw datagram data. The packet ID * is initialized from the input data.<br/> * Subclasses start reading at byte {@link #HEADER_LENGTH} after calling this constructor. * @param data */ protected CommunicationPacket(byte[] data) { super(data[5]); // byte 5 is the protocol version in a communication packet if (!isPrefixValid(data)) { byte[] prefix = Arrays.copyOf(data, PACKET_PREFIX.length); log.error("Packet prefix invalid. Expected: " + Arrays.toString(PACKET_PREFIX) + ", actual: " + Arrays.toString(prefix)); } checkPacketType(data[4]); packetId = new UniqueId(data, 6); sentSignal = new CountDownLatch(1); sentTime = -1; }
private EncryptedEmailPacket makeEmailPacket(String message) throws GeneralSecurityException, I2PSessionException, IOException { byte[] content = message.getBytes(); byte[] messageIdBytes = new byte[] {6, -32, -23, 17, 55, 15, -45, -19, 91, 100, -76, -76, 118, -118, -53, -109, -108, 113, -112, 81, 117, 9, -126, 20, 0, -83, -89, 7, 48, 76, -58, 83}; UniqueId messageId = new UniqueId(messageIdBytes, 0); int fragmentIndex = 0; UnencryptedEmailPacket plaintextPacket = new UnencryptedEmailPacket(new ByteArrayInputStream(content), messageId, fragmentIndex, I2PBotePacket.MAX_DATAGRAM_SIZE); plaintextPacket.setNumFragments(1); return new EncryptedEmailPacket(plaintextPacket, identity); }
EncryptedEmailPacket createEmailPacket(EmailDestination dest, String message) throws Exception { byte[] content = message.getBytes(); byte[] messageIdBytes = new byte[] {-69, -24, -109, 1, 69, -122, -69, 113, -68, -90, 55, -28, 105, 97, 125, 70, 51, 58, 14, 2, -13, -53, 90, -29, 36, 67, 36, -94, -108, -125, 11, 123}; UniqueId messageId = new UniqueId(messageIdBytes, 0); int fragmentIndex = 0; UnencryptedEmailPacket plaintextPacket = new UnencryptedEmailPacket(new ByteArrayInputStream(content), messageId, fragmentIndex, I2PBotePacket.MAX_DATAGRAM_SIZE); plaintextPacket.setNumFragments(1); return new EncryptedEmailPacket(plaintextPacket, dest); }
@Before public void setUp() throws Exception { String message = "This is a test message. Test 1 2 3 Test"; byte[] content = message.getBytes(); byte[] messageIdBytes = new byte[] {-69, -24, -109, 1, 69, -122, -69, 113, -68, -90, 55, -28, 105, 97, 125, 70, 51, 58, 14, 2, -13, -53, 90, -29, 36, 67, 36, -94, -108, -125, 11, 123}; UniqueId messageId = new UniqueId(messageIdBytes, 0); int fragmentIndex = 0; packet = new UnencryptedEmailPacket(new ByteArrayInputStream(content), messageId, fragmentIndex, I2PBotePacket.MAX_DATAGRAM_SIZE); packet.setNumFragments(1); }
@Before public void setUp() throws Exception { byte[] packetIdBytes = new byte[] {120, 120, -8, -88, 21, 126, 46, -61, 18, -101, 15, 53, 20, -44, -112, 42, 86, -117, 30, -96, -66, 33, 71, -55, -102, -78, 78, -82, -105, 66, -116, 43}; UniqueId packetId = new UniqueId(packetIdBytes, 0); Collection<ResponsePacket> responsePackets = ResponsePacket.create(createDataPacket(), StatusCode.OK, packetId); assertEquals(1, responsePackets.size()); responsePacket = responsePackets.iterator().next(); }
private DataPacket createDataPacket() throws GeneralSecurityException, IOException { byte[] dataToStore = new byte[] {2, 109, -80, -37, -106, 83, -33, -39, -94, -76, -112, -98, 99, 25, -61, 44, -92, -85, 1, 10, -128, -2, -27, -86, -126, -33, -11, 42, 56, 3, -97, -101, 111, 7, -96, 25, 121, 74, 89, -40, -33, 82, -50, -18, 49, 106, 13, -121, 53, -83, -2, 35, -7, 71, -71, 26, 90, 1}; byte[] messageIdBytes = new byte[] {-69, -24, -109, 1, 69, -122, -69, 113, -68, -90, 55, -28, 105, 97, 125, 70, 51, 58, 14, 2, -13, -53, 90, -29, 36, 67, 36, -94, -108, -125, 11, 123}; UniqueId messageId = new UniqueId(messageIdBytes, 0); int fragmentIndex = 0; UnencryptedEmailPacket unencryptedPacket = new UnencryptedEmailPacket(new ByteArrayInputStream(dataToStore), messageId, fragmentIndex, I2PBotePacket.MAX_DATAGRAM_SIZE); unencryptedPacket.setNumFragments(1); EmailDestination destination = new EmailDestination("0XuJjhgp58aOhvHHgpaxoQYsCUfDS6BECMEoVxFGEFPdk3y8lbzIsq9eUyeizFleMacYwoscCir8nQLlW34lxfRmirkNpD9vU1XnmjnZ5hGdnor1qIDqz3KJ040dVQ617MwyG97xxYLT0FsH907vBXgdc4RCHwKd1~9siagA5CSMaA~wM8ymKXLypiZGYexENLmim7nMzJTQYoOM~fVS99UaGJleDBN3pgZ2EvRYDQV2VqKH7Gee07R3y7b~c0tAKVHS0IbPQfTVJigrIHjTl~ZczxpaeTM04T8IgxKnO~lSmR1w7Ik8TpEkETwT9PDwUqQsjmlSY8E~WwwGMRJVyIRZUkHeRZ0aFq7us8W9EKzYtjjiU1z0QFpZrTfJE8oqCbnH5Lqv5Q86UdTPpriJC1N99E77TpCTnNzcBnpp6ko2JCy2IJUveaigKxS6EmS9KarkkkBRsckOKZZ6UNTOqPZsBCsx0Q9WvDF-Uc3dtouXWyenxRptaQsdkZyYlEQv"); return new EncryptedEmailPacket(unencryptedPacket, destination); }
@Before public void setUp() throws Exception { File tempDir = new File(System.getProperty("java.io.tmpdir")); testDir = new File(tempDir, "EmailPacketFolderTest-" + System.currentTimeMillis()); folderDir = new File(testDir, "dht_email_pkt"); packetFolder = new EmailPacketFolder(folderDir); // make two UnencryptedEmailPackets with different contents byte[] content1 = "test TEST test ABCDEFGH asdfsadfsadf 3487562384".getBytes(); byte[] content2 = "fdlkhgjfljh test 123456".getBytes(); byte[] messageIdBytes = new byte[] {-69, -24, -109, 1, 69, -122, -69, 113, -68, -90, 55, -28, 105, 97, 125, 70, 51, 58, 14, 2, -13, -53, 90, -29, 36, 67, 36, -94, -108, -125, 11, 123}; UniqueId messageId = new UniqueId(messageIdBytes, 0); int fragmentIndex = 0; unencryptedPacket = new UnencryptedEmailPacket(new ByteArrayInputStream(content1), messageId, fragmentIndex, I2PBotePacket.MAX_DATAGRAM_SIZE); unencryptedPacket.setNumFragments(1); unencryptedPacket2 = new UnencryptedEmailPacket(new ByteArrayInputStream(content2), messageId, fragmentIndex, I2PBotePacket.MAX_DATAGRAM_SIZE); unencryptedPacket.setNumFragments(1); String base64Dest = "m-5~1dZ0MrGdyAWu-C2ecNAB5LCCsHQpeSfjn-r~mqMfNvroR98~BRmReUDmb0la-r-pBHLMtflrJE7aTrGwDTBm5~AJFEm-9SJPZnyGs-ed5pOj4Db65yJml1y1n77qr1~mM4GITl6KuIoxg8YwvPrCIlXe2hiiDCoC-uY9-np9UY"; recipient = new EmailDestination(base64Dest); emailPacket = new EncryptedEmailPacket(unencryptedPacket, recipient); sender = new Destination("X3oKYQJ~1EAz7B1ZYGSrOTIMCW5Rnn2Svoc38dx5D9~zvz8vqiWcH-pCqQDwLgPWl9RTBzHtTmZcGRPXIv54i0XWeUfX6rTPDQGuZsnBMM0xrkH2FNLNFaJa0NgW3uKXWpNj9AI1AXUXzK-2MYTYoaZHx5SBoCaKfAGMcFJvTON1~kopxBxdBF9Q7T4~PJ3I2LeU-ycmUlehe9N9bIu7adUGyPGVl8Ka-UxwQromoJ~vSWHHl8HkwcDkW--v9Aj~wvFqxqriFkB1EeBiThi3V4XtVY~GUP4IkRj9YZGTsSBf3eS4xwXgnYWlB7IvxAGBfHY9MCg3lbAa1Dg~1IH6rhtXxsXUtGcXsz9yMZTxXHd~rGo~JrXeM1y~Vcenpr6tJcum6pxevkKzzT0qDegGPH3Zhqz7sSeeIaJEcPBUAkX89csqyFWFIjTMm6yZp2rW-QYUnVNLNTjf7vndYUAEICogAkq~btqpIzrGEpm3Pr9F23br3SpbOmdxQxg51AMmAAAA"); }
@Before public void setUp() throws Exception { Hash dhtKey = new Hash(new byte[] {-48, 78, 66, 58, -79, 87, 38, -103, -60, -27, 108, 55, 117, 37, -99, 93, -23, -102, -83, 20, 44, -80, 65, 89, -68, -73, 69, 51, 115, 79, 24, 127}); byte[] packetIdBytes = new byte[] {120, 120, -8, -88, 21, 126, 46, -61, 18, -101, 15, 53, 20, -44, -112, 42, 86, -117, 30, -96, -66, 33, 71, -55, -102, -78, 78, -82, -105, 66, -116, 43}; UniqueId deleteAuthorization = new UniqueId(packetIdBytes, 0); delRequest = new EmailPacketDeleteRequest(dhtKey, deleteAuthorization); }
@Before public void setUp() throws Exception { Hash emailDestHash = new Hash(new byte[] {-48, 78, 66, 58, -79, 87, 38, -103, -60, -27, 108, 55, 117, 37, -99, 93, -23, -102, -83, 20, 44, -80, 65, 89, -68, -73, 69, 51, 115, 79, 24, 127}); delRequest = new IndexPacketDeleteRequest(emailDestHash); dhtKeys = new Hash[3]; delKeys = new UniqueId[3]; dhtKeys[0] = new Hash(new byte[] {120, 120, -8, -88, 21, 126, 46, -61, 18, -101, 15, 53, 20, -44, -112, 42, 86, -117, 30, -96, -66, 33, 71, -55, -102, -78, 78, -82, -105, 66, -116, 43}); delKeys[0] = new UniqueId(new byte[] {-62, -112, 99, -65, 13, 44, -117, -111, 96, 45, -6, 64, 78, 57, 117, 103, -24, 101, 106, -116, -18, 62, 99, -49, 60, -81, 8, 64, 27, -41, -104, 58}, 0); delRequest.put(dhtKeys[0], delKeys[0]); dhtKeys[1] = new Hash(new byte[] {-69, -24, -109, 1, 69, -122, -69, 113, -68, -90, 55, -28, 105, 97, 125, 70, 51, 58, 14, 2, -13, -53, 90, -29, 36, 67, 36, -94, -108, -125, 11, 123}); delKeys[1] = new UniqueId(new byte[] {-16, 67, 107, 80, 27, 65, 81, 71, 61, 70, -72, 126, 64, -10, 57, -128, 111, -107, -42, 24, -90, -4, -46, 63, 7, -6, 43, 76, -9, -81, 8, -68}, 0); delRequest.put(dhtKeys[1], delKeys[1]); dhtKeys[2] = new Hash(new byte[] {-37, -8, 37, 82, -40, -34, 68, -51, -16, 74, 27, 89, 113, -15, 112, 69, 92, 102, 62, 111, 99, -27, -42, -71, 6, 38, 106, 121, 21, -72, -83, 3}); delKeys[2] = new UniqueId(new byte[] {6, -32, -23, 17, 55, 15, -45, -19, 91, 100, -76, -76, 118, -118, -53, -109, -108, 113, -112, 81, 117, 9, -126, 20, 0, -83, -89, 7, 48, 76, -58, 83}, 0); delRequest.put(dhtKeys[2], delKeys[2]); }
@Before public void setUp() throws Exception { byte[] emailContent = new byte[] {2, 109, -80, -37, -106, 83, -33, -39, -94, -76, -112, -98, 99, 25, -61, 44, -92, -85, 1, 10, -128, -2, -27, -86, -126, -33, -11, 42, 56, 3, -97, -101, 111, 7, -96, 25, 121, 74, 89, -40, -33, 82, -50, -18, 49, 106, 13, -121, 53, -83, -2, 35, -7, 71, -71, 26, 90, 1}; UniqueId messageId = new UniqueId(new byte[] {-69, -24, -109, 1, 69, -122, -69, 113, -68, -90, 55, -28, 105, 97, 125, 70, 51, 58, 14, 2, -13, -53, 90, -29, 36, 67, 36, -94, -108, -125, 11, 123}, 0); String base64Identity = "piYT1uJ3O8~bBPZmTvehMbp3-Zksg5enhvIlp2X8txqL25l0WdQMWwyt30UAOVQqxGdnMPTqqjh~-zoa~rCQORo~J1gRxLwCX9LlHQqaIimJilrbN-rhKy4Xlft054wbgQjLSC-WICE4W64KDfitwRzdr7lV6lz~0KFiZ8erZ-~WPMG1CgWEku9lILQUdUHyFBguPcK9oPDq7oGBuFGy8w0CvAq7ex3nmbL7zQVA~VqILtOGeGK2fidCuuofj4AQsTcXmH9O0nxZGCIJBhf~4EWmazvxu8XVB8pabNQvRDbmFu6q85JTwmxC45lCjqNw30hp8q2zoqP-zchjWOrxFUhSumpBdD0xXJR~qmhejh4WnuRnnam9j3fcxH5i~T7xWgmvIbpZEI4kyc9VEbXbLI7k-bU2A6sdP-AGt5~TjGLcxpdsPnOLRXO-Dsi7E9-3Kc84s4TmdpEJdtHn1dxYyeeT-ysVOqXjv5w5Cuk0XJpUIJG8n7aXHpNb-QLxPD3yAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADWF3qnAX-p41Po~VNmOUzS-Yt~noD8-e~L3P5rZXBWf-XtB4hkloo6m1jwqphEdf1"; EmailIdentity identity = new EmailIdentity(base64Identity); EmailDestination destination = new EmailDestination(identity.getKey()); int fragmentIndex = 0; UnencryptedEmailPacket emailPacket = new UnencryptedEmailPacket(new ByteArrayInputStream(emailContent), messageId, fragmentIndex, I2PBotePacket.MAX_DATAGRAM_SIZE); emailPacket.setNumFragments(1); dhtPacket = new EncryptedEmailPacket(emailPacket, destination); storeRequest = new StoreRequest(dhtPacket); }
/** Tests processing of one valid and one invalid {@link EmailPacketDeleteRequest}. */ @Test public void testPacketReceived() throws PasswordException { packetFolder.store(emailPacket); assertEquals(1, packetFolder.getElements().size()); EmailPacketDeleteRequest delRequest; // send an invalid delete request byte[] delAuthBytes = unencryptedPacket.getDeleteAuthorization().toByteArray().clone(); delAuthBytes[5] ^= 1; UniqueId invalidAuthorization = new UniqueId(delAuthBytes, 0); delRequest = new EmailPacketDeleteRequest(emailPacket.getDhtKey(), invalidAuthorization); packetFolder.packetReceived(delRequest, sender, System.currentTimeMillis()); assertEquals(1, packetFolder.getElements().size()); // send a valid delete request delRequest = new EmailPacketDeleteRequest(emailPacket.getDhtKey(), unencryptedPacket.getDeleteAuthorization()); packetFolder.packetReceived(delRequest, sender, System.currentTimeMillis()); assertEquals(0, packetFolder.getElements().size()); }