Skip to content

Commit f09ad39

Browse files
committed
add new method
1 parent d9e7599 commit f09ad39

File tree

5 files changed

+531
-46
lines changed

5 files changed

+531
-46
lines changed
Lines changed: 54 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
package com.zeroone.concealexample;
22

3+
import android.content.Intent;
4+
import android.content.res.Resources;
5+
import android.graphics.Bitmap;
6+
import android.graphics.BitmapFactory;
7+
import android.net.Uri;
8+
import android.os.Environment;
39
import android.support.v7.app.AppCompatActivity;
410
import android.os.Bundle;
511
import android.util.Log;
612

713
import com.facebook.crypto.CryptoConfig;
8-
import com.facebook.crypto.Entity;
9-
import com.zeroone.conceal.ConcealCrypto;
1014
import com.zeroone.conceal.ConcealPrefRepository;
1115

16+
import java.io.File;
17+
1218
public class MainActivity extends AppCompatActivity {
1319
@Override
1420
protected void onCreate(Bundle savedInstanceState) {
@@ -21,46 +27,53 @@ protected void onCreate(Bundle savedInstanceState) {
2127
.sharedPrefsBackedKeyChain(CryptoConfig.KEY_256)
2228
.enableCrypto(true,true)
2329
.createPassword("Android")
30+
.setFolderName("testing")
2431
.create();
2532

26-
27-
28-
new ConcealPrefRepository.Editor()
29-
.putString("Bellow","Hello")
30-
.putInt("Number",1000000)
31-
.putBoolean("enable",true)
32-
.apply();
33-
34-
Log.d("DisplayPref",concealPrefRepository.getString("Bellow"));
35-
Log.d("DisplayPref", String.valueOf(concealPrefRepository.getInt("Number")));
36-
37-
38-
String test = "My Name Hafiq";
39-
String test2 = "Live in Semenyih";
40-
41-
ConcealCrypto concealCrypto = new ConcealCrypto(this,CryptoConfig.KEY_256);
42-
concealCrypto.setEnableCrypto(true);
43-
concealCrypto.setEntityPassword("Android");
44-
concealCrypto.setEntityPassword(Entity.create("Android"));
45-
46-
String cipher = concealCrypto.obscure(test);
47-
String cipher2 = concealCrypto.obscure(test2);
48-
49-
Log.d("DisplayCC", "Name: "+concealCrypto.obscure(test)+" ===== "+concealCrypto.deObscure(cipher));
50-
Log.d("DisplayCC", "Live: "+concealCrypto.obscure(test2)+" ===== "+concealCrypto.deObscure(cipher2));
51-
52-
53-
ConcealCrypto concealCrypto1 = new ConcealCrypto.CryptoBuilder(this)
54-
.setEnableCrypto(true)
55-
.setKeyChain(CryptoConfig.KEY_256)
56-
.createPassword("Mac OSX")
57-
.create();
58-
59-
cipher = concealCrypto1.obscure(test);
60-
cipher2 = concealCrypto1.obscure(test2);
61-
62-
Log.d("DisplayCC2", "Name: "+concealCrypto1.obscure(test)+" ===== "+concealCrypto1.deObscure(cipher));
63-
Log.d("DisplayCC2", "Live: "+concealCrypto1.obscure(test2)+" ===== "+concealCrypto1.deObscure(cipher2));
64-
33+
//// File getFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/.files/here.pdf");
34+
//
35+
// new ConcealPrefRepository.Editor()
36+
// .putString("Bellow","Hello")
37+
// .putInt("Number",1000000)
38+
// .putBoolean("enable",true)
39+
// .putImage("images", BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
40+
//// .putFile("filing",getFile,true)
41+
// .apply();
42+
//
43+
// Log.d("DisplayPref",concealPrefRepository.getString("Bellow"));
44+
// Log.d("DisplayPref",String.valueOf(concealPrefRepository.getInt("Number")));
45+
// Log.d("DisplayPref",concealPrefRepository.getString("images"));
46+
// Log.d("DisplayPref",concealPrefRepository.getString("filing"));
47+
48+
// File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/.testing/files/conceal_enc_here.pdf");
49+
File file = concealPrefRepository.getFile("filing",true);
50+
Log.d("TAG",file.getAbsolutePath());
51+
52+
// String test = "My Name Hafiq";
53+
// String test2 = "Live in Semenyih";
54+
//
55+
// ConcealCrypto concealCrypto = new ConcealCrypto(this,CryptoConfig.KEY_256);
56+
// concealCrypto.setEnableCrypto(true);
57+
// concealCrypto.setEntityPassword("Android");
58+
// concealCrypto.setEntityPassword(Entity.create("Android"));
59+
//
60+
// String cipher = concealCrypto.obscure(test);
61+
// String cipher2 = concealCrypto.obscure(test2);
62+
//
63+
// Log.d("DisplayCC", "Name: "+concealCrypto.obscure(test)+" ===== "+concealCrypto.deObscure(cipher));
64+
// Log.d("DisplayCC", "Live: "+concealCrypto.obscure(test2)+" ===== "+concealCrypto.deObscure(cipher2));
65+
//
66+
//
67+
// ConcealCrypto concealCrypto1 = new ConcealCrypto.CryptoBuilder(this)
68+
// .setEnableCrypto(true)
69+
// .setKeyChain(CryptoConfig.KEY_256)
70+
// .createPassword("Mac OSX")
71+
// .create();
72+
//
73+
// cipher = concealCrypto1.obscure(test);
74+
// cipher2 = concealCrypto1.obscure(test2);
75+
//
76+
// Log.d("DisplayCC2", "Name: "+concealCrypto1.obscure(test)+" ===== "+concealCrypto1.deObscure(cipher));
77+
// Log.d("DisplayCC2", "Live: "+concealCrypto1.obscure(test2)+" ===== "+concealCrypto1.deObscure(cipher2));
6578
}
6679
}

library/src/main/java/com/zeroone/conceal/ConcealCrypto.java

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,35 @@
22

33
import android.content.Context;
44
import android.util.Base64;
5+
import android.util.Log;
56

67
import com.facebook.android.crypto.keychain.AndroidConceal;
78
import com.facebook.android.crypto.keychain.SharedPrefsBackedKeyChain;
89
import com.facebook.crypto.Crypto;
910
import com.facebook.crypto.CryptoConfig;
1011
import com.facebook.crypto.Entity;
12+
import com.facebook.crypto.exception.CryptoInitializationException;
13+
import com.facebook.crypto.exception.KeyChainException;
1114
import com.facebook.crypto.keychain.KeyChain;
15+
import com.zeroone.conceal.helper.FileUtils;
1216

17+
import java.io.BufferedInputStream;
18+
import java.io.BufferedOutputStream;
19+
import java.io.ByteArrayOutputStream;
20+
import java.io.File;
21+
import java.io.FileInputStream;
22+
import java.io.FileOutputStream;
23+
import java.io.IOException;
24+
import java.io.InputStream;
25+
import java.io.OutputStream;
1326
import java.security.MessageDigest;
1427
import java.security.NoSuchAlgorithmException;
1528

29+
import static com.zeroone.conceal.helper.Constant.DEFAULT_DIRECTORY;
30+
import static com.zeroone.conceal.helper.Constant.DEFAULT_FILES_FOLDER;
31+
import static com.zeroone.conceal.helper.Constant.DEFAULT_MAIN_FOLDER;
32+
import static com.zeroone.conceal.helper.Constant.DEFAULT_PREFIX_FILENAME;
33+
1634
/**
1735
* @author : hafiq on 23/03/2017.
1836
*/
@@ -24,12 +42,16 @@ public class ConcealCrypto {
2442
private Entity mEntityPassword = Entity.create(BuildConfig.APPLICATION_ID);
2543
private boolean enableCrypto=true;
2644
private boolean enableKeyCrypt=true;
45+
private String MAIN_DIRECTORY;
2746

2847
public ConcealCrypto(CryptoBuilder builder){
2948
crypto = builder.crypto;
3049
mEntityPassword = builder.mEntityPassword;
3150
enableCrypto = builder.mEnabledCrypto;
3251
enableKeyCrypt = builder.mEnableCryptKey;
52+
MAIN_DIRECTORY = builder.mFolderName;
53+
54+
if (MAIN_DIRECTORY == null) MAIN_DIRECTORY = DEFAULT_MAIN_FOLDER;
3355
}
3456

3557
public ConcealCrypto(Context context,CryptoConfig config){
@@ -52,20 +74,34 @@ public void setEnableKeyCrypto(boolean enableKeyCrypt) {
5274
this.enableKeyCrypt = enableKeyCrypt;
5375
}
5476

77+
public Crypto getCrypto(){
78+
return crypto;
79+
}
80+
81+
private String makeDirectory(){
82+
if (MAIN_DIRECTORY == null) MAIN_DIRECTORY = DEFAULT_MAIN_FOLDER;
83+
84+
return DEFAULT_DIRECTORY+ MAIN_DIRECTORY +"/";
85+
}
86+
87+
private String makeFileDirectory(){
88+
return makeDirectory()+DEFAULT_FILES_FOLDER;
89+
}
90+
5591
public void clearCrypto(){
5692
if (crypto.isAvailable()){
5793
keyChain.destroyKeys();
5894
}
5995
}
6096

61-
6297
//Encrypt
6398
public String obscure(String plain){
6499
if (enableCrypto) {
65100
try {
66101
byte[] a = crypto.encrypt(plain.getBytes(), mEntityPassword);
67102
return Base64.encodeToString(a, Base64.DEFAULT);
68103
} catch (Exception e) {
104+
e.printStackTrace();
69105
return null;
70106
}
71107
}
@@ -74,12 +110,46 @@ public String obscure(String plain){
74110
}
75111
}
76112

113+
public File obscureFile(File file,boolean deleteOldFile){
114+
if (enableCrypto) {
115+
try {
116+
File mEncryptedFile = new File(makeDirectory()+DEFAULT_PREFIX_FILENAME+file.getName());
117+
OutputStream fileStream = new BufferedOutputStream(new FileOutputStream(mEncryptedFile));
118+
OutputStream outputStream = crypto.getCipherOutputStream(fileStream, mEntityPassword);
119+
120+
int read;
121+
byte[] buffer = new byte[1024];
122+
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
123+
while ((read = bis.read(buffer)) != -1) {
124+
outputStream.write(buffer, 0, read);
125+
}
126+
outputStream.close();
127+
bis.close();
128+
129+
if (deleteOldFile)
130+
file.delete();
131+
132+
File pathDir = new File(makeFileDirectory());
133+
return FileUtils.moveFile(mEncryptedFile,pathDir);
134+
135+
} catch (KeyChainException | CryptoInitializationException | IOException e) {
136+
e.printStackTrace();
137+
return null;
138+
}
139+
}
140+
else {
141+
return file;
142+
}
143+
}
144+
77145
//Decrypt
78146
public String deObscure(String cipher){
79147
if (enableCrypto) {
148+
if (cipher == null) return null;
80149
try {
81150
return new String(crypto.decrypt(Base64.decode(cipher, Base64.DEFAULT), mEntityPassword));
82151
} catch (Exception e) {
152+
e.printStackTrace();
83153
return null;
84154
}
85155
}
@@ -88,6 +158,46 @@ public String deObscure(String cipher){
88158
}
89159
}
90160

161+
public File deObscureFile(File file,boolean deleteOldFile){
162+
if (enableCrypto) {
163+
try {
164+
if (file.getName().contains(DEFAULT_PREFIX_FILENAME)) {
165+
File mDecryptedFile = new File(makeDirectory() + file.getName().replace(DEFAULT_PREFIX_FILENAME,""));
166+
167+
InputStream inputStream = crypto.getCipherInputStream(new FileInputStream(file), mEntityPassword);
168+
ByteArrayOutputStream out = new ByteArrayOutputStream();
169+
170+
OutputStream outputStream = new FileOutputStream(mDecryptedFile);
171+
BufferedInputStream bis = new BufferedInputStream(inputStream);
172+
int mRead;
173+
byte[] mBuffer = new byte[1024];
174+
while ((mRead = bis.read(mBuffer)) != -1) {
175+
outputStream.write(mBuffer, 0, mRead);
176+
}
177+
bis.close();
178+
out.writeTo(outputStream);
179+
inputStream.close();
180+
outputStream.close();
181+
out.close();
182+
183+
if (deleteOldFile)
184+
file.delete();
185+
186+
File pathDir = new File(makeFileDirectory());
187+
return FileUtils.moveFile(mDecryptedFile, pathDir);
188+
}
189+
190+
return null;
191+
192+
} catch (KeyChainException | CryptoInitializationException | IOException e) {
193+
e.printStackTrace();
194+
return null;
195+
}
196+
}
197+
198+
return file;
199+
}
200+
91201
//SHA-256 hash key Message Digest
92202
public String hashKey(String key) {
93203
if (enableKeyCrypt) {
@@ -116,6 +226,7 @@ public static class CryptoBuilder{
116226
private boolean mEnableCryptKey = false;
117227
private Entity mEntityPassword = null;
118228
private String mEntityPasswordRaw = BuildConfig.APPLICATION_ID;
229+
private String mFolderName;
119230

120231
public CryptoBuilder(Context context) {
121232
this.context = context;
@@ -141,7 +252,17 @@ public CryptoBuilder createPassword(String password){
141252
return this;
142253
}
143254

255+
/***
256+
* @param folderName - Main Folder to be stored
257+
* @return - CryptoBuilder
258+
*/
259+
public CryptoBuilder setStoredFolder(String folderName){
260+
mFolderName = (folderName!=null)?folderName:null;
261+
return this;
262+
}
263+
144264
public ConcealCrypto create(){
265+
145266
mEntityPassword = Entity.create(Base64.encodeToString(mEntityPasswordRaw.getBytes(),Base64.DEFAULT));
146267
makeKeyChain = new SharedPrefsBackedKeyChain(context,(mKeyChain==null)?CryptoConfig.KEY_256:mKeyChain);
147268

0 commit comments

Comments
 (0)