Skip to content

Commit

Permalink
Add delete single string and all string functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ptsiogas4 committed May 29, 2019
1 parent 057d3d2 commit b39d0af
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 35 deletions.
8 changes: 8 additions & 0 deletions app/src/main/java/ptsiogas/gr/securestorage/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ class MainActivity : AppCompatActivity() {
wrongPassTestButton.setOnClickListener {
resultTextView.text = SecureBoxHelper.instance.decryptString("testVar", "wrongPass")
}

deleteTestButton.setOnClickListener {
if (SecureBoxHelper.instance.deleteString("testVar")) {
resultTextView.text = "deleted succesfully"
} else {
resultTextView.text = "error!"
}
}
}


Expand Down
14 changes: 12 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginTop="64dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
Expand All @@ -38,9 +38,19 @@
android:layout_marginTop="16dp"
android:id="@+id/wrongPassTestButton"/>

<Button android:layout_width="140dp" android:layout_height="40dp"
android:background="@color/colorPrimary"
android:textColor="#ffffff"
android:text="Delete variable"
app:layout_constraintTop_toBottomOf="@id/wrongPassTestButton"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="16dp"
android:id="@+id/deleteTestButton"/>

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/resultTextView"
app:layout_constraintTop_toBottomOf="@id/wrongPassTestButton"
app:layout_constraintTop_toBottomOf="@id/deleteTestButton"
android:layout_marginTop="32dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package ptsiogas.gr.securebox

import android.content.Context
import android.support.test.InstrumentationRegistry
import junit.framework.Assert.assertEquals
import org.junit.Before
import org.junit.Test

class SecureBoxHelperTests {

lateinit var instrumentationContext: Context

@Before
fun setup() {
instrumentationContext = InstrumentationRegistry.getInstrumentation().context
SecureBoxHelper.instance.init(instrumentationContext)
}

@Test
fun encrypt_isCorrect() {
assert(SecureBoxHelper.instance.encryptString("testVar", "This is a test", "testPassword"))
}

@Test
fun decrypt_isCorrect() {
val varName = "testVar"
val message = "This is a test"
val password = "testPassword"
SecureBoxHelper.instance.encryptString(varName, message, password)
assertEquals(message, SecureBoxHelper.instance.decryptString(varName, password))
}

@Test
fun delete_string_isCorrect() {
val varName = "testVar"
val message = "This is a test"
val password = "testPassword"
SecureBoxHelper.instance.encryptString(varName, message, password)
assert(SecureBoxHelper.instance.deleteString(varName))
}

@Test
fun delete_all_strings_isCorrect() {
val varName = "testVar"
val varName2 = "testVar2"
val message = "This is a test"
val password = "testPassword"
SecureBoxHelper.instance.encryptString(varName, message, password)
SecureBoxHelper.instance.encryptString(varName2, message, password)
assert(SecureBoxHelper.instance.deleteAllStrings())
}
}
68 changes: 65 additions & 3 deletions securebox/src/main/java/ptsiogas/gr/securebox/SecureBoxHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ptsiogas.gr.securebox

import android.content.Context
import android.util.Log
import java.io.File
import java.io.IOException
import java.io.ObjectInputStream
import java.security.SecureRandom
Expand All @@ -12,7 +13,6 @@ import javax.crypto.spec.PBEKeySpec
import javax.crypto.spec.SecretKeySpec
import java.io.ObjectOutputStream


class SecureBoxHelper {
companion object {
private var secureBoxHelper: SecureBoxHelper? = null
Expand Down Expand Up @@ -51,6 +51,7 @@ class SecureBoxHelper {
val oos = ObjectOutputStream(fos)
oos.writeObject(map)
oos.close()
storeVarName(variableName)
return true
} catch (e: Exception) {
e.printStackTrace()
Expand Down Expand Up @@ -86,6 +87,42 @@ class SecureBoxHelper {
return null
}

fun deleteString(variableName: String): Boolean {
if (!checkInit()) {
return false
}
val dir = this.context?.filesDir
val file = File(dir, variableName + ".dat")
if (file.delete()) {
Log.e("SecureBoxHelper", "variable deleted successfully")
return true
}
return false
}

fun deleteAllStrings(): Boolean {
if (!checkInit()) {
return false
}
val map = loadVarNames()
for (entry in map.entries) {
if (!deleteString(entry.key)) {
return false
}
}
try {
val deletedMap = HashMap<String, Boolean>()
val fos = context!!.openFileOutput("varNames_secureHelper.dat", Context.MODE_PRIVATE)
val oos = ObjectOutputStream(fos)
oos.writeObject(deletedMap)
oos.close()
} catch (e: Exception) {
Log.e("SecureBoxHelper", "delete all exception", e)
return false
}
return true
}

private fun encryptString(plainTextBytes: ByteArray, passwordString: String): HashMap<String, ByteArray> {
val map = HashMap<String, ByteArray>()

Expand Down Expand Up @@ -117,7 +154,7 @@ class SecureBoxHelper {
map["iv"] = iv
map["encrypted"] = encrypted
} catch (e: Exception) {
Log.e("MYAPP", "encryption exception", e)
Log.e("SecureBoxHelper", "encryption exception", e)
}

return map
Expand All @@ -143,9 +180,34 @@ class SecureBoxHelper {
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec)
decrypted = cipher.doFinal(encrypted)
} catch (e: Exception) {
Log.e("MYAPP", "decryption exception", e)
Log.e("SecureBoxHelper", "decryption exception", e)
}

return decrypted
}

private fun storeVarName(variableName: String) {
try {
val map = loadVarNames()
map[variableName] = true
val fos = context!!.openFileOutput("varNames_secureHelper.dat", Context.MODE_PRIVATE)
val oos = ObjectOutputStream(fos)
oos.writeObject(map)
oos.close()
} catch (e: Exception) {
Log.e("SecureBoxHelper", "store exception", e)
}
}

private fun loadVarNames(): HashMap<String, Boolean> {
try {
val fileInputStream = context!!.openFileInput("varNames_secureHelper.dat")
val objectInputStream = ObjectInputStream(fileInputStream)
return objectInputStream.readObject() as HashMap<String, Boolean>
} catch (e: Exception) {
Log.e("SecureBoxHelper", "load exception", e)
}
return HashMap<String, Boolean>()
}

}

This file was deleted.

0 comments on commit b39d0af

Please sign in to comment.