/** * 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; }
@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 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)); }
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(); }
@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); }
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; }
@Override protected byte[] doWriteMessage() throws I2CPMessageException, IOException { if ((_sessionId == null) || (_signingPrivateKey == null) || (_privateKey == null) || (_leaseSet == null)) throw new I2CPMessageException("Unable to write out the message as there is not enough data"); int size = 4 // sessionId + _signingPrivateKey.length() + PrivateKey.KEYSIZE_BYTES + _leaseSet.size(); ByteArrayOutputStream os = new ByteArrayOutputStream(size); try { _sessionId.writeBytes(os); _signingPrivateKey.writeBytes(os); _privateKey.writeBytes(os); _leaseSet.writeBytes(os); } catch (DataFormatException dfe) { throw new I2CPMessageException("Error writing out the message data", dfe); } return os.toByteArray(); }
if (type.isAvailable()) { try { PrivateKey privKey = new PrivateKey(type); privKey.fromBase64(spk.substring(colon + 1)); privKeys.add(privKey); if (_log.shouldDebug()) PrivateKey privKey = new PrivateKey(); privKey.fromBase64(spk); privKeys.add(privKey); } catch (DataFormatException dfe) {
_ntcp2StaticPrivkey = keys.getPrivate().getData(); _ntcp2StaticPubkey = keys.getPublic().getData(); shouldSave = true; } else { _ntcp2StaticPrivkey = priv; _ntcp2StaticPubkey = (new PrivateKey(EncType.ECIES_X25519, priv)).toPublic().getData();
@Override protected byte[] doWriteMessage() throws I2CPMessageException, IOException { int type = _leaseSet.getType(); if (_sessionId == null || _leaseSet == null || (type != DatabaseEntry.KEY_TYPE_META_LS2 && (_signingPrivateKey == null || _privateKey == null))) throw new I2CPMessageException("Unable to write out the message as there is not enough data"); int size = 4 // sessionId + 1 // type + _leaseSet.size() + _signingPrivateKey.length() + _privateKey.length(); ByteArrayOutputStream os = new ByteArrayOutputStream(size); try { _sessionId.writeBytes(os); os.write(_leaseSet.getType()); _leaseSet.writeBytes(os); if (type != DatabaseEntry.KEY_TYPE_META_LS2) { _signingPrivateKey.writeBytes(os); for (PrivateKey pk : getPrivateKeys()) { pk.writeBytes(os); } } } catch (DataFormatException dfe) { throw new I2CPMessageException("Error writing out the message data", dfe); } return os.toByteArray(); }
EncType type = priv.getType(); byte[] data; switch (type) { case ELGAMAL_2048: BigInteger a = new NativeBigInteger(1, priv.toByteArray()); BigInteger aalpha = CryptoConstants.elgg.modPow(a, CryptoConstants.elgp); try { Curve25519.eval(data, 0, priv.getData(), null); break;
/** * @param primary must be a I2PSessionMuxedImpl */ public SubSession(I2PSession primary, InputStream destKeyStream, Properties options) throws I2PSessionException { super((I2PSessionMuxedImpl)primary, destKeyStream, options); _primary = (I2PSessionMuxedImpl) primary; if (!getDecryptionKey().equals(_primary.getDecryptionKey())) throw new I2PSessionException("encryption key mismatch"); if (getPrivateKey().equals(_primary.getPrivateKey())) throw new I2PSessionException("signing key must differ"); // state management }
_privateKey.readBytes(destKeyStream); SigType dtype = _myDestination.getSigningPublicKey().getType(); _signingPrivateKey = new SigningPrivateKey(dtype);
@Override public void generateKeyPair() { KeyPair kp = _xdh.getKeys(); System.arraycopy(kp.getPrivate().getData(), 0, privateKey, 0, 32); System.arraycopy(kp.getPublic().getData(), 0, publicKey, 0, 32); mode = 0x03; }
/** constructs from base64 * @param base64Data a string of base64 data (the output of .toBase64() called * on a prior instance of PrivateKey */ public PrivateKey(String base64Data) throws DataFormatException { this(DEF_TYPE); fromBase64(base64Data); }
/** * @param type non-null * @param data must be non-null * @since 0.9.38 */ public PrivateKey(EncType type, byte data[]) { this(type); if (data == null) throw new IllegalArgumentException("Data must be specified"); setData(data); }
public DataStructure createStructureToRead() { return new PrivateKey(); }
/** * Copied from I2PClientImpl.createDestination() */ public void write() throws IOException, DataFormatException { OutputStream out = null; try { out = new SecureFileOutputStream(this.file); this.dest.writeBytes(out); this.privKey.writeBytes(out); this.signingPrivKey.writeBytes(out); if (isOffline()) { DataHelper.writeLong(out, 4, _offlineExpiration / 1000); DataHelper.writeLong(out, 2, _transientSigningPubKey.getType().getCode()); _transientSigningPubKey.writeBytes(out); _offlineSignature.writeBytes(out); _transientSigningPrivKey.writeBytes(out); } } finally { if (out != null) { try { out.close(); } catch (IOException ioe) {} } } }
@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); }