mirror of
https://github.com/CatimaLoyalty/Android.git
synced 2026-02-01 10:42:01 -05:00
Merge #494
494: Inter Related Code changes for exporting encrypted zip r=TheLastProject a=ankittiwari101 1. Created new method `multipleCardsExportImportPasswordProtected()` in ImportExportTest.java 2. Added 5th Parameter `char[] password` to exportData method of MultiFormater.java 3. From this new method passed password field to `importData` and `exportData` method calls. 4. In CatimaExporter.java added a password parameter to ZipOutputStream(output,password) if a non null char[] array of non zero length is received for password parameter and setEncryptFiles(true) using AES 256 Encryption(by default)" Remaining - Dialogue Box for entering password. Note - This is only a draft pull request, if everything here looks good, I'll work next on The Password Dialog which is the final piece in the puzzle. Co-authored-by: Ankit Tiwari <ankitr.tiwari@gmail.com> Co-authored-by: Sylvia van Os <sylvia@hackerchick.me>
This commit is contained in:
@@ -12,13 +12,14 @@ import android.text.InputType;
|
||||
import android.util.Log;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@@ -47,6 +48,7 @@ public class ImportExportActivity extends CatimaAppCompatActivity
|
||||
private String importAlertTitle;
|
||||
private String importAlertMessage;
|
||||
private DataFormat importDataFormat;
|
||||
private String exportPassword;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState)
|
||||
@@ -88,7 +90,28 @@ public class ImportExportActivity extends CatimaAppCompatActivity
|
||||
@Override
|
||||
public void onClick(View v)
|
||||
{
|
||||
chooseFileWithIntent(intentCreateDocumentAction, CHOOSE_EXPORT_LOCATION);
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(ImportExportActivity.this);
|
||||
builder.setTitle(R.string.exportPassword);
|
||||
|
||||
FrameLayout container = new FrameLayout(ImportExportActivity.this);
|
||||
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
||||
params.leftMargin = 50;
|
||||
params.rightMargin = 50;
|
||||
|
||||
final EditText input = new EditText(ImportExportActivity.this);
|
||||
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
|
||||
input.setLayoutParams(params);
|
||||
input.setHint(R.string.exportPasswordHint);
|
||||
|
||||
container.addView(input);
|
||||
builder.setView(container);
|
||||
builder.setPositiveButton(R.string.ok, (dialogInterface, i) -> {
|
||||
exportPassword = input.getText().toString();
|
||||
chooseFileWithIntent(intentCreateDocumentAction, CHOOSE_EXPORT_LOCATION);
|
||||
});
|
||||
builder.setNegativeButton(R.string.cancel, (dialogInterface, i) -> dialogInterface.cancel());
|
||||
builder.show();
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
@@ -211,7 +234,7 @@ public class ImportExportActivity extends CatimaAppCompatActivity
|
||||
importExporter.execute();
|
||||
}
|
||||
|
||||
private void startExport(final OutputStream target, final Uri targetUri, final boolean closeWhenDone)
|
||||
private void startExport(final OutputStream target, final Uri targetUri,char[] password, final boolean closeWhenDone)
|
||||
{
|
||||
ImportExportTask.TaskCompleteListener listener = new ImportExportTask.TaskCompleteListener()
|
||||
{
|
||||
@@ -229,7 +252,7 @@ public class ImportExportActivity extends CatimaAppCompatActivity
|
||||
};
|
||||
|
||||
importExporter = new ImportExportTask(ImportExportActivity.this,
|
||||
DataFormat.Catima, target, listener);
|
||||
DataFormat.Catima, target,password, listener);
|
||||
importExporter.execute();
|
||||
}
|
||||
|
||||
@@ -407,6 +430,7 @@ public class ImportExportActivity extends CatimaAppCompatActivity
|
||||
{
|
||||
if (requestCode == CHOOSE_EXPORT_LOCATION)
|
||||
{
|
||||
|
||||
OutputStream writer;
|
||||
if (uri.getScheme() != null)
|
||||
{
|
||||
@@ -416,9 +440,8 @@ public class ImportExportActivity extends CatimaAppCompatActivity
|
||||
{
|
||||
writer = new FileOutputStream(new File(uri.toString()));
|
||||
}
|
||||
|
||||
Log.e(TAG, "Starting file export with: " + uri.toString());
|
||||
startExport(writer, uri, true);
|
||||
startExport(writer, uri,exportPassword.toCharArray(),true);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -35,7 +35,7 @@ class ImportExportTask extends AsyncTask<Void, Void, ImportExportResult>
|
||||
/**
|
||||
* Constructor which will setup a task for exporting to the given file
|
||||
*/
|
||||
ImportExportTask(Activity activity, DataFormat format, OutputStream output,
|
||||
ImportExportTask(Activity activity, DataFormat format, OutputStream output,char[] password,
|
||||
TaskCompleteListener listener)
|
||||
{
|
||||
super();
|
||||
@@ -43,6 +43,7 @@ class ImportExportTask extends AsyncTask<Void, Void, ImportExportResult>
|
||||
this.doImport = false;
|
||||
this.format = format;
|
||||
this.outputStream = output;
|
||||
this.password = password;
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
@@ -70,14 +71,14 @@ class ImportExportTask extends AsyncTask<Void, Void, ImportExportResult>
|
||||
return importResult;
|
||||
}
|
||||
|
||||
private ImportExportResult performExport(Context context, OutputStream stream, DBHelper db)
|
||||
private ImportExportResult performExport(Context context, OutputStream stream, DBHelper db,char[] password)
|
||||
{
|
||||
ImportExportResult result = ImportExportResult.GenericFailure;
|
||||
|
||||
try
|
||||
{
|
||||
OutputStreamWriter writer = new OutputStreamWriter(stream, StandardCharsets.UTF_8);
|
||||
result = MultiFormatExporter.exportData(context, db, stream, format);
|
||||
result = MultiFormatExporter.exportData(context, db, stream, format,password);
|
||||
writer.close();
|
||||
}
|
||||
catch (IOException e)
|
||||
@@ -118,7 +119,7 @@ class ImportExportTask extends AsyncTask<Void, Void, ImportExportResult>
|
||||
}
|
||||
else
|
||||
{
|
||||
result = performExport(activity.getApplicationContext(), outputStream, db);
|
||||
result = performExport(activity.getApplicationContext(), outputStream, db,password);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
@@ -6,6 +6,7 @@ import android.graphics.Bitmap;
|
||||
|
||||
import net.lingala.zip4j.io.outputstream.ZipOutputStream;
|
||||
import net.lingala.zip4j.model.ZipParameters;
|
||||
import net.lingala.zip4j.model.enums.EncryptionMethod;
|
||||
import net.lingala.zip4j.util.InternalZipConstants;
|
||||
|
||||
import org.apache.commons.csv.CSVFormat;
|
||||
@@ -30,14 +31,21 @@ import protect.card_locker.Utils;
|
||||
*/
|
||||
public class CatimaExporter implements Exporter
|
||||
{
|
||||
public void exportData(Context context, DBHelper db, OutputStream output) throws IOException, InterruptedException
|
||||
public void exportData(Context context, DBHelper db, OutputStream output,char[] password) throws IOException, InterruptedException
|
||||
{
|
||||
// Necessary vars
|
||||
int readLen;
|
||||
byte[] readBuffer = new byte[InternalZipConstants.BUFF_SIZE];
|
||||
|
||||
// Create zip output stream
|
||||
ZipOutputStream zipOutputStream = new ZipOutputStream(output);
|
||||
ZipOutputStream zipOutputStream;
|
||||
|
||||
if(password!=null && password.length>0){
|
||||
zipOutputStream = new ZipOutputStream(output,password);
|
||||
}
|
||||
else{
|
||||
zipOutputStream = new ZipOutputStream(output);
|
||||
}
|
||||
|
||||
// Generate CSV
|
||||
ByteArrayOutputStream catimaOutputStream = new ByteArrayOutputStream();
|
||||
@@ -45,8 +53,7 @@ public class CatimaExporter implements Exporter
|
||||
writeCSV(db, catimaOutputStreamWriter);
|
||||
|
||||
// Add CSV to zip file
|
||||
ZipParameters csvZipParameters = new ZipParameters();
|
||||
csvZipParameters.setFileNameInZip("catima.csv");
|
||||
ZipParameters csvZipParameters = createZipParameters("catima.csv",password);
|
||||
zipOutputStream.putNextEntry(csvZipParameters);
|
||||
InputStream csvInputStream = new ByteArrayInputStream(catimaOutputStream.toByteArray());
|
||||
while ((readLen = csvInputStream.read(readBuffer)) != -1) {
|
||||
@@ -71,8 +78,7 @@ public class CatimaExporter implements Exporter
|
||||
// If it exists, add to the .zip file
|
||||
Bitmap image = Utils.retrieveCardImage(context, card.id, front);
|
||||
if (image != null) {
|
||||
ZipParameters imageZipParameters = new ZipParameters();
|
||||
imageZipParameters.setFileNameInZip(Utils.getCardImageFileName(card.id, front));
|
||||
ZipParameters imageZipParameters = createZipParameters(Utils.getCardImageFileName(card.id, front),password);
|
||||
zipOutputStream.putNextEntry(imageZipParameters);
|
||||
InputStream imageInputStream = new ByteArrayInputStream(Utils.bitmapToByteArray(image));
|
||||
while ((readLen = imageInputStream.read(readBuffer)) != -1) {
|
||||
@@ -86,6 +92,16 @@ public class CatimaExporter implements Exporter
|
||||
zipOutputStream.close();
|
||||
}
|
||||
|
||||
private ZipParameters createZipParameters(String fileName, char[] password){
|
||||
ZipParameters zipParameters = new ZipParameters();
|
||||
zipParameters.setFileNameInZip(fileName);
|
||||
if(password!=null && password.length>0){
|
||||
zipParameters.setEncryptFiles(true);
|
||||
zipParameters.setEncryptionMethod(EncryptionMethod.AES);
|
||||
}
|
||||
return zipParameters;
|
||||
}
|
||||
|
||||
private void writeCSV(DBHelper db, OutputStreamWriter output) throws IOException, InterruptedException {
|
||||
CSVPrinter printer = new CSVPrinter(output, CSVFormat.RFC4180);
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ public class CatimaImporter implements Importer
|
||||
bufferedInputStream.mark(100);
|
||||
|
||||
// First, check if this is a zip file
|
||||
ZipInputStream zipInputStream = new ZipInputStream(bufferedInputStream);
|
||||
ZipInputStream zipInputStream = new ZipInputStream(bufferedInputStream,password);
|
||||
|
||||
boolean isZipFile = false;
|
||||
|
||||
|
||||
@@ -17,5 +17,5 @@ public interface Exporter
|
||||
* Export the database to the output stream in a given format.
|
||||
* @throws IOException
|
||||
*/
|
||||
void exportData(Context context, DBHelper db, OutputStream output) throws IOException, InterruptedException;
|
||||
void exportData(Context context, DBHelper db, OutputStream output,char[] password) throws IOException, InterruptedException;
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ public class MultiFormatExporter
|
||||
* another ImportExportResult otherwise. If not Success, partial data may have been
|
||||
* written to the output stream, and it should be discarded.
|
||||
*/
|
||||
public static ImportExportResult exportData(Context context, DBHelper db, OutputStream output, DataFormat format)
|
||||
public static ImportExportResult exportData(Context context, DBHelper db, OutputStream output, DataFormat format,char[] password)
|
||||
{
|
||||
Exporter exporter = null;
|
||||
|
||||
@@ -40,7 +40,7 @@ public class MultiFormatExporter
|
||||
{
|
||||
try
|
||||
{
|
||||
exporter.exportData(context, db, output);
|
||||
exporter.exportData(context, db, output,password);
|
||||
return ImportExportResult.Success;
|
||||
}
|
||||
catch(IOException e)
|
||||
|
||||
Reference in New Issue
Block a user