use of net.i2p.data.SessionKey in project i2p.i2p by i2p.
the class ElGamalTest method testLoop.
public void testLoop() {
for (int i = 0; i < 5; i++) {
Object[] keys = KeyGenerator.getInstance().generatePKIKeypair();
PublicKey pubKey = (PublicKey) keys[0];
PrivateKey privKey = (PrivateKey) keys[1];
byte[] msg = new byte[400];
RandomSource.getInstance().nextBytes(msg);
SessionKey key = _context.sessionKeyManager().getCurrentKey(pubKey);
if (key == null)
key = _context.sessionKeyManager().createSession(pubKey);
byte[] encrypted = _context.elGamalAESEngine().encrypt(msg, pubKey, key, null, null, 1024);
byte[] decrypted = null;
try {
decrypted = _context.elGamalAESEngine().decrypt(encrypted, privKey, _context.sessionKeyManager());
} catch (DataFormatException dfe) {
dfe.printStackTrace();
fail();
}
assertTrue(DataHelper.eq(msg, decrypted));
}
}
use of net.i2p.data.SessionKey in project i2p.i2p by i2p.
the class HMACSHA256Bench method runTest.
private static void runTest(I2PAppContext ctx) {
SessionKey key = ctx.keyGenerator().generateSessionKey();
byte[] output = new byte[32];
ctx.hmac().calculate(key, "qwerty".getBytes(), 0, 6, output, 0);
int times = 100000;
long shorttime = 0;
long medtime = 0;
long longtime = 0;
long minShort = 0;
long maxShort = 0;
long minMed = 0;
long maxMed = 0;
long minLong = 0;
long maxLong = 0;
long shorttime1 = 0;
long medtime1 = 0;
long longtime1 = 0;
long minShort1 = 0;
long maxShort1 = 0;
long minMed1 = 0;
long maxMed1 = 0;
long minLong1 = 0;
long maxLong1 = 0;
byte[] smess = "abc".getBytes();
StringBuilder buf = new StringBuilder();
for (int x = 0; x < 2 * 1024; x++) {
buf.append("a");
}
// new String("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq").getBytes();
byte[] mmess = DataHelper.getASCII(buf.toString());
buf = new StringBuilder();
for (int x = 0; x < 10000; x++) {
buf.append("a");
}
byte[] lmess = DataHelper.getASCII(buf.toString());
// warm up the engines
ctx.hmac().calculate(key, smess, 0, smess.length, output, 0);
ctx.hmac().calculate(key, mmess, 0, mmess.length, output, 0);
ctx.hmac().calculate(key, lmess, 0, lmess.length, output, 0);
long before = System.currentTimeMillis();
for (int x = 0; x < times; x++) ctx.hmac().calculate(key, smess, 0, smess.length, output, 0);
long after = System.currentTimeMillis();
display(times, before, after, smess.length, "3 byte");
before = System.currentTimeMillis();
for (int x = 0; x < times; x++) ctx.hmac().calculate(key, mmess, 0, mmess.length, output, 0);
after = System.currentTimeMillis();
display(times, before, after, mmess.length, "2KB");
before = System.currentTimeMillis();
for (int x = 0; x < times; x++) ctx.hmac().calculate(key, lmess, 0, lmess.length, output, 0);
after = System.currentTimeMillis();
display(times, before, after, lmess.length, "10KB");
}
use of net.i2p.data.SessionKey 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));
}
use of net.i2p.data.SessionKey in project i2p.i2p by i2p.
the class CryptixAESEngineTest method testEDBlock.
public static void testEDBlock() {
I2PAppContext ctx = I2PAppContext.getGlobalContext();
SessionKey key = ctx.keyGenerator().generateSessionKey();
byte[] iv = new byte[16];
byte[] orig = new byte[16];
byte[] encrypted = new byte[16];
byte[] decrypted = new byte[16];
ctx.random().nextBytes(iv);
ctx.random().nextBytes(orig);
CryptixAESEngine aes = new CryptixAESEngine(ctx);
aes.encryptBlock(orig, 0, key, encrypted, 0);
aes.decryptBlock(encrypted, 0, key, decrypted, 0);
assertTrue(DataHelper.eq(decrypted, orig));
}
use of net.i2p.data.SessionKey in project i2p.i2p by i2p.
the class CryptixAESEngineTest method testED2.
public static void testED2() {
I2PAppContext ctx = I2PAppContext.getGlobalContext();
SessionKey key = ctx.keyGenerator().generateSessionKey();
byte[] iv = new byte[16];
byte[] orig = new byte[128];
byte[] data = new byte[128];
ctx.random().nextBytes(iv);
ctx.random().nextBytes(orig);
CryptixAESEngine aes = new CryptixAESEngine(ctx);
aes.encrypt(orig, 0, data, 0, key, iv, data.length);
aes.decrypt(data, 0, data, 0, key, iv, data.length);
assertTrue(DataHelper.eq(data, orig));
}
Aggregations