Skip to content

Commit e890efc

Browse files
author
Ryan
committed
Throw exception on bad base64 input. Implement expandable cards. Fix UI bugs.
1 parent b64e27a commit e890efc

36 files changed

+492
-182
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ eCryptSymmetric.encrypt (input, password,
6868
object : ECResultListener {
6969

7070
// Optional
71-
override fun onProgress(newBytes: Int, bytesProcessed: Long) {
71+
override fun onProgress(newBytes: Int, bytesProcessed: Long, totalBytes: Long) {
7272

7373
}
7474

@@ -90,7 +90,7 @@ eCryptSymmetric.decrypt(input, password,
9090
object : ECResultListener {
9191

9292
// Optional
93-
override fun onProgress(newBytes: Int, bytesProcessed: Long) {
93+
override fun onProgress(newBytes: Int, bytesProcessed: Long, totalBytes: Long) {
9494

9595
}
9696

@@ -117,7 +117,7 @@ eCryptAsymmetric.generateKeyPair(object : ECRSAKeyPairListener {
117117
object : ECResultListener {
118118

119119
// Optional
120-
override fun onProgress(newBytes: Int, bytesProcessed: Long) {
120+
override fun onProgress(newBytes: Int, bytesProcessed: Long, totalBytes: Long) {
121121

122122
}
123123

@@ -146,7 +146,7 @@ eCryptAsymmetric.decrypt(input, privateKey,
146146
object : ECResultListener {
147147

148148
// Optional
149-
override fun onProgress(newBytes: Int, bytesProcessed: Long) {
149+
override fun onProgress(newBytes: Int, bytesProcessed: Long, totalBytes: Long) {
150150

151151
}
152152

@@ -175,7 +175,7 @@ eCryptKeys.genRSAKeyPair(object : ECRSAKeyPairListener {
175175
object : ECResultListener {
176176

177177
// Optional
178-
override fun onProgress(newBytes: Int, bytesProcessed: Long) {
178+
override fun onProgress(newBytes: Int, bytesProcessed: Long, totalBytes: Long) {
179179

180180
}
181181

@@ -217,7 +217,7 @@ eCryptHash.calculate(input, hashAlgorithm, // from ECHashAlgorithms
217217
object : ECResultListener {
218218

219219
// Optional
220-
override fun onProgress(newBytes: Int, bytesProcessed: Long) {
220+
override fun onProgress(newBytes: Int, bytesProcessed: Long, totalBytes: Long) {
221221

222222
}
223223

appKotlin/build.gradle

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ android {
2020
applicationId "com.pvryan.easycryptsample"
2121
minSdkVersion 19
2222
targetSdkVersion 27
23-
versionCode 1
24-
versionName "1.0"
23+
versionCode 2
24+
versionName "2.1"
2525
vectorDrawables.useSupportLibrary = true
2626
signingConfig signingConfigs.config
2727
}
@@ -43,6 +43,11 @@ android {
4343
targetCompatibility JavaVersion.VERSION_1_8
4444
}
4545
}
46+
47+
repositories {
48+
jcenter()
49+
}
50+
4651
dependencies {
4752
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
4853
implementation "org.jetbrains.anko:anko:$anko_version"
@@ -51,10 +56,6 @@ dependencies {
5156
implementation "com.android.support:recyclerview-v7:$support_version"
5257
implementation "com.android.support:appcompat-v7:$support_version"
5358
implementation "com.android.support:design:$support_version"
54-
implementation 'com.pvryan.easycrypt:easycrypt:1.3.0'
55-
implementation 'com.nulab-inc:zxcvbn:1.2.3'
56-
}
57-
58-
repositories {
59-
jcenter()
59+
//implementation 'com.pvryan.easycrypt:easycrypt:1.3.1'
60+
implementation project(':easycrypt')
6061
}

appKotlin/src/main/java/com/pvryan/easycryptsample/action/fragments/FragmentAsymmetricFile.kt

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import android.content.Intent
2020
import android.os.Bundle
2121
import android.os.Environment
2222
import android.support.v4.app.Fragment
23+
import android.util.Log
2324
import android.view.LayoutInflater
2425
import android.view.View
2526
import android.view.ViewGroup
@@ -28,10 +29,9 @@ import com.pvryan.easycrypt.ECResultListener
2829
import com.pvryan.easycrypt.asymmetric.ECAsymmetric
2930
import com.pvryan.easycrypt.asymmetric.ECRSAKeyPairListener
3031
import com.pvryan.easycrypt.asymmetric.ECVerifiedListener
31-
import com.pvryan.easycrypt.hash.ECHash
32-
import com.pvryan.easycrypt.hash.ECHashAlgorithms
33-
import com.pvryan.easycrypt.symmetric.ECSymmetric
3432
import com.pvryan.easycryptsample.R
33+
import com.pvryan.easycryptsample.extensions.hide
34+
import com.pvryan.easycryptsample.extensions.show
3535
import kotlinx.android.synthetic.main.fragment_asymmetric_file.*
3636
import org.jetbrains.anko.AnkoLogger
3737
import org.jetbrains.anko.support.v4.longToast
@@ -44,19 +44,17 @@ import java.security.interfaces.RSAPublicKey
4444

4545
class FragmentAsymmetricFile : Fragment(), AnkoLogger, ECResultListener {
4646

47-
private val _rCHash = 2
48-
private val _rCEncrypt = 3
49-
private val _rCDecrypt = 4
50-
private val _rCSign = 5
51-
private val _rCVerify = 6
52-
private val eCryptSymmetric = ECSymmetric()
47+
private val _rCEncrypt = 2
48+
private val _rCDecrypt = 3
49+
private val _rCSign = 4
50+
private val _rCVerify = 5
5351
private val eCryptAsymmetric = ECAsymmetric()
54-
private val eCryptHash = ECHash()
5552
private val eCryptKeys = ECKeys()
5653
private lateinit var privateKey: RSAPrivateKey
5754
private lateinit var publicKey: RSAPublicKey
5855

59-
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
56+
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
57+
savedInstanceState: Bundle?): View? {
6058
return inflater.inflate(R.layout.fragment_asymmetric_file, container, false)
6159
}
6260

@@ -90,15 +88,12 @@ class FragmentAsymmetricFile : Fragment(), AnkoLogger, ECResultListener {
9088

9189
val fis = context?.contentResolver?.openInputStream(data?.data)
9290

93-
progressBarF.visibility = View.VISIBLE
91+
progressBarF.show()
9492

9593
when (requestCode) {
9694

97-
_rCHash -> {
98-
eCryptHash.calculate(fis, ECHashAlgorithms.SHA_256, this)
99-
}
100-
10195
_rCEncrypt -> {
96+
tvStatus.text = getString(R.string.tv_status_encrypting)
10297
eCryptKeys.genRSAKeyPair(object : ECRSAKeyPairListener {
10398
override fun onGenerated(keyPair: KeyPair) {
10499
privateKey = keyPair.private as RSAPrivateKey
@@ -109,18 +104,21 @@ class FragmentAsymmetricFile : Fragment(), AnkoLogger, ECResultListener {
109104
override fun onFailure(message: String, e: Exception) {
110105
e.printStackTrace()
111106
onUiThread {
112-
progressBarF.visibility = View.INVISIBLE
107+
progressBarF.hide()
108+
tvStatus.text = getString(R.string.tv_status_idle)
113109
longToast("Error: $message")
114110
}
115111
}
116112
})
117113
}
118114

119115
_rCDecrypt -> {
116+
tvStatus.text = getString(R.string.tv_status_decrypting)
120117
eCryptAsymmetric.decrypt(fis, privateKey, this)
121118
}
122119

123120
_rCSign -> {
121+
tvStatus.text = getString(R.string.tv_status_signing)
124122
val sigFile = File(Environment.getExternalStorageDirectory(),
125123
"ECryptSample/sample.sig")
126124
if (sigFile.exists()) sigFile.delete()
@@ -138,28 +136,32 @@ class FragmentAsymmetricFile : Fragment(), AnkoLogger, ECResultListener {
138136
override fun onFailure(message: String, e: Exception) {
139137
e.printStackTrace()
140138
onUiThread {
139+
progressBarF.hide()
140+
tvStatus.text = getString(R.string.tv_status_idle)
141141
longToast("Failed to generate RSA key pair. Try again.")
142142
}
143143
}
144144
})
145145
}
146146

147147
_rCVerify -> {
148+
tvStatus.text = getString(R.string.tv_status_verifying)
148149
eCryptAsymmetric.verify(fis, publicKey,
149150
File(Environment.getExternalStorageDirectory(), "ECryptSample/sample.sig"),
150151
object : ECVerifiedListener {
151152
override fun onSuccess(verified: Boolean) {
152153
onUiThread {
154+
progressBarF.hide()
153155
if (verified) tvResultF.text = getString(R.string.msg_valid)
154156
else tvResultF.text = getString(R.string.msg_invalid)
155-
progressBarF.visibility = View.INVISIBLE
156157
}
157158
}
158159

159160
override fun onFailure(message: String, e: Exception) {
160161
e.printStackTrace()
161162
onUiThread {
162-
progressBarF.visibility = View.INVISIBLE
163+
progressBarF.hide()
164+
tvStatus.text = getString(R.string.tv_status_idle)
163165
toast("Error: $message")
164166
}
165167
}
@@ -169,13 +171,25 @@ class FragmentAsymmetricFile : Fragment(), AnkoLogger, ECResultListener {
169171
}
170172
}
171173

172-
override fun onProgress(newBytes: Int, bytesProcessed: Long) {
173-
progressBarF.progress = (bytesProcessed / 1024).toInt()
174+
private var maxSet = false
175+
override fun onProgress(newBytes: Int, bytesProcessed: Long, totalBytes: Long) {
176+
Log.i("TEST", "" + bytesProcessed)
177+
if (totalBytes > -1) {
178+
onUiThread {
179+
if (!maxSet) {
180+
progressBarF.isIndeterminate = false
181+
progressBarF.max = (totalBytes / 1024).toInt()
182+
maxSet = true
183+
}
184+
progressBarF.progress = (bytesProcessed / 1024).toInt()
185+
}
186+
}
174187
}
175188

176189
override fun <T> onSuccess(result: T) {
177190
onUiThread {
178-
progressBarF.visibility = View.INVISIBLE
191+
progressBarF.hide()
192+
tvStatus.text = getString(R.string.tv_status_idle)
179193
tvResultF.text = resources.getString(
180194
R.string.success_result_to_file,
181195
(result as File).absolutePath)
@@ -185,7 +199,8 @@ class FragmentAsymmetricFile : Fragment(), AnkoLogger, ECResultListener {
185199
override fun onFailure(message: String, e: Exception) {
186200
e.printStackTrace()
187201
onUiThread {
188-
progressBarF.visibility = View.INVISIBLE
202+
progressBarF.hide()
203+
tvStatus.text = getString(R.string.tv_status_idle)
189204
toast("Error: $message")
190205
}
191206
}

appKotlin/src/main/java/com/pvryan/easycryptsample/action/fragments/FragmentAsymmetricString.kt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import com.pvryan.easycrypt.asymmetric.ECAsymmetric
3030
import com.pvryan.easycrypt.asymmetric.ECRSAKeyPairListener
3131
import com.pvryan.easycrypt.asymmetric.ECVerifiedListener
3232
import com.pvryan.easycryptsample.R
33+
import com.pvryan.easycryptsample.extensions.hide
34+
import com.pvryan.easycryptsample.extensions.show
3335
import kotlinx.android.synthetic.main.fragment_asymmetric_string.*
3436
import org.jetbrains.anko.support.v4.longToast
3537
import org.jetbrains.anko.support.v4.onUiThread
@@ -62,7 +64,7 @@ class FragmentAsymmetricString : Fragment(), ECResultListener {
6264

6365
buttonEncryptS.setOnClickListener {
6466

65-
progressBarS.visibility = View.VISIBLE
67+
progressBarS.show()
6668
eCryptKeys.genRSAKeyPair(object : ECRSAKeyPairListener {
6769
override fun onGenerated(keyPair: KeyPair) {
6870
privateKey = keyPair.private as RSAPrivateKey
@@ -73,19 +75,21 @@ class FragmentAsymmetricString : Fragment(), ECResultListener {
7375
override fun onFailure(message: String, e: Exception) {
7476
e.printStackTrace()
7577
onUiThread {
76-
progressBarS.visibility = View.INVISIBLE
78+
progressBarS.hide()
7779
longToast("Error: $message")
7880
}
7981
}
8082
})
8183
}
8284

8385
buttonDecryptS.setOnClickListener {
86+
progressBarS.show()
8487
eCryptAsymmetric.decrypt(edInputS.text, privateKey, this)
8588
}
8689

8790
buttonSignS.setOnClickListener {
8891

92+
progressBarS.show()
8993
val sigFile = File(Environment.getExternalStorageDirectory(),
9094
"ECryptSample/sample.sig")
9195
if (sigFile.exists()) sigFile.delete()
@@ -103,6 +107,7 @@ class FragmentAsymmetricString : Fragment(), ECResultListener {
103107
override fun onFailure(message: String, e: Exception) {
104108
e.printStackTrace()
105109
onUiThread {
110+
progressBarS.hide()
106111
longToast("Failed to generate RSA key pair. Try again.")
107112
}
108113
}
@@ -116,6 +121,7 @@ class FragmentAsymmetricString : Fragment(), ECResultListener {
116121
object : ECVerifiedListener {
117122
override fun onSuccess(verified: Boolean) {
118123
onUiThread {
124+
progressBarS.hide()
119125
if (verified) tvResultS.text = getString(R.string.msg_valid)
120126
else tvResultS.text = getString(R.string.msg_invalid)
121127
}
@@ -124,18 +130,17 @@ class FragmentAsymmetricString : Fragment(), ECResultListener {
124130
override fun onFailure(message: String, e: Exception) {
125131
e.printStackTrace()
126132
onUiThread {
127-
progressBarS.visibility = View.INVISIBLE
133+
progressBarS.hide()
128134
longToast("Error: $message")
129135
}
130136
}
131-
132137
})
133138
}
134139
}
135140

136141
override fun <T> onSuccess(result: T) {
137142
onUiThread {
138-
progressBarS.visibility = View.INVISIBLE
143+
progressBarS.hide()
139144
tvResultS.text = when (result) {
140145
is String -> result
141146
is File -> resources.getString(
@@ -149,7 +154,7 @@ class FragmentAsymmetricString : Fragment(), ECResultListener {
149154
override fun onFailure(message: String, e: Exception) {
150155
e.printStackTrace()
151156
onUiThread {
152-
progressBarS.visibility = View.INVISIBLE
157+
progressBarS.hide()
153158
longToast("Error: $message")
154159
}
155160
}

appKotlin/src/main/java/com/pvryan/easycryptsample/action/fragments/FragmentGeneratePassword.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ import android.view.ViewGroup
2727
import com.pvryan.easycrypt.ECKeys
2828
import com.pvryan.easycrypt.symmetric.ECPasswordListener
2929
import com.pvryan.easycryptsample.R
30+
import com.pvryan.easycryptsample.extensions.snackShort
3031
import kotlinx.android.synthetic.main.fragment_generate_password.*
31-
import org.jetbrains.anko.support.v4.longToast
32-
import org.jetbrains.anko.support.v4.toast
32+
import org.jetbrains.anko.support.v4.onUiThread
3333
import java.security.InvalidParameterException
3434

3535
class FragmentGeneratePassword : Fragment() {
@@ -47,7 +47,7 @@ class FragmentGeneratePassword : Fragment() {
4747
tvResultP.setOnLongClickListener {
4848
val data = ClipData.newPlainText("result", tvResultP.text)
4949
clipboard.primaryClip = data
50-
longToast("Result copied to clipboard")
50+
view.snackShort("Result copied to clipboard")
5151
true
5252
}
5353

@@ -64,10 +64,10 @@ class FragmentGeneratePassword : Fragment() {
6464
}
6565
} catch (e: InvalidParameterException) {
6666
e.printStackTrace()
67-
toast(e.localizedMessage)
67+
view.snackShort(e.localizedMessage)
6868
} catch (e: NumberFormatException) {
6969
e.printStackTrace()
70-
toast("Too big number.")
70+
view.snackShort("Too big number.")
7171
}
7272
}
7373

@@ -81,7 +81,7 @@ class FragmentGeneratePassword : Fragment() {
8181
override fun onFailure(message: String, e: Exception) {
8282
Log.w(FragmentGeneratePassword::class.java.simpleName, message)
8383
e.printStackTrace()
84-
toast(e.localizedMessage)
84+
onUiThread { view.snackShort(e.localizedMessage) }
8585
}
8686

8787
override fun onGenerated(password: String) {
@@ -90,7 +90,7 @@ class FragmentGeneratePassword : Fragment() {
9090
})
9191
} catch (e: NumberFormatException) {
9292
e.printStackTrace()
93-
toast("Too big number.")
93+
view.snackShort("Too big number.")
9494
}
9595
}
9696
}

0 commit comments

Comments
 (0)