Search in sources :

Example 1 with CopyInputStream

use of com.keepassdroid.stream.CopyInputStream in project KeePassDX by Kunzisoft.

the class Kdb4 method testSaving.

private void testSaving(String inputFile, String password, String outputFile) throws IOException, InvalidDBException, PwDbOutputException {
    Context ctx = getContext();
    AssetManager am = ctx.getAssets();
    InputStream is = am.open(inputFile, AssetManager.ACCESS_STREAMING);
    ImporterV4 importer = new ImporterV4();
    PwDatabaseV4 db = importer.openDatabase(is, password, null);
    is.close();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    PwDbV4Output output = (PwDbV4Output) PwDbOutput.getInstance(db, bos);
    output.output();
    byte[] data = bos.toByteArray();
    FileOutputStream fos = new FileOutputStream(TestUtil.getSdPath(outputFile), false);
    InputStream bis = new ByteArrayInputStream(data);
    bis = new CopyInputStream(bis, fos);
    importer = new ImporterV4();
    db = importer.openDatabase(bis, password, null);
    bis.close();
    fos.close();
}
Also used : Context(android.content.Context) PwDbV4Output(com.keepassdroid.database.save.PwDbV4Output) AssetManager(android.content.res.AssetManager) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) CopyInputStream(com.keepassdroid.stream.CopyInputStream) InputStream(java.io.InputStream) ImporterV4(com.keepassdroid.database.load.ImporterV4) FileOutputStream(java.io.FileOutputStream) CopyInputStream(com.keepassdroid.stream.CopyInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PwDatabaseV4(com.keepassdroid.database.PwDatabaseV4)

Example 2 with CopyInputStream

use of com.keepassdroid.stream.CopyInputStream in project KeePassDX by Kunzisoft.

the class PwDbHeaderV4 method loadFromFile.

/**
 * Assumes the input stream is at the beginning of the .kdbx file
 * @param is
 * @throws IOException
 * @throws InvalidDBVersionException
 */
public HeaderAndHash loadFromFile(InputStream is) throws IOException, InvalidDBVersionException {
    MessageDigest md;
    try {
        md = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        throw new IOException("No SHA-256 implementation");
    }
    ByteArrayOutputStream headerBOS = new ByteArrayOutputStream();
    CopyInputStream cis = new CopyInputStream(is, headerBOS);
    DigestInputStream dis = new DigestInputStream(cis, md);
    LEDataInputStream lis = new LEDataInputStream(dis);
    int sig1 = lis.readInt();
    int sig2 = lis.readInt();
    if (!matchesHeader(sig1, sig2)) {
        throw new InvalidDBVersionException();
    }
    version = lis.readUInt();
    if (!validVersion(version)) {
        throw new InvalidDBVersionException();
    }
    boolean done = false;
    while (!done) {
        done = readHeaderField(lis);
    }
    byte[] hash = md.digest();
    return new HeaderAndHash(headerBOS.toByteArray(), hash);
}
Also used : DigestInputStream(java.security.DigestInputStream) CopyInputStream(com.keepassdroid.stream.CopyInputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MessageDigest(java.security.MessageDigest) LEDataInputStream(com.keepassdroid.stream.LEDataInputStream) InvalidDBVersionException(com.keepassdroid.database.exception.InvalidDBVersionException)

Aggregations

CopyInputStream (com.keepassdroid.stream.CopyInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Context (android.content.Context)1 AssetManager (android.content.res.AssetManager)1 PwDatabaseV4 (com.keepassdroid.database.PwDatabaseV4)1 InvalidDBVersionException (com.keepassdroid.database.exception.InvalidDBVersionException)1 ImporterV4 (com.keepassdroid.database.load.ImporterV4)1 PwDbV4Output (com.keepassdroid.database.save.PwDbV4Output)1 LEDataInputStream (com.keepassdroid.stream.LEDataInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 DigestInputStream (java.security.DigestInputStream)1 MessageDigest (java.security.MessageDigest)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1