aboutsummaryrefslogtreecommitdiff
path: root/app/src/main
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2020-09-05 05:39:17 +0530
committerHarsh Shandilya <me@msfjarvis.dev>2020-09-05 22:49:03 +0530
commit730da7ef0faf55f7bb90eccc41e7b57997ba839d (patch)
treebb5f3ad9c6bdd37ce8b4505061d7290b6adf1ea1 /app/src/main
parent104c6550f651434cb60393ac167c7d3b46014442 (diff)
Otp: use runCatching to replace exception handling
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'app/src/main')
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/model/PasswordEntry.kt3
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/utils/Otp.kt39
2 files changed, 13 insertions, 29 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/model/PasswordEntry.kt b/app/src/main/java/com/zeapo/pwdstore/model/PasswordEntry.kt
index 2cd31256..ea6255c4 100644
--- a/app/src/main/java/com/zeapo/pwdstore/model/PasswordEntry.kt
+++ b/app/src/main/java/com/zeapo/pwdstore/model/PasswordEntry.kt
@@ -4,6 +4,7 @@
*/
package com.zeapo.pwdstore.model
+import com.github.michaelbull.result.get
import com.zeapo.pwdstore.utils.Otp
import com.zeapo.pwdstore.utils.TotpFinder
import com.zeapo.pwdstore.utils.UriTotpFinder
@@ -55,7 +56,7 @@ class PasswordEntry(content: String, private val totpFinder: TotpFinder = UriTot
fun calculateTotpCode(): String? {
if (totpSecret == null)
return null
- return Otp.calculateCode(totpSecret, Date().time / (1000 * totpPeriod), totpAlgorithm, digits)
+ return Otp.calculateCode(totpSecret, Date().time / (1000 * totpPeriod), totpAlgorithm, digits).get()
}
val extraContentWithoutAuthData by lazy {
diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/Otp.kt b/app/src/main/java/com/zeapo/pwdstore/utils/Otp.kt
index b2e4faba..994b4d82 100644
--- a/app/src/main/java/com/zeapo/pwdstore/utils/Otp.kt
+++ b/app/src/main/java/com/zeapo/pwdstore/utils/Otp.kt
@@ -5,10 +5,9 @@
package com.zeapo.pwdstore.utils
-import com.github.ajalt.timberkt.e
+import com.github.michaelbull.result.Err
+import com.github.michaelbull.result.runCatching
import java.nio.ByteBuffer
-import java.security.InvalidKeyException
-import java.security.NoSuchAlgorithmException
import java.util.Locale
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
@@ -24,26 +23,13 @@ object Otp {
check(STEAM_ALPHABET.size == 26)
}
- fun calculateCode(secret: String, counter: Long, algorithm: String, digits: String): String? {
+ fun calculateCode(secret: String, counter: Long, algorithm: String, digits: String) = runCatching {
val algo = "Hmac${algorithm.toUpperCase(Locale.ROOT)}"
- val decodedSecret = try {
- BASE_32.decode(secret)
- } catch (e: Exception) {
- e(e) { "Failed to decode secret" }
- return null
- }
+ val decodedSecret = BASE_32.decode(secret)
val secretKey = SecretKeySpec(decodedSecret, algo)
- val digest = try {
- Mac.getInstance(algo).run {
- init(secretKey)
- doFinal(ByteBuffer.allocate(8).putLong(counter).array())
- }
- } catch (e: NoSuchAlgorithmException) {
- e(e)
- return null
- } catch (e: InvalidKeyException) {
- e(e) { "Key is malformed" }
- return null
+ val digest = Mac.getInstance(algo).run {
+ init(secretKey)
+ doFinal(ByteBuffer.allocate(8).putLong(counter).array())
}
// Least significant 4 bits are used as an offset into the digest.
val offset = (digest.last() and 0xf).toInt()
@@ -52,7 +38,7 @@ object Otp {
code[0] = (0x7f and code[0].toInt()).toByte()
val codeInt = ByteBuffer.wrap(code).int
check(codeInt > 0)
- return if (digits == "s") {
+ if (digits == "s") {
// Steam
var remainingCodeInt = codeInt
buildString {
@@ -66,16 +52,13 @@ object Otp {
val numDigits = digits.toIntOrNull()
when {
numDigits == null -> {
- e { "Digits specifier has to be either 's' or numeric" }
- return null
+ return Err(IllegalArgumentException("Digits specifier has to be either 's' or numeric"))
}
numDigits < 6 -> {
- e { "TOTP codes have to be at least 6 digits long" }
- return null
+ return Err(IllegalArgumentException("TOTP codes have to be at least 6 digits long"))
}
numDigits > 10 -> {
- e { "TOTP codes can be at most 10 digits long" }
- return null
+ return Err(IllegalArgumentException("TOTP codes can be at most 10 digits long"))
}
else -> {
// 2^31 = 2_147_483_648, so we can extract at most 10 digits with the first one