Search in sources :

Example 41 with I2PAppContext

use of net.i2p.I2PAppContext in project i2p.i2p by i2p.

the class I2PTunnel method destFromName.

/**
 *  @param i2cpHost may be null
 *  @param i2cpPort may be null
 *  @param user may be null
 *  @param pw may be null
 *  @since 0.9.11
 */
private static Destination destFromName(String name, String i2cpHost, String i2cpPort, boolean isSSL, String user, String pw) throws DataFormatException {
    if ((name == null) || (name.trim().length() <= 0))
        throw new DataFormatException("Empty destination provided");
    I2PAppContext ctx = I2PAppContext.getGlobalContext();
    Log log = ctx.logManager().getLog(I2PTunnel.class);
    if (name.startsWith("file:")) {
        Destination result = new Destination();
        byte[] content = null;
        FileInputStream in = null;
        try {
            in = new FileInputStream(name.substring("file:".length()));
            byte[] buf = new byte[1024];
            int read = DataHelper.read(in, buf);
            content = new byte[read];
            System.arraycopy(buf, 0, content, 0, read);
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
            return null;
        } finally {
            if (in != null)
                try {
                    in.close();
                } catch (IOException io) {
                }
        }
        try {
            result.fromByteArray(content);
            return result;
        } catch (RuntimeException ex) {
            if (log.shouldLog(Log.INFO))
                log.info("File is not a binary destination - trying base64");
            try {
                byte[] decoded = Base64.decode(new String(content));
                result.fromByteArray(decoded);
                return result;
            } catch (DataFormatException dfe) {
                if (log.shouldLog(Log.WARN))
                    log.warn("File is not a base64 destination either - failing!");
                return null;
            }
        }
    } else {
        // ask naming service
        name = name.trim();
        NamingService inst = ctx.namingService();
        boolean b32 = name.length() == 60 && name.toLowerCase(Locale.US).endsWith(".b32.i2p");
        Destination d = null;
        if (ctx.isRouterContext() || !b32) {
            // Local lookup.
            // Even though we could do b32 outside router ctx here,
            // we do it below instead so we can set the host and port,
            // which we can't do with lookup()
            d = inst.lookup(name);
            if (d != null || ctx.isRouterContext() || name.length() >= 516)
                return d;
        }
        // Outside router context only,
        // try simple session to ask the router.
        I2PClient client = new I2PSimpleClient();
        Properties opts = new Properties();
        if (i2cpHost != null)
            opts.put(I2PClient.PROP_TCP_HOST, i2cpHost);
        if (i2cpPort != null)
            opts.put(I2PClient.PROP_TCP_PORT, i2cpPort);
        opts.put("i2cp.SSL", Boolean.toString(isSSL));
        if (user != null)
            opts.put("i2cp.username", user);
        if (pw != null)
            opts.put("i2cp.password", pw);
        I2PSession session = null;
        try {
            session = client.createSession(null, opts);
            session.connect();
            d = session.lookupDest(name);
        } catch (I2PSessionException ise) {
            if (log.shouldLog(Log.WARN))
                log.warn("Lookup via router failed", ise);
        } finally {
            if (session != null) {
                try {
                    session.destroySession();
                } catch (I2PSessionException ise) {
                }
            }
        }
        return d;
    }
}
Also used : Destination(net.i2p.data.Destination) I2PAppContext(net.i2p.I2PAppContext) Log(net.i2p.util.Log) IOException(java.io.IOException) OrderedProperties(net.i2p.util.OrderedProperties) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) DataFormatException(net.i2p.data.DataFormatException) NamingService(net.i2p.client.naming.NamingService) I2PSimpleClient(net.i2p.client.I2PSimpleClient) I2PSession(net.i2p.client.I2PSession) I2PSessionException(net.i2p.client.I2PSessionException) I2PClient(net.i2p.client.I2PClient)

Example 42 with I2PAppContext

use of net.i2p.I2PAppContext in project i2p.i2p by i2p.

the class FortunaRandomSource method main.

/**
 *  Outputs to stdout for dieharder:
 *  <code>
 *  java -cp build/i2p.jar net.i2p.util.FortunaRandomSource | dieharder -a -g 200
 *  </code>
 */
public static void main(String[] args) {
    try {
        java.util.Properties props = new java.util.Properties();
        props.setProperty("prng.buffers", "12");
        I2PAppContext ctx = new I2PAppContext(props);
        RandomSource rand = ctx.random();
        byte[] buf = new byte[65536];
        while (true) {
            rand.nextBytes(buf);
            System.out.write(buf);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : I2PAppContext(net.i2p.I2PAppContext) IOException(java.io.IOException)

Example 43 with I2PAppContext

use of net.i2p.I2PAppContext in project i2p.i2p by i2p.

the class ElGamalTest method testYKGen.

public void testYKGen() {
    RandomSource.getInstance().nextBoolean();
    I2PAppContext context = I2PAppContext.getGlobalContext();
    YKGenerator ykgen = new YKGenerator(context);
    for (int i = 0; i < 5; i++) {
        ykgen.getNextYK();
    }
}
Also used : I2PAppContext(net.i2p.I2PAppContext)

Example 44 with I2PAppContext

use of net.i2p.I2PAppContext in project i2p.i2p by i2p.

the class KeyGeneratorTest method testKeyGen.

public void testKeyGen() {
    RandomSource.getInstance().nextBoolean();
    byte[] src = new byte[200];
    RandomSource.getInstance().nextBytes(src);
    I2PAppContext ctx = I2PAppContext.getGlobalContext();
    for (int i = 0; i < 10; i++) {
        Object[] keys = KeyGenerator.getInstance().generatePKIKeypair();
        byte[] ctext = ctx.elGamalEngine().encrypt(src, (PublicKey) keys[0]);
        byte[] ptext = ctx.elGamalEngine().decrypt(ctext, (PrivateKey) keys[1]);
        assertTrue(DataHelper.eq(ptext, src));
    }
    Object[] obj = KeyGenerator.getInstance().generateSigningKeypair();
    SigningPublicKey fake = (SigningPublicKey) obj[0];
    for (int i = 0; i < 10; i++) {
        Object[] keys = KeyGenerator.getInstance().generateSigningKeypair();
        Signature sig = DSAEngine.getInstance().sign(src, (SigningPrivateKey) keys[1]);
        assertTrue(DSAEngine.getInstance().verifySignature(sig, src, (SigningPublicKey) keys[0]));
        assertFalse(DSAEngine.getInstance().verifySignature(sig, src, fake));
    }
    for (int i = 0; i < 1000; i++) {
        KeyGenerator.getInstance().generateSessionKey();
    }
}
Also used : SigningPublicKey(net.i2p.data.SigningPublicKey) I2PAppContext(net.i2p.I2PAppContext) Signature(net.i2p.data.Signature)

Example 45 with I2PAppContext

use of net.i2p.I2PAppContext in project i2p.i2p by i2p.

the class AES256Test method testLong.

@SuppressWarnings("deprecation")
public void testLong() {
    I2PAppContext ctx = new I2PAppContext();
    SessionKey key = ctx.keyGenerator().generateSessionKey();
    byte[] iv = new byte[16];
    RandomSource.getInstance().nextBytes(iv);
    byte[] lbuf = new byte[1024];
    RandomSource.getInstance().nextBytes(lbuf);
    byte[] le = ctx.aes().safeEncrypt(lbuf, key, iv, 2048);
    byte[] ld = ctx.aes().safeDecrypt(le, key, iv);
    assertTrue(DataHelper.eq(ld, lbuf));
}
Also used : I2PAppContext(net.i2p.I2PAppContext) SessionKey(net.i2p.data.SessionKey)

Aggregations

I2PAppContext (net.i2p.I2PAppContext)55 SessionKey (net.i2p.data.SessionKey)13 File (java.io.File)11 IOException (java.io.IOException)11 Properties (java.util.Properties)9 Test (org.junit.Test)7 Getopt (gnu.getopt.Getopt)5 ArrayList (java.util.ArrayList)5 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3 InputStream (java.io.InputStream)3 DataFormatException (net.i2p.data.DataFormatException)3 Log (net.i2p.util.Log)3 SecureFileOutputStream (net.i2p.util.SecureFileOutputStream)3 Encoding (i2p.susi.webmail.encoding.Encoding)2 OutputStream (java.io.OutputStream)2 GeneralSecurityException (java.security.GeneralSecurityException)2 HashSet (java.util.HashSet)2 I2PSession (net.i2p.client.I2PSession)2 NamingService (net.i2p.client.naming.NamingService)2