use of net.i2p.util.SecureFileOutputStream in project i2p.i2p-bote by i2p.
the class KademliaDHT method writePeers.
private void writePeers(List<KademliaPeer> peers, File file) {
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(new SecureFileOutputStream(file.getAbsolutePath())));
writer.write("# Each line is one Base64-encoded I2P destination.");
writer.newLine();
writer.write("# Do not edit while I2P-Bote is running as it will be overwritten.");
writer.newLine();
for (KademliaPeer peer : peers) {
writer.write(peer.toBase64());
writer.newLine();
}
} catch (IOException e) {
log.error("Can't write peers to file <" + file.getAbsolutePath() + ">", e);
} finally {
if (writer != null)
try {
writer.close();
} catch (IOException e) {
log.error("Can't close BufferedWriter for file <" + file.getAbsolutePath() + ">", e);
}
}
}
use of net.i2p.util.SecureFileOutputStream in project i2p.i2p-bote by i2p.
the class I2PBote method saveLocalDestinationKeys.
/**
* Writes private + public keys for the local destination out to a file.
* @param keyFile
* @param localDestinationArray
* @throws DataFormatException
* @throws IOException
*/
private void saveLocalDestinationKeys(File keyFile, byte[] localDestinationArray) throws DataFormatException, IOException {
keyFile = new SecureFile(keyFile.getAbsolutePath());
if (keyFile.exists()) {
File oldKeyFile = new File(keyFile.getPath() + "_backup");
if (!keyFile.renameTo(oldKeyFile))
log.error("Cannot rename destination key file <" + keyFile.getAbsolutePath() + "> to <" + oldKeyFile.getAbsolutePath() + ">");
} else if (!keyFile.createNewFile())
log.error("Cannot create destination key file: <" + keyFile.getAbsolutePath() + ">");
BufferedWriter fileWriter = new BufferedWriter(new OutputStreamWriter(new SecureFileOutputStream(keyFile)));
try {
fileWriter.write(Base64.encode(localDestinationArray));
} finally {
fileWriter.close();
}
}
use of net.i2p.util.SecureFileOutputStream in project i2p.i2p-bote by i2p.
the class AddressBook method save.
public void save() throws IOException, PasswordException, GeneralSecurityException {
initializeIfNeeded();
OutputStream encryptedStream = new EncryptedOutputStream(new SecureFileOutputStream(addressFile), passwordHolder);
try {
Properties properties = saveToProperties();
properties.store(new OutputStreamWriter(encryptedStream, "UTF-8"), null);
} catch (IOException e) {
log.error("Can't save email identities to file <" + addressFile.getAbsolutePath() + ">.", e);
throw e;
} finally {
encryptedStream.close();
}
}
use of net.i2p.util.SecureFileOutputStream in project i2p.i2p-bote by i2p.
the class PasswordCache method createDerivedKey.
/**
* Reads salt and <code>scrypt</code> parameters from the cache file, or chooses
* a new salt array if the file doesn't exist. The encryption key is then computed
* and the variable <code>derivedKey</code> is populated.
* @throws IOException
* @throws GeneralSecurityException
*/
private void createDerivedKey() throws IOException, GeneralSecurityException {
byte[] salt = null;
derivedKey = null;
// read salt + scrypt parameters from file if available
File derivParamFile = configuration.getKeyDerivationParametersFile();
if (derivParamFile.exists())
derivedKey = FileEncryptionUtil.getEncryptionKey(password, derivParamFile);
// if necessary, create a new salt and key and write the derivation parameters to the cache file
if (derivedKey == null || !derivedKey.scryptParams.equals(KDF_PARAMETERS)) {
I2PAppContext appContext = I2PAppContext.getGlobalContext();
salt = new byte[SALT_LENGTH];
appContext.random().nextBytes(salt);
DataOutputStream outputStream = null;
try {
byte[] key = FileEncryptionUtil.getEncryptionKey(password, salt, KDF_PARAMETERS);
derivedKey = new DerivedKey(salt, KDF_PARAMETERS, key);
outputStream = new DataOutputStream(new SecureFileOutputStream(derivParamFile));
KDF_PARAMETERS.writeTo(outputStream);
outputStream.write(salt);
} finally {
if (outputStream != null)
outputStream.close();
}
}
}
use of net.i2p.util.SecureFileOutputStream in project i2p.i2p by i2p.
the class PersistNews method store.
/**
* Store each entry.
* Old entries are always overwritten, as they may change even without the updated date changing.
*
* @param entries each one should be "entry" at the root
* @return success
*/
public static boolean store(I2PAppContext ctx, List<Node> entries) {
Log log = ctx.logManager().getLog(PersistNews.class);
File dir = new SecureDirectory(ctx.getConfigDir(), DIR);
if (!dir.exists())
dir.mkdirs();
StringBuilder buf = new StringBuilder();
boolean rv = true;
for (Node entry : entries) {
Node nid = entry.getNode("id");
if (nid == null) {
if (log.shouldWarn())
log.warn("entry without UUID");
continue;
}
String id = nid.getValue();
if (id == null) {
if (log.shouldWarn())
log.warn("entry without UUID");
continue;
}
String name = idToName(ctx, id);
File file = new File(dir, name);
Writer out = null;
try {
out = new OutputStreamWriter(new GZIPOutputStream(new SecureFileOutputStream(file)));
out.write(XML_START);
XMLParser.toString(buf, entry);
out.write(buf.toString());
buf.setLength(0);
} catch (IOException ioe) {
if (log.shouldWarn())
log.warn("failed store to " + file, ioe);
rv = false;
} finally {
if (out != null)
try {
out.close();
} catch (IOException ioe) {
}
}
}
return rv;
}
Aggregations