Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit 6d73b63

Browse files
Fix detekt warnings in format-common (#2167)
* fix warning ktlint to format-common folder * update to code review * adjusted spotless
1 parent 224d956 commit 6d73b63

File tree

6 files changed

+35
-28
lines changed

6 files changed

+35
-28
lines changed

detekt-baselines/format-common.xml

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
11
<?xml version='1.0' encoding='UTF-8'?>
22
<SmellBaseline>
33
<ManuallySuppressedIssues/>
4-
<CurrentIssues>
5-
<ID>MagicNumber:Otp.kt$Otp$0x7f</ID>
6-
<ID>MagicNumber:Otp.kt$Otp$10</ID>
7-
<ID>MagicNumber:Otp.kt$Otp$26</ID>
8-
<ID>MagicNumber:Otp.kt$Otp$4</ID>
9-
<ID>MagicNumber:Otp.kt$Otp$5</ID>
10-
<ID>MagicNumber:Otp.kt$Otp$6</ID>
11-
<ID>MagicNumber:Otp.kt$Otp$8</ID>
12-
<ID>MagicNumber:PasswordEntry.kt$PasswordEntry$1000</ID>
13-
<ID>MatchingDeclarationName:Clocks.kt$UserClock : Clock</ID>
14-
<ID>MatchingDeclarationName:TestClocks.kt$TestUserClock : UserClock</ID>
15-
<ID>MaxLineLength:PasswordEntryTest.kt$PasswordEntryTest.Companion$"otpauth://totp/ACME%20Co:[email protected]?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&amp;issuer=ACME%20Co&amp;algorithm=SHA1&amp;digits=6&amp;period=30"</ID>
16-
<ID>ReturnCount:PasswordEntry.kt$PasswordEntry$private fun findAndStripPassword(passContent: List&lt;String>): Pair&lt;String?, List&lt;String>></ID>
17-
</CurrentIssues>
4+
<CurrentIssues/>
185
</SmellBaseline>

format-common/src/main/kotlin/app/passwordstore/data/passfile/PasswordEntry.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ constructor(
6262
do {
6363
val otp = calculateTotp()
6464
emit(otp)
65-
delay(1000L)
65+
delay(ONE_SECOND.seconds)
6666
} while (coroutineContext.isActive)
6767
} else {
6868
awaitCancellation()
@@ -90,6 +90,7 @@ constructor(
9090
return totpSecret != null
9191
}
9292

93+
@Suppress("ReturnCount")
9394
private fun findAndStripPassword(passContent: List<String>): Pair<String?, List<String>> {
9495
if (TotpFinder.TOTP_FIELDS.any { passContent[0].startsWith(it) }) return Pair(null, passContent)
9596
for (line in passContent) {
@@ -180,8 +181,14 @@ constructor(
180181
val totpAlgorithm = totpFinder.findAlgorithm(content)
181182
val issuer = totpFinder.findIssuer(content)
182183
val millis = clock.millis()
183-
val remainingTime = (totpPeriod - ((millis / 1000) % totpPeriod)).seconds
184-
Otp.calculateCode(totpSecret!!, millis / (1000 * totpPeriod), totpAlgorithm, digits, issuer)
184+
val remainingTime = (totpPeriod - ((millis / ONE_SECOND) % totpPeriod)).seconds
185+
Otp.calculateCode(
186+
totpSecret!!,
187+
millis / (ONE_SECOND * totpPeriod),
188+
totpAlgorithm,
189+
digits,
190+
issuer
191+
)
185192
.mapBoth(
186193
{ code ->
187194
return Totp(code, remainingTime)
@@ -217,5 +224,6 @@ constructor(
217224
"secret:",
218225
"pass:",
219226
)
227+
private const val ONE_SECOND = 1000
220228
}
221229
}

format-common/src/main/kotlin/app/passwordstore/util/totp/Otp.kt

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ internal object Otp {
1818

1919
private val BASE_32 = Base32()
2020
private val STEAM_ALPHABET = "23456789BCDFGHJKMNPQRTVWXY".toCharArray()
21+
private const val BYTE_BUFFER_CAPACITY = 8
22+
private const val END_INDEX_OFFSET = 4
23+
private const val STEAM_GUARD_DIGITS = 5
24+
private const val MINIMUM_DIGITS = 6
25+
private const val MAXIMUM_DIGITS = 10
26+
private const val ALPHABET_LENGTH = 26
27+
private const val MOST_SIGNIFICANT_BYTE = 0x7f
2128

2229
fun calculateCode(
2330
secret: String,
@@ -32,13 +39,13 @@ internal object Otp {
3239
val digest =
3340
Mac.getInstance(algo).run {
3441
init(secretKey)
35-
doFinal(ByteBuffer.allocate(8).putLong(counter).array())
42+
doFinal(ByteBuffer.allocate(BYTE_BUFFER_CAPACITY).putLong(counter).array())
3643
}
3744
// Least significant 4 bits are used as an offset into the digest.
3845
val offset = (digest.last() and 0xf).toInt()
3946
// Extract 32 bits at the offset and clear the most significant bit.
40-
val code = digest.copyOfRange(offset, offset + 4)
41-
code[0] = (0x7f and code[0].toInt()).toByte()
47+
val code = digest.copyOfRange(offset, offset.plus(END_INDEX_OFFSET))
48+
code[0] = (MOST_SIGNIFICANT_BYTE and code[0].toInt()).toByte()
4249
val codeInt = ByteBuffer.wrap(code).int
4350
check(codeInt > 0)
4451
// SteamGuard is a horrible OTP implementation that generates non-standard 5 digit OTPs as
@@ -47,9 +54,9 @@ internal object Otp {
4754
if (digits == "s" || issuer == "Steam") {
4855
var remainingCodeInt = codeInt
4956
buildString {
50-
repeat(5) {
57+
repeat(STEAM_GUARD_DIGITS) {
5158
append(STEAM_ALPHABET[remainingCodeInt % STEAM_ALPHABET.size])
52-
remainingCodeInt /= 26
59+
remainingCodeInt /= ALPHABET_LENGTH
5360
}
5461
}
5562
} else {
@@ -59,17 +66,21 @@ internal object Otp {
5966
numDigits == null -> {
6067
return Err(IllegalArgumentException("Digits specifier has to be either 's' or numeric"))
6168
}
62-
numDigits < 6 -> {
63-
return Err(IllegalArgumentException("TOTP codes have to be at least 6 digits long"))
69+
numDigits < MINIMUM_DIGITS -> {
70+
return Err(
71+
IllegalArgumentException("TOTP codes have to be at least $MINIMUM_DIGITS digits long")
72+
)
6473
}
65-
numDigits > 10 -> {
66-
return Err(IllegalArgumentException("TOTP codes can be at most 10 digits long"))
74+
numDigits > MAXIMUM_DIGITS -> {
75+
return Err(
76+
IllegalArgumentException("TOTP codes can be at most $MAXIMUM_DIGITS digits long")
77+
)
6778
}
6879
else -> {
6980
// 2^31 = 2_147_483_648, so we can extract at most 10 digits with the first one
7081
// always being 0, 1, or 2. Pad with leading zeroes.
71-
val codeStringBase10 = codeInt.toString(10).padStart(10, '0')
72-
check(codeStringBase10.length == 10)
82+
val codeStringBase10 = codeInt.toString(MAXIMUM_DIGITS).padStart(MAXIMUM_DIGITS, '0')
83+
check(codeStringBase10.length == MAXIMUM_DIGITS)
7384
codeStringBase10.takeLast(numDigits)
7485
}
7586
}

format-common/src/test/kotlin/app/passwordstore/data/passfile/PasswordEntryTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ class PasswordEntryTest {
194194

195195
companion object {
196196

197+
@Suppress("MaxLineLength")
197198
const val TOTP_URI =
198199
"otpauth://totp/ACME%20Co:[email protected]?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=SHA1&digits=6&period=30"
199200

0 commit comments

Comments
 (0)