protected void finishHeaders() throws IOException { out.write(DataHelper.getASCII("\r\n")); // end of the headers }
/** * Generate mod data from _cal. * Caller must synch. * @since 0.9.10 */ private byte[] generateModDataFromCal() { Date today = _cal.getTime(); String modVal = _fmt.format(today); if (modVal.length() != LENGTH) throw new IllegalStateException(); byte[] mod = DataHelper.getASCII(modVal); return mod; }
/** * 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); }
/** * Case-insensitive. * Does not allow trailing '='. * * @param s non-null * @return decoded data, null on error */ public static byte[] decode(String s) { return decode(DataHelper.getASCII(s)); }
@Override protected void finishHeaders() throws IOException { // TODO if browser supports gzip, send as gzip if (shouldCompress()) out.write(DataHelper.getASCII("Content-Encoding: x-i2p-gzip\r\n")); super.finishHeaders(); }
private boolean writeDest(String dest) { try { FileOutputStream fos = new FileOutputStream(_destFile); fos.write(DataHelper.getASCII(dest)); fos.close(); return true; } catch (Exception e) { _log.error("Error writing to " + _destFile, e); return false; } }
private static void testValidVersion(String host, int port) { _log.info("\n\nTesting valid version (should come back with an OK)\n\n\n"); try { Socket s = new Socket(host, port); OutputStream out = s.getOutputStream(); out.write(DataHelper.getASCII("HELLO VERSION MIN=1.0 MAX=1.0\n")); BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream())); String line = reader.readLine(); _log.info("line read for valid version: " + line); s.close(); } catch (Exception e) { _log.error("Error testing for valid version", e); } }
private static void testCorruptLine(String host, int port) { _log.info("\n\nTesting corrupt line (should come back with an error)\n\n\n"); try { Socket s = new Socket(host, port); OutputStream out = s.getOutputStream(); out.write(DataHelper.getASCII("HELLO h0 h0 h0\n")); BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream())); String line = reader.readLine(); _log.info("line read for valid version: " + line); s.close(); } catch (Exception e) { _log.error("Error testing for valid version", e); } }
private static void testInvalidVersion(String host, int port) { _log.info("\n\nTesting invalid version (should come back with an error)\n\n\n"); try { Socket s = new Socket(host, port); OutputStream out = s.getOutputStream(); out.write(DataHelper.getASCII("HELLO VERSION MIN=9.0 MAX=8.3\n")); BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream())); String line = reader.readLine(); _log.info("line read for invalid version: " + line); s.close(); } catch (Exception e) { _log.error("Error testing for valid version", e); } }
@Test public void testShortRead() throws Exception{ SigningPrivateKey signingPrivateKey = new SigningPrivateKey(); ByteArrayInputStream in = new ByteArrayInputStream(DataHelper.getASCII("short")); exception.expect(EOFException.class); exception.expectMessage("EOF after reading 5 bytes of " + SigningPrivateKey.KEYSIZE_BYTES + " byte value"); signingPrivateKey.readBytes(in); } }
@Test public void testShortRead() throws Exception{ PublicKey publicKey = new PublicKey(); ByteArrayInputStream in = new ByteArrayInputStream(DataHelper.getASCII("six times nine equals forty-two")); exception.expect(EOFException.class); exception.expectMessage("EOF after reading 31 bytes of " + PublicKey.KEYSIZE_BYTES + " byte value"); publicKey.readBytes(in); } }
@Test public void testShortRead() throws Exception{ SigningPublicKey publicKey = new SigningPublicKey(); ByteArrayInputStream in = new ByteArrayInputStream(DataHelper.getASCII("six times nine equals forty-two")); exception.expect(EOFException.class); exception.expectMessage("EOF after reading 31 bytes of " + SigningPublicKey.KEYSIZE_BYTES + " byte value"); publicKey.readBytes(in); } }
@Test public void testShortRead() throws Exception{ PrivateKey privateKey = new PrivateKey(); ByteArrayInputStream in = new ByteArrayInputStream(DataHelper.getASCII("six times nine equals forty-two")); exception.expect(EOFException.class); exception.expectMessage("EOF after reading 31 bytes of " + PrivateKey.KEYSIZE_BYTES + " byte value"); privateKey.readBytes(in); } }
@Test public void fromByteArrayThrowsOnError() throws Exception{ exception.expect(DataFormatException.class); exception.expectMessage("Error reading the byte array"); _struct.fromByteArray(DataHelper.getASCII("water is poison")); }
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)); }
/** * Check if the behaviour remains the same. */ public void testMultipleEquality(){ byte[] data = DataHelper.getASCII("blahblah"); Hash firstHash = SHA256Generator.getInstance().calculateHash(data); for(int i=0; i<5; i++){ Hash h = SHA256Generator.getInstance().calculateHash(data); assertEquals(firstHash, h); } }
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)); } }
public DataStructure createDataStructure() throws DataFormatException { Payload payload = new Payload(); SessionKey key = (SessionKey)(new SessionKeyTest()).createDataStructure(); byte data[] = DataHelper.getASCII("Hello, I2P"); // This causes equals() to fail unless we override the test // to set the unencrypted data after reading. // Unencrypted data is deprecated, just use encrypted data for the test. //payload.setUnencryptedData(data); Hash hash = (Hash)(new HashTest()).createDataStructure(); Destination target = (Destination)(new DestinationTest()).createDataStructure(); payload.setEncryptedData(data); return payload; } public DataStructure createStructureToRead() { return new Payload(); }
public void testNoSessions2() 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 2"); 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)); }
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)); }