Skip to content

Commit

Permalink
Completely finished Google Drive integration - need code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Javinator9889 committed Apr 25, 2018
1 parent 4866934 commit 141f38d
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 43 deletions.
Binary file modified APP/.idea/caches/build_file_checksums.ser
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,24 @@ public void createFileInAppFolder(@NonNull final ClassContainer dataToBackup) {
.continueWith(task -> {
DriveFolder parent = appFolderTask.getResult();
DriveContents contents = createContentsTask.getResult();
Map<SealedObject, CipherOutputStream> encryptedBackup;
//Map<SealedObject, CipherOutputStream> encryptedBackup;
String password = ioManager.readPassword();
Log.d(TAG, "Password: " + password);
try (OutputStream outputStream = contents.getOutputStream()) {
if (password != null) {
FileCipher cipher = FileCipher.newInstance(password, null);
generatedIv = cipher.getIv();
Log.d(TAG, Arrays.toString(generatedIv));
encryptedBackup = cipher.encrypt(dataToBackup, outputStream);
/*encryptedBackup = cipher.encrypt(dataToBackup, outputStream);
CipherOutputStream createdCipher =
encryptedBackup.values().iterator().next();
SealedObject createdSealedObject =
encryptedBackup.keySet().iterator().next();
ObjectOutputStream encryptedClassWriter =
new ObjectOutputStream(createdCipher);
encryptedClassWriter.writeObject(createdSealedObject);
encryptedClassWriter.close();
encryptedClassWriter.close();*/
cipher.newEncrypt(dataToBackup, outputStream);
}
}
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
import com.google.android.gms.drive.DriveContents;
import com.google.android.gms.drive.DriveFile;
import com.google.android.gms.drive.DriveResourceClient;
import com.google.android.gms.drive.events.OpenFileCallback;
import com.google.android.gms.tasks.Task;
import com.google.common.hash.Hashing;
import com.google.common.io.ByteStreams;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -118,7 +121,7 @@ public void retrieveIvVector(DriveFile file) {

@Override
public void retrieveContents(DriveFile file) {
Task<DriveContents> openFileTask = resourceClient.openFile(file, DriveFile.MODE_READ_ONLY);
/*Task<DriveContents> openFileTask = resourceClient.openFile(file, DriveFile.MODE_READ_ONLY);
openFileTask
.continueWith(task -> {
DriveContents contents = task.getResult();
Expand All @@ -130,8 +133,8 @@ public void retrieveContents(DriveFile file) {
}
completeDownload();
return resourceClient.discardContents(contents);
});
/*mProgressBar.show();
});*/
mProgressBar.show();
resourceClient.openFile(file, DriveFile.MODE_READ_ONLY, new OpenFileCallback() {
@Override
public void onProgress(long bytesDownloaded, long bytesExpected) {
Expand All @@ -144,7 +147,17 @@ public void onProgress(long bytesDownloaded, long bytesExpected) {
public void onContents(@NonNull DriveContents driveContents) {
mProgressBar.setProgress(100);
mProgressBar.dismiss();
final StringBuilder passwordBuilder = new StringBuilder();
try {
try (InputStream obtainedFile = driveContents.getInputStream()){
IOManager io = IOManager.newInstance(driveContext);
io.writeDownloadedClass(obtainedFile);
completeDownload();
}
} catch (IOException e) {
Log.e(TAG, "IOException when retrieving contents", e);
}
//return resourceClient.discardContents(driveContents);
/*final StringBuilder passwordBuilder = new StringBuilder();
new MaterialDialog.Builder(
driveContext)
.title(R.string.put_pass)
Expand All @@ -168,7 +181,7 @@ public void onContents(@NonNull DriveContents driveContents) {
e.printStackTrace();
}
})
.show();
.show();*/
}

@Override
Expand All @@ -191,14 +204,17 @@ private void completeDownload() {
.input(null, null, false,
((dialog, input) -> passwordBuilder.append(input)))
.onAny(((dialog, which) -> {
String password = passwordBuilder.toString();
String password = Hashing.sha256()
.hashString(passwordBuilder.toString(), StandardCharsets.UTF_8)
.toString();
System.out.println(password);
try {
FileCipher fileDecrypt = FileCipher.newInstance(password,
iv);
ClassContainer restoredData =
(ClassContainer) fileDecrypt.decrypt(obtainedStream);
Log.d(TAG, restoredData.toString());
io.deleteDownloadedClass();
} catch (IOException e) {
Log.e(TAG, "IOException captured. Message: " +
e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,29 @@

import android.app.Activity;
import android.content.Context;
import android.content.IntentSender;
import android.support.annotation.NonNull;
import android.util.Log;
import android.widget.Toast;

import com.afollestad.materialdialogs.MaterialDialog;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.drive.Drive;
import com.google.android.gms.drive.DriveClient;
import com.google.android.gms.drive.DriveFolder;
import com.google.android.gms.drive.DriveId;
import com.google.android.gms.drive.DriveResourceClient;
import com.google.android.gms.drive.Metadata;
import com.google.android.gms.drive.MetadataBuffer;
import com.google.android.gms.drive.OpenFileActivityOptions;
import com.google.android.gms.drive.query.Filter;
import com.google.android.gms.drive.query.Filters;
import com.google.android.gms.drive.query.Query;
import com.google.android.gms.drive.query.SearchableField;
import com.google.android.gms.drive.query.SortOrder;
import com.google.android.gms.drive.query.SortableField;
import com.google.android.gms.drive.widget.DataBufferAdapter;
import com.google.android.gms.tasks.Continuation;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.TaskCompletionSource;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import javinator9889.securepass.R;
Expand Down Expand Up @@ -67,28 +61,46 @@ public void signIn() {
mSignInClient.signIn();
}

private boolean isAbleToSignIn() {
if (!mSignInClient.isSignedIn()) {
GoogleSignInAccount latestSignedInAccount = GoogleSignIn
.getLastSignedInAccount(driveContext);
if (latestSignedInAccount != null) {
mDriveResourceClient = Drive
.getDriveResourceClient(driveContext, latestSignedInAccount);
return true;
}
else {
Toast.makeText(driveContext, driveContext.getString(R.string.sign_in_first),
Toast.LENGTH_LONG).show();
return false;
}
} else
return true;
}

@Override
public void uploadFile(@NonNull ClassContainer dataToBackup) {
if (!mSignInClient.isSignedIn())
signIn();
CreateFileInAppFolder createFileTask = new CreateFileInAppFolder(
driveContext,
mainActivity,
mDriveResourceClient);
//createFileTask.createFileInAppFolder(dataToBackup);
queryFilesAndDelete(createFileTask, dataToBackup);
if (isAbleToSignIn()) {
CreateFileInAppFolder createFileTask = new CreateFileInAppFolder(
driveContext,
mainActivity,
mDriveResourceClient);
//createFileTask.createFileInAppFolder(dataToBackup);
queryFilesAndDelete(createFileTask, dataToBackup);
}
}

@Override
public synchronized void restoreData() {
if (!mSignInClient.isSignedIn())
signIn();
MaterialDialog md = new MaterialDialog.Builder(driveContext)
.progress(true, 0)
.title(R.string.wait)
.build();
md.show();
queryFiles(md);
public void restoreData() {
if (isAbleToSignIn()) {
/*MaterialDialog md = new MaterialDialog.Builder(driveContext)
.progress(true, 0)
.title(R.string.wait)
.build();
md.show();*/
queryFiles();
}
}

private void deleteFiles(@NonNull CreateFileInAppFolder createFileTask,
Expand All @@ -111,7 +123,7 @@ private void deleteFiles(@NonNull CreateFileInAppFolder createFileTask,
createFileTask.createFileInAppFolder(dataToBackup);
}

private void continueWithDownload(MaterialDialog md) {
private void continueWithDownload() {
RetrieveContentWithDownloadProgress retrieveContentClass =
new RetrieveContentWithDownloadProgress(
driveContext,
Expand All @@ -138,7 +150,7 @@ private void continueWithDownload(MaterialDialog md) {
break;
}
}
md.dismiss();
//md.dismiss();
try {
retrieveContentClass.retrieveIvVector(ivId.asDriveFile());
retrieveContentClass.retrieveContents(classId.asDriveFile());
Expand Down Expand Up @@ -169,7 +181,7 @@ private void queryFilesAndDelete(@NonNull CreateFileInAppFolder createFileTask,
});
}

private void queryFiles(MaterialDialog md) {
private void queryFiles() {
SortOrder sortOrder = new SortOrder.Builder().addSortAscending(SortableField.CREATED_DATE)
.build();
Query query = new Query.Builder()
Expand All @@ -185,7 +197,7 @@ private void queryFiles(MaterialDialog md) {
.addOnSuccessListener(mainActivity,
metadata -> {
resultsAdapter.append(metadata);
continueWithDownload(md);
continueWithDownload();
});
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,12 @@ public InputStream readDownloadedClass() {
return null;
}
}

public void deleteDownloadedClass() {
String filename = filesDir.getAbsolutePath() + "/class.bck";

File fileToDelete = new File(filename);
if (!fileToDelete.delete())
Log.e("Delete IO", "There was an error while trying to delete class.bck");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import java.io.BufferedInputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.lang.reflect.Constructor;
Expand All @@ -21,6 +23,7 @@
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -34,6 +37,7 @@
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import javinator9889.securepass.data.container.ClassContainer;
import javinator9889.securepass.util.values.Constants.CIPHER.FILE;

/**
Expand Down Expand Up @@ -80,8 +84,7 @@ public byte[] getIv() {
public Map<SealedObject, CipherOutputStream> encrypt(@NonNull Serializable classToEncrypt,
@NonNull OutputStream outputStream)
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
IOException, InvalidAlgorithmParameterException
{
IOException, InvalidAlgorithmParameterException {
try {
SecretKeySpec secretKeySpec = new SecretKeySpec(key, FILE.ALGORITHM);
Cipher cipher = Cipher.getInstance(FILE.TRANSFORMATION);
Expand All @@ -98,10 +101,30 @@ public Map<SealedObject, CipherOutputStream> encrypt(@NonNull Serializable class
}
}

public void newEncrypt(@NonNull ClassContainer dataToEncrypt,
@NonNull OutputStream outputStream) throws IOException,
NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException,
InvalidKeyException {
try {
System.out.print("Password in bytes: " + Arrays.toString(key));
SecretKeySpec keySpec = new SecretKeySpec(key, FILE.ALGORITHM);
Cipher cipher = Cipher.getInstance(FILE.TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivspec);
SealedObject sealedObject = new SealedObject(dataToEncrypt, cipher);

CipherOutputStream cipherFile = new CipherOutputStream(outputStream, cipher);
ObjectOutputStream outputFile = new ObjectOutputStream(cipherFile);
outputFile.writeObject(sealedObject);
outputFile.close();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
}

public Object decrypt(@NonNull InputStream inputStream) throws IOException,
NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
InvalidAlgorithmParameterException
{
InvalidAlgorithmParameterException {
System.out.print("Password in bytes: " + Arrays.toString(key));
SecretKeySpec secretKeySpec = new SecretKeySpec(key, FILE.ALGORITHM);
Cipher cipher = Cipher.getInstance(FILE.TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivspec);
Expand Down
1 change: 1 addition & 0 deletions APP/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -671,4 +671,5 @@ copy of the Program in return for a fee.\n\n
<string name="put_pass">Enter password:</string>
<string name="pass_need">Please, enter the password you used when you created this backup. It may not be the same as the one you have right now</string>
<string name="read_failed">There was an error while trying to restore data</string>
<string name="sign_in_first">Please, sign-in first</string>
</resources>
2 changes: 1 addition & 1 deletion APP/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.1'
classpath 'com.android.tools.build:gradle:3.1.2'


// NOTE: Do not place your application dependencies here; they belong
Expand Down

0 comments on commit 141f38d

Please sign in to comment.