22
33import android .content .Context ;
44import android .util .Base64 ;
5+ import android .util .Log ;
56
67import com .facebook .android .crypto .keychain .AndroidConceal ;
78import com .facebook .android .crypto .keychain .SharedPrefsBackedKeyChain ;
89import com .facebook .crypto .Crypto ;
910import com .facebook .crypto .CryptoConfig ;
1011import com .facebook .crypto .Entity ;
12+ import com .facebook .crypto .exception .CryptoInitializationException ;
13+ import com .facebook .crypto .exception .KeyChainException ;
1114import 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 ;
1326import java .security .MessageDigest ;
1427import 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