use of com.keepassdroid.database.exception.PwDbOutputException in project KeePassDX by Kunzisoft.
the class Database method SaveData.
public void SaveData(Context ctx, Uri uri) throws IOException, PwDbOutputException {
if (uri.getScheme().equals("file")) {
String filename = uri.getPath();
File tempFile = new File(filename + ".tmp");
FileOutputStream fos = new FileOutputStream(tempFile);
// BufferedOutputStream bos = new BufferedOutputStream(fos);
// PwDbV3Output pmo = new PwDbV3Output(pm, bos, App.getCalendar());
PwDbOutput pmo = PwDbOutput.getInstance(pm, fos);
pmo.output();
// bos.flush();
// bos.close();
fos.close();
// Force data to disk before continuing
try {
fos.getFD().sync();
} catch (SyncFailedException e) {
// Ignore if fsync fails. We tried.
}
File orig = new File(filename);
if (!tempFile.renameTo(orig)) {
throw new IOException("Failed to store database.");
}
} else {
OutputStream os;
try {
os = ctx.getContentResolver().openOutputStream(uri);
} catch (Exception e) {
throw new IOException("Failed to store database.");
}
PwDbOutput pmo = PwDbOutput.getInstance(pm, os);
pmo.output();
os.close();
}
mUri = uri;
}
use of com.keepassdroid.database.exception.PwDbOutputException in project KeePassDX by Kunzisoft.
the class PwDbOutput method setIVs.
protected SecureRandom setIVs(PwDbHeader header) throws PwDbOutputException {
SecureRandom random;
try {
random = SecureRandom.getInstance("SHA1PRNG");
} catch (NoSuchAlgorithmException e) {
throw new PwDbOutputException("Does not support secure random number generation.");
}
random.nextBytes(header.encryptionIV);
random.nextBytes(header.masterSeed);
return random;
}
use of com.keepassdroid.database.exception.PwDbOutputException in project KeePassDX by Kunzisoft.
the class PwDbV3Output method outputHeader.
public PwDbHeaderV3 outputHeader(OutputStream os) throws PwDbOutputException {
// Build header
PwDbHeaderV3 header = new PwDbHeaderV3();
header.signature1 = PwDbHeader.PWM_DBSIG_1;
header.signature2 = PwDbHeaderV3.DBSIG_2;
header.flags = PwDbHeaderV3.FLAG_SHA2;
if (mPM.getEncAlgorithm() == PwEncryptionAlgorithm.Rjindal) {
header.flags |= PwDbHeaderV3.FLAG_RIJNDAEL;
} else if (mPM.getEncAlgorithm() == PwEncryptionAlgorithm.Twofish) {
header.flags |= PwDbHeaderV3.FLAG_TWOFISH;
} else {
throw new PwDbOutputException("Unsupported algorithm.");
}
header.version = PwDbHeaderV3.DBVER_DW;
header.numGroups = mPM.getGroups().size();
header.numEntries = mPM.entries.size();
header.numKeyEncRounds = mPM.getNumKeyEncRecords();
setIVs(header);
// Content checksum
MessageDigest md = null;
try {
md = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
throw new PwDbOutputException("SHA-256 not implemented here.");
}
// Header checksum
MessageDigest headerDigest;
try {
headerDigest = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
throw new PwDbOutputException("SHA-256 not implemented here.");
}
NullOutputStream nos;
nos = new NullOutputStream();
DigestOutputStream headerDos = new DigestOutputStream(nos, headerDigest);
// Output header for the purpose of calculating the header checksum
PwDbHeaderOutputV3 pho = new PwDbHeaderOutputV3(header, headerDos);
try {
pho.outputStart();
pho.outputEnd();
headerDos.flush();
} catch (IOException e) {
throw new PwDbOutputException(e);
}
byte[] headerHash = headerDigest.digest();
headerHashBlock = getHeaderHashBuffer(headerHash);
// Output database for the purpose of calculating the content checksum
nos = new NullOutputStream();
DigestOutputStream dos = new DigestOutputStream(nos, md);
BufferedOutputStream bos = new BufferedOutputStream(dos);
try {
outputPlanGroupAndEntries(bos);
bos.flush();
bos.close();
} catch (IOException e) {
throw new PwDbOutputException("Failed to generate checksum.");
}
header.contentsHash = md.digest();
// Output header for real output, containing content hash
pho = new PwDbHeaderOutputV3(header, os);
try {
pho.outputStart();
dos.on(false);
pho.outputContentHash();
dos.on(true);
pho.outputEnd();
dos.flush();
} catch (IOException e) {
throw new PwDbOutputException(e);
}
return header;
}
use of com.keepassdroid.database.exception.PwDbOutputException in project KeePassDX by Kunzisoft.
the class PwDbV3Output method outputPlanGroupAndEntries.
public void outputPlanGroupAndEntries(OutputStream os) throws PwDbOutputException {
LEDataOutputStream los = new LEDataOutputStream(os);
if (useHeaderHash() && headerHashBlock != null) {
try {
los.writeUShort(0x0000);
los.writeInt(headerHashBlock.length);
los.write(headerHashBlock);
} catch (IOException e) {
throw new PwDbOutputException("Failed to output header hash: " + e.getMessage());
}
}
// Groups
List<PwGroup> groups = mPM.getGroups();
for (int i = 0; i < groups.size(); i++) {
PwGroupV3 pg = (PwGroupV3) groups.get(i);
PwGroupOutputV3 pgo = new PwGroupOutputV3(pg, os);
try {
pgo.output();
} catch (IOException e) {
throw new PwDbOutputException("Failed to output a tree: " + e.getMessage());
}
}
// Entries
for (int i = 0; i < mPM.entries.size(); i++) {
PwEntryV3 pe = (PwEntryV3) mPM.entries.get(i);
PwEntryOutputV3 peo = new PwEntryOutputV3(pe, os);
try {
peo.output();
} catch (IOException e) {
throw new PwDbOutputException("Failed to output an entry.");
}
}
}
use of com.keepassdroid.database.exception.PwDbOutputException in project KeePassDX by Kunzisoft.
the class PwDbV4Output method output.
@Override
public void output() throws PwDbOutputException {
try {
try {
engine = CipherFactory.getInstance(mPM.dataCipher);
} catch (NoSuchAlgorithmException e) {
throw new PwDbOutputException("No such cipher", e);
}
header = (PwDbHeaderV4) outputHeader(mOS);
OutputStream osPlain;
if (header.version < PwDbHeaderV4.FILE_VERSION_32_4) {
CipherOutputStream cos = attachStreamEncryptor(header, mOS);
cos.write(header.streamStartBytes);
HashedBlockOutputStream hashed = new HashedBlockOutputStream(cos);
osPlain = hashed;
} else {
mOS.write(hashOfHeader);
mOS.write(headerHmac);
HmacBlockOutputStream hbos = new HmacBlockOutputStream(mOS, mPM.hmacKey);
osPlain = attachStreamEncryptor(header, hbos);
}
OutputStream osXml;
try {
if (mPM.compressionAlgorithm == PwCompressionAlgorithm.Gzip) {
osXml = new GZIPOutputStream(osPlain);
} else {
osXml = osPlain;
}
if (header.version >= PwDbHeaderV4.FILE_VERSION_32_4) {
PwDbInnerHeaderOutputV4 ihOut = new PwDbInnerHeaderOutputV4((PwDatabaseV4) mPM, header, osXml);
ihOut.output();
}
outputDatabase(osXml);
osXml.close();
} catch (IllegalArgumentException e) {
throw new PwDbOutputException(e);
} catch (IllegalStateException e) {
throw new PwDbOutputException(e);
}
} catch (IOException e) {
throw new PwDbOutputException(e);
}
}
Aggregations