use of net.i2p.I2PAppContext in project i2p.i2p by i2p.
the class ConnectCloseIT method test.
@Test
public void test() throws Exception {
I2PAppContext context = I2PAppContext.getGlobalContext();
_log = context.logManager().getLog(ConnectCloseIT.class);
_log.debug("creating server session");
_server = createSession();
_log.debug("running server");
runServer(context, _server);
_log.debug("running client");
runClient(context, createSession());
}
use of net.i2p.I2PAppContext in project i2p.i2p by i2p.
the class UPnP method main.
/**
* Dumps out device info in semi-HTML format
*/
public static void main(String[] args) throws Exception {
Properties props = new Properties();
props.setProperty(PROP_ADVANCED, "true");
I2PAppContext ctx = new I2PAppContext(props);
UPnP cp = new UPnP(ctx);
org.cybergarage.upnp.UPnP.setEnable(org.cybergarage.upnp.UPnP.USE_ONLY_IPV4_ADDR);
Debug.initialize(ctx);
cp.setHTTPPort(49152 + ctx.random().nextInt(5000));
cp.setSSDPPort(54152 + ctx.random().nextInt(5000));
long start = System.currentTimeMillis();
cp.start();
long s2 = System.currentTimeMillis();
System.err.println("Start took " + (s2 - start) + "ms");
System.err.println("Searching for UPnP devices");
start = System.currentTimeMillis();
cp.search();
s2 = System.currentTimeMillis();
System.err.println("Search kickoff took " + (s2 - start) + "ms");
System.err.println("Waiting 10 seconds for responses");
Thread.sleep(10000);
DeviceList list = cp.getDeviceList();
if (list.isEmpty()) {
System.err.println("No UPnP devices found");
System.exit(1);
}
System.err.println("Found " + list.size() + " devices.");
System.err.println("Redirect the following output to an html file and view in a browser.");
StringBuilder sb = new StringBuilder();
Iterator<Device> it = list.iterator();
int i = 0;
while (it.hasNext()) {
Device device = it.next();
cp.listSubDev(device.toString(), device, sb);
System.out.println("<h3>Device " + (++i) + ": " + DataHelper.escapeHTML(device.getFriendlyName()) + "</h3>");
System.out.println("<p>UDN: " + DataHelper.escapeHTML(device.getUDN()));
System.out.println("<br>IP: " + getIP(device));
System.out.println(sb.toString());
sb.setLength(0);
}
System.exit(0);
}
use of net.i2p.I2PAppContext in project i2p.i2p by i2p.
the class DHSessionKeyBuilderTest method testDHSessionKeyBuilder.
public void testDHSessionKeyBuilder() {
I2PAppContext ctx = new I2PAppContext();
for (int i = 0; i < 5; i++) {
DHSessionKeyBuilder builder1 = new DHSessionKeyBuilder();
DHSessionKeyBuilder builder2 = new DHSessionKeyBuilder();
BigInteger pub1 = builder1.getMyPublicValue();
BigInteger pub2 = builder2.getMyPublicValue();
try {
builder2.setPeerPublicValue(pub1);
builder1.setPeerPublicValue(pub2);
} catch (DHSessionKeyBuilder.InvalidPublicParameterException ippe) {
assertTrue(ippe.getMessage(), true);
}
SessionKey key1 = builder1.getSessionKey();
SessionKey key2 = builder2.getSessionKey();
assertEquals(key1, key2);
byte[] iv = new byte[16];
RandomSource.getInstance().nextBytes(iv);
// 16 bytes max using AESEngine
String origVal = "1234567890123456";
byte[] enc = new byte[16];
byte[] dec = new byte[16];
ctx.aes().encrypt(DataHelper.getASCII(origVal), 0, enc, 0, key1, iv, 16);
ctx.aes().decrypt(enc, 0, dec, 0, key2, iv, 16);
String tranVal = new String(dec);
assertEquals(origVal, tranVal);
}
}
use of net.i2p.I2PAppContext in project i2p.i2p-bote by i2p.
the class Util method encrypt.
/**
* Encrypts data with an I2P public key
*/
public static byte[] encrypt(byte[] data, PublicKey key) {
I2PAppContext appContext = I2PAppContext.getGlobalContext();
SessionKeyManager sessionKeyMgr = new net.i2p.crypto.SessionKeyManager(appContext) {
};
SessionKey sessionKey = sessionKeyMgr.createSession(key);
return appContext.elGamalAESEngine().encrypt(data, key, sessionKey, null, null, null, 0);
}
use of net.i2p.I2PAppContext in project i2p.i2p-bote by i2p.
the class Encrypt method getDerivedKey.
private DerivedKey getDerivedKey(byte[] password, File inputFile) throws GeneralSecurityException {
if (derivParamsFile == null) {
// the derivation parameters file is in the I2P-Bote root dir, so search all parent directories
File parentDir = inputFile.getAbsoluteFile().getParentFile();
while (derivParamsFile == null && parentDir != null) {
boolean paramsFileFound = Util.contains(parentDir, Configuration.KEY_DERIVATION_PARAMETERS_FILE);
if (paramsFileFound)
derivParamsFile = new File(parentDir, Configuration.KEY_DERIVATION_PARAMETERS_FILE);
else
parentDir = parentDir.getParentFile();
}
}
if (derivParamsFile != null) {
if (verbose)
System.out.println("Using derivation parameters file: " + derivParamsFile.getAbsolutePath());
try {
return FileEncryptionUtil.getEncryptionKey(password, derivParamsFile);
} catch (IOException e) {
System.out.println("Can't create key from derivation parameters file: " + e.getLocalizedMessage());
}
}
// derivation parameters file not found or not readable, use default params and random salt
if (verbose)
System.out.println("No derivation parameters file available, creating new salt.");
I2PAppContext appContext = I2PAppContext.getGlobalContext();
byte[] salt = new byte[SALT_LENGTH];
appContext.random().nextBytes(salt);
byte[] key = FileEncryptionUtil.getEncryptionKey(password, salt, FileEncryptionConstants.KDF_PARAMETERS);
return new DerivedKey(salt, FileEncryptionConstants.KDF_PARAMETERS, key);
}
Aggregations