public UnencryptedEmailPacket(InputStream inputStream) throws IOException { this(Util.readBytes(inputStream)); }
/** * Moves a file. * @param from * @param to * @return <code>true</code> on success, <code>false</code> if the file can't be moved * @throws IOException */ private boolean move(File from, File to) throws IOException { boolean success = from.renameTo(to); // renameTo doesn't always work, even when "from" and "to" are on the same partition, // so in those cases do copy+delete if (success) return true; else { Util.copy(from, to); return from.delete(); } }
public PeerList(byte[] data) throws DataFormatException { super(data); ByteBuffer buffer = ByteBuffer.wrap(data, HEADER_LENGTH, data.length-HEADER_LENGTH); int numPeers = buffer.getShort() & 0xFFFF; peers = new ArrayList<Destination>(); for (int i=0; i<numPeers; i++) { Destination peer = Util.createDestination(buffer); peers.add(peer); } if (buffer.hasRemaining()) log.debug("Peer List has " + buffer.remaining() + " extra bytes."); }
/** Returns a <code>ThreadFactory</code> that creates threads that run at minimum priority */ public static ThreadFactory createThreadFactory(final String threadName, final int stackSize) { return createThreadFactory(threadName, stackSize, Thread.MIN_PRIORITY); }
/** * Decrypts the <code>CommunicationPacket</code> inside this packet. * @throws DataFormatException * @throws MalformedPacketException */ private CommunicationPacket decrypt(I2PSession i2pSession) throws DataFormatException, MalformedPacketException { PrivateKey privateKey = i2pSession.getDecryptionKey(); byte[] decryptedData = Util.decrypt(payload, privateKey); return CommunicationPacket.createPacket(decryptedData); } }
/** Returns the DHT keys of all email packets which haven't been deleted from the DHT */ public Collection<PacketInfo> getUndeliveredPacketKeys() { Collection<PacketInfo> packets = new ArrayList<PacketInfo>(); for (Object property: keySet()) if (property instanceof String) { String delFlagProperty = (String)property; if (delFlagProperty.matches(PROPERTY_DESTINATION + ".*" + DELETED_FROM_DHT) && "false".equals(getProperty(delFlagProperty))) { String baseProperty = delFlagProperty.replace(DELETED_FROM_DHT, ""); String dhtKeyProperty = baseProperty + DHT_KEY; String dhtKeyStr = getProperty(dhtKeyProperty); String delVerifProperty = baseProperty + DELETE_VERIFICATION_HASH; String delVerifStr = getProperty(delVerifProperty); try { Hash dhtKey = Util.createHash(dhtKeyStr); Hash delVerifHash = Util.createHash(delVerifStr); packets.add(new PacketInfo(dhtKey, delVerifHash)); } catch (DataFormatException e) { log.error("Invalid DHT key or verification hash in email metadata for property key " + baseProperty, e); } } } return packets; }
/** * Returns a byte array of length <code>NUM_FINGERPRINT_BYTES</code>. * @throws GeneralSecurityException */ private byte[] getRawFingerprint() throws GeneralSecurityException { byte[] input = Util.concat(nameHash.toByteArray(), destination.toByteArray()); byte[] fingerprint = SCrypt.scrypt(input, salt, SCRYPT_PARAMETERS.N, SCRYPT_PARAMETERS.r, SCRYPT_PARAMETERS.p, NUM_FINGERPRINT_BYTES); return fingerprint; }
/** Only accepts <code>ElGamalPublicKey</code>s. */ @Override public byte[] encrypt(byte[] data, PublicKey key) throws GeneralSecurityException { ElGamalPublicKey elGamalKey = castToElGamal(key); net.i2p.data.PublicKey i2pPublicKey = elGamalKey.getI2PKey(); return Util.encrypt(data, i2pPublicKey); }
boolean paramsFileFound = Util.contains(parentDir, Configuration.KEY_DERIVATION_PARAMETERS_FILE); if (paramsFileFound) derivParamsFile = new File(parentDir, Configuration.KEY_DERIVATION_PARAMETERS_FILE);
destinationString = Util.fixAddress(destinationString);
public RelayPacketHandler(RelayPacketFolder relayPacketFolder, DHT dht, I2PSendQueue sendQueue, I2PSession i2pSession) { this.relayPacketFolder = relayPacketFolder; this.dht = dht; this.sendQueue = sendQueue; this.i2pSession = i2pSession; dhtTaskExecutor = Executors.newFixedThreadPool(MAX_CONCURRENT_DHT_TASKS, Util.createThreadFactory("DHTStoreTask", THREAD_STACK_SIZE)); }
/** Only accepts <code>ElGamalPrivateKey</code>s. The public key is not used. */ @Override public byte[] decrypt(byte[] data, PublicKey publicKey, PrivateKey privateKey) throws GeneralSecurityException { if (data == null) return null; ElGamalPrivateKey elGamalKey = castToElGamal(privateKey); try { net.i2p.data.PrivateKey i2pPrivateKey = elGamalKey.getI2PKey(); return Util.decrypt(data, i2pPrivateKey); } catch (DataFormatException e) { byte[] shortenedData = data.length>10?Arrays.copyOf(data, 10):data; throw new KeyException("Can't decrypt data: " + Arrays.toString(shortenedData) + " (only the first 10 elements are shown).", e); } }
public Hash getDeleteVerificationHash(EmailDestination destination, Hash dhtKey) { int destIndex = getDestinationIndex(destination); int packetIndex = getPacketIndex(destIndex, dhtKey); String hashStr = getProperty(PROPERTY_DESTINATION + destIndex + "." + PACKET + packetIndex + "." + DELETE_VERIFICATION_HASH); try { return Util.createHash(hashStr); } catch (DataFormatException e) { log.error("Invalid delete verification hash: <" + hashStr + ">", e); return null; } }
private static Fingerprint generate(Hash nameHash, EmailDestination destination) throws GeneralSecurityException { byte[] input = Util.concat(nameHash.toByteArray(), destination.toByteArray()); RandomSource randomSource = I2PAppContext.getGlobalContext().random(); while (true) { byte[] salt = new byte[NUM_SALT_BYTES]; randomSource.nextBytes(salt); byte[] fingerprint = SCrypt.scrypt(input, salt, SCRYPT_PARAMETERS.N, SCRYPT_PARAMETERS.r, SCRYPT_PARAMETERS.p, NUM_FINGERPRINT_BYTES); if (fingerprint[NUM_FINGERPRINT_BYTES-1]==0 && (fingerprint[NUM_FINGERPRINT_BYTES-2]&3)==0) return new Fingerprint(nameHash, destination, salt); } }
private byte[] encrypt(CommunicationPacket packet, Destination destination) { PublicKey publicKey = destination.getPublicKey(); byte[] data = packet.toByteArray(); return Util.encrypt(data, publicKey); }
/** * Decrypts a file with a given password and returns <code>true</code> if the decrypted * text is {@link FileEncryptionConstants#PASSWORD_FILE_PLAIN_TEXT}; <code>false</code> * otherwise. * @param password * @param passwordFile * @throws IOException * @throws GeneralSecurityException */ public static boolean isPasswordCorrect(byte[] password, File passwordFile) throws IOException, GeneralSecurityException { if (!passwordFile.exists()) return true; EncryptedInputStream inputStream = null; try { inputStream = new EncryptedInputStream(new FileInputStream(passwordFile), password); byte[] decryptedText = Util.readBytes(inputStream); return Arrays.equals(PASSWORD_FILE_PLAIN_TEXT, decryptedText); } catch (PasswordException e) { return false; } finally { if (inputStream != null) inputStream.close(); } }
this.dht = dht; this.peerManager = peerManager; mailCheckThreadFactory = Util.createThreadFactory("ChkEmailTask", CheckEmailTask.THREAD_STACK_SIZE); mailCheckExecutor = new ThreadPoolExecutor( 0,
public RelayRequest(byte[] data) throws DataFormatException { super(data); ByteBuffer buffer = ByteBuffer.wrap(data, HEADER_LENGTH, data.length-HEADER_LENGTH); int hashCashLength = buffer.getShort() & 0xFFFF; byte[] hashCashData = new byte[hashCashLength]; buffer.get(hashCashData); try { hashCash = new HashCash(new String(hashCashData)); } catch (NoSuchAlgorithmException e) { log.error("Cannot create HashCash.", e); } delayMilliseconds = buffer.getInt() * 1000L; nextDestination = Util.createDestination(buffer); returnChain = new ReturnChain(buffer); int payloadLength = buffer.getShort() & 0xFFFF; payload = new byte[payloadLength]; buffer.get(payload); // read padding (the actual data doesn't matter, only the length) padBytes = buffer.remaining(); }
try { inputStream = new EncryptedInputStream(new FileInputStream(file), oldPassword); decryptedData = Util.readBytes(inputStream);