use of com.keepassdroid.database.exception.InvalidKeyFileException in project KeePassDX by Kunzisoft.
the class PwDatabase method getFileKey.
protected byte[] getFileKey(InputStream keyInputStream) throws InvalidKeyFileException, IOException {
assert (keyInputStream != null);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Util.copyStream(keyInputStream, bos);
byte[] keyData = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(keyData);
byte[] key = loadXmlKeyFile(bis);
if (key != null) {
return key;
}
long fileSize = keyData.length;
if (fileSize == 0) {
throw new KeyFileEmptyException();
} else if (fileSize == 32) {
return keyData;
} else if (fileSize == 64) {
byte[] hex = new byte[64];
try {
return hexStringToByteArray(new String(keyData));
} catch (IndexOutOfBoundsException e) {
// Key is not base 64, treat it as binary data
}
}
MessageDigest md;
try {
md = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
throw new IOException("SHA-256 not supported");
}
// SHA256Digest md = new SHA256Digest();
byte[] buffer = new byte[2048];
int offset = 0;
try {
md.update(keyData);
} catch (Exception e) {
System.out.println(e.toString());
}
return md.digest();
}
use of com.keepassdroid.database.exception.InvalidKeyFileException in project KeePassDX by Kunzisoft.
the class SetPassword method run.
@Override
public void run() {
PwDatabase pm = mDb.pm;
byte[] backupKey = new byte[pm.masterKey.length];
System.arraycopy(pm.masterKey, 0, backupKey, 0, backupKey.length);
// Set key
try {
InputStream is = UriUtil.getUriInputStream(ctx, mKeyfile);
pm.setMasterKey(mPassword, is);
} catch (InvalidKeyFileException e) {
erase(backupKey);
finish(false, e.getMessage());
return;
} catch (IOException e) {
erase(backupKey);
finish(false, e.getMessage());
return;
}
// Save Database
mFinish = new AfterSave(backupKey, mFinish);
SaveDB save = new SaveDB(ctx, mDb, mFinish, mDontSave);
save.run();
}
use of com.keepassdroid.database.exception.InvalidKeyFileException in project keepass2android by PhilippC.
the class PwDatabaseV3 method getFileKey.
protected byte[] getFileKey(InputStream keyfileStream) throws InvalidKeyFileException, IOException {
assert (keyfileStream != null);
byte[] buff = new byte[8000];
int bytesRead = 0;
ByteArrayOutputStream bao = new ByteArrayOutputStream();
while ((bytesRead = keyfileStream.read(buff)) != -1) {
bao.write(buff, 0, bytesRead);
}
byte[] keyFileData = bao.toByteArray();
ByteArrayInputStream bin = new ByteArrayInputStream(keyFileData);
if (keyFileData.length == 32) {
byte[] outputKey = new byte[32];
if (bin.read(outputKey, 0, 32) != 32) {
throw new IOException("Error reading key.");
}
return outputKey;
} else if (keyFileData.length == 64) {
byte[] hex = new byte[64];
bin.mark(64);
if (bin.read(hex, 0, 64) != 64) {
throw new IOException("Error reading key.");
}
try {
return hexStringToByteArray(new String(hex));
} catch (IndexOutOfBoundsException e) {
// Key is not base 64, treat it as binary data
bin.reset();
}
}
MessageDigest md;
try {
md = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
throw new IOException("SHA-256 not supported");
}
// SHA256Digest md = new SHA256Digest();
byte[] buffer = new byte[2048];
int offset = 0;
try {
while (true) {
bytesRead = bin.read(buffer, 0, 2048);
// End of file
if (bytesRead == -1)
break;
md.update(buffer, 0, bytesRead);
offset += bytesRead;
}
} catch (Exception e) {
System.out.println(e.toString());
}
return md.digest();
}
Aggregations