private void syncPrivateKey(File keyDir) { DataStructure ds; File keyFile = new File(keyDir, KEYFILE_PRIVATE_ENC); boolean exists = (_privateKey != null); if (exists) ds = _privateKey; else ds = new PrivateKey(); DataStructure readin = syncKey(keyFile, ds, exists); if (readin != null && !exists) _privateKey = (PrivateKey) readin; }
public DataStructure createStructureToRead() { return new PrivateKey(); }
public DataStructure createDataStructure() throws DataFormatException { PrivateKey privateKey = new PrivateKey(); byte data[] = new byte[PrivateKey.KEYSIZE_BYTES]; for (int i = 0; i < data.length; i++) data[i] = (byte)(i%16); privateKey.setData(data); return privateKey; } public DataStructure createStructureToRead() { return new PrivateKey(); }
_availableMessages = new ConcurrentHashMap<Long, MessagePayloadMessage>(); _myDestination = new Destination(); _privateKey = new PrivateKey(); _signingPrivateKey = new SigningPrivateKey(); } else {
private KeyPair precalc() { long start = System.currentTimeMillis(); byte[] priv = new byte[32]; do { _context.random().nextBytes(priv); // little endian, loop if too small // worth doing? } while (priv[31] == 0); byte[] pub = new byte[32]; Curve25519.eval(pub, 0, priv, null); KeyPair rv = new KeyPair(new PublicKey(EncType.ECIES_X25519, pub), new PrivateKey(EncType.ECIES_X25519, priv)); long end = System.currentTimeMillis(); long diff = end - start; _context.statManager().addRateData("crypto.XDHGenerateTime", diff); if (_log.shouldLog(Log.DEBUG)) _log.debug("Took " + diff + "ms to generate local DH value"); return rv; }
@Test public void testBase64Constructor() throws Exception{ PrivateKey privateKey = new PrivateKey(); byte data[] = new byte[PrivateKey.KEYSIZE_BYTES]; for (int i = 0; i < data.length; i++) data[i] = (byte)(i%56); privateKey.setData(data); PrivateKey key2 = new PrivateKey(privateKey.toBase64()); assertEquals(privateKey, key2); }
if (type.isAvailable()) { try { PrivateKey privKey = new PrivateKey(type); privKey.fromBase64(spk.substring(colon + 1)); privKeys.add(privKey); PrivateKey privKey = new PrivateKey(); privKey.fromBase64(spk); privKeys.add(privKey);
/** * Check whether a base64-encoded {dest,privkey,signingprivkey} is valid * * @param dest The base64-encoded destination and keys to be checked (same format as PrivateKeyFile) * @return true if valid */ public static boolean checkPrivateDestination(String dest) { byte[] b = Base64.decode(dest); if (b == null || b.length < 663) return false; ByteArrayInputStream destKeyStream = new ByteArrayInputStream(b); try { Destination d = Destination.create(destKeyStream); new PrivateKey().readBytes(destKeyStream); SigningPrivateKey spk = new SigningPrivateKey(d.getSigningPublicKey().getType()); spk.readBytes(destKeyStream); } catch (DataFormatException e) { return false; } catch (IOException e) { return false; } return destKeyStream.available() == 0; }
/** * Supports EncTypes * @since 0.9.38 */ public KeyPair generatePKIKeys(EncType type) { PublicKey pub; PrivateKey priv; switch (type) { case ELGAMAL_2048: SimpleDataStructure[] keys = generatePKIKeys(); pub = (PublicKey) keys[0]; priv = (PrivateKey) keys[1]; break; case ECIES_X25519: byte[] bpriv = new byte[32]; do { _context.random().nextBytes(bpriv); // little endian, loop if too small // worth doing? } while (bpriv[31] == 0); byte[] bpub = new byte[32]; Curve25519.eval(bpub, 0, bpriv, null); pub = new PublicKey(type, bpub); priv = new PrivateKey(type, bpriv); break; default: throw new IllegalArgumentException("Unsupported algorithm"); } return new KeyPair(pub, priv); }
/** * Same as above but different return type * @since 0.8.7 */ public SimpleDataStructure[] generatePKIKeys() { BigInteger a = new NativeBigInteger(getElGamalExponentSize(), _context.random()); BigInteger aalpha = CryptoConstants.elgg.modPow(a, CryptoConstants.elgp); SimpleDataStructure[] keys = new SimpleDataStructure[2]; keys[0] = new PublicKey(); keys[1] = new PrivateKey(); // bigInteger.toByteArray returns SIGNED integers, but since they'return positive, // signed two's complement is the same as unsigned try { keys[0].setData(SigUtil.rectify(aalpha, PublicKey.KEYSIZE_BYTES)); keys[1].setData(SigUtil.rectify(a, PrivateKey.KEYSIZE_BYTES)); } catch (InvalidKeyException ike) { throw new IllegalArgumentException(ike); } return keys; }
@Test public void testNullData() throws Exception{ PrivateKey privateKey = new PrivateKey(); privateKey.toString(); exception.expect(DataFormatException.class); exception.expectMessage("No data to write out"); privateKey.writeBytes(new ByteArrayOutputStream()); }
@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 testShortData() throws Exception{ PrivateKey privateKey = new PrivateKey(); byte data[] = new byte[56]; for (int i = 0; i < data.length; i++) data[i] = (byte)(i); exception.expect(IllegalArgumentException.class); exception.expectMessage("Bad data length: 56; required: " + PrivateKey.KEYSIZE_BYTES); privateKey.setData(data); }
@Test public void testNullEquals(){ PrivateKey privateKey = new PrivateKey(); byte data[] = new byte[PrivateKey.KEYSIZE_BYTES]; for (int i = 0; i < data.length; i++) data[i] = (byte)(i%56); privateKey.setData(data); assertFalse(privateKey.equals(null)); }
RouterIdentity ri = new RouterIdentity(); ri.readBytes(in); privKey = new PrivateKey(); privKey.readBytes(in); SigType type = ri.getSigningPublicKey().getType();
@Override protected void doReadMessage(InputStream in, int size) throws I2CPMessageException, IOException { try { _sessionId = new SessionId(); _sessionId.readBytes(in); // Revocation is unimplemented. // As the SPK comes before the LeaseSet, we don't know the key type. // We could have some sort of callback or state setting so we get the // expected type from the session. But for now, we just assume it's 20 bytes. // Clients outside router context should throw in a dummy 20 bytes. _signingPrivateKey = new SigningPrivateKey(); _signingPrivateKey.readBytes(in); _privateKey = new PrivateKey(); _privateKey.readBytes(in); _leaseSet = new LeaseSet(); _leaseSet.readBytes(in); } catch (DataFormatException dfe) { throw new I2CPMessageException("Error reading the CreateLeaseSetMessage", dfe); } }
try { fis = new BufferedInputStream(new FileInputStream(rkf1)); privkey = new PrivateKey(); privkey.readBytes(fis); signingPrivKey = new SigningPrivateKey();
if (etype == null) throw new I2CPMessageException("Unsupported encryption type"); PrivateKey priv = new PrivateKey(etype); priv.readBytes(in); addPrivateKey(priv); if (etype == null) throw new I2CPMessageException("Unsupported encryption type"); _privateKey = new PrivateKey(etype); _privateKey.readBytes(in);
public void testVerifyCompatability(){ PublicKey pub = new PublicKey(); PrivateKey priv = new PrivateKey(); try{ pub.fromBase64(PUBLIC_KEY); priv.fromBase64(PRIVATE_KEY); }catch(DataFormatException dfe){ dfe.printStackTrace(); fail(); } for (int i = 0; i < ENCRYPTED.length; i++) { byte enc[] = Base64.decode(ENCRYPTED[i]); byte decrypted[] = _context.elGamalEngine().decrypt(enc, priv); assertTrue(DataHelper.eq(decrypted, DataHelper.getASCII(UNENCRYPTED[i]))); } }
} else { _ntcp2StaticPrivkey = priv; _ntcp2StaticPubkey = (new PrivateKey(EncType.ECIES_X25519, priv)).toPublic().getData();