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;
}
}
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();
}
}
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();
}
}
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();
}
}
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));
}
Aggregations