16237ac7d53f
feat: E1 zxcvbn-style strength scoring — shared scorer+StrengthBar (ui/security), CreateVault bar+gate (score<2 blocks), ExportScreen swapped to shared scorer; +12 tests (v2.1.36)
- time
- 2026-06-09 04:40 UTC · 5 months ago
- machine
- koh2
- agent
- claude-opus-4-8
- session
- peach remaining gaps
- parent
- 7097e444900e
↓ peach-android-16237ac7.tar.gzatlas at this save
files changed9 files
app/drafts/E1/PasswordStrength.kt+0 −111app/drafts/E1/PasswordStrengthTest.kt+0 −85app/drafts/E1/StrengthBar.kt+0 −69app/src/main/java/com/peachpasswords/ui/lock/LockScreen.kt+19 −1app/src/main/java/com/peachpasswords/ui/lock/LockViewModel.kt+6 −0app/src/main/java/com/peachpasswords/ui/security/PasswordStrength.kt+111 −0app/src/main/java/com/peachpasswords/ui/security/StrengthBar.kt+69 −0app/src/main/java/com/peachpasswords/ui/vault/ExportScreen.kt+5 −42app/src/test/java/com/peachpasswords/ui/security/PasswordStrengthTest.kt+85 −0app/drafts/E1/PasswordStrength.kt+0 −111
@@ -1,111 +0,0 @@1-package com.peachpasswords.ui.security2-3-/**4- * Dependency-free password strength scoring (0..4), modelled on zxcvbn's5- * *intent* (entropy from length + character variety, minus penalties for6- * predictable structure) without bundling the dictionary-heavy library.7- *8- * This is a UI affordance and a creation gate — NOT a cryptographic strength9- * guarantee. The real defence is Argon2id KDF at the vault layer. The score10- * exists to steer users away from trivially guessable master passwords.11- *12- * Scoring model13- * -------------14- * We estimate a rough bit-entropy as `log2(poolSize) * length`, where `poolSize`15- * is the count of character classes present (lower/upper/digit/symbol) mapped to16- * a conventional alphabet size, then bucket entropy into 0..4. Penalties knock17- * the score down for the patterns zxcvbn weights most heavily:18- * - exact match against a small common-password list → forced to 019- * - the password is a single repeated character → forced to 020- * - a long straight run (sequence or repeat) dominates → −1 bucket21- * The buckets are calibrated so the prior heuristic's behaviour is preserved:22- * mixed-class passwords of length >= 12 still reach "good" (3), and adding a23- * 4th class / more length reaches "strong" (4). This keeps the existing24- * ExportScreen gate (score >= 3) behaving as before.25- */26-27-private val COMMON_PASSWORDS: Set<String> = setOf(28- "password", "passw0rd", "password1", "password123", "123456", "1234567",29- "12345678", "123456789", "1234567890", "qwerty", "qwertyuiop", "abc123",30- "111111", "000000", "iloveyou", "admin", "letmein", "welcome", "monkey",31- "dragon", "sunshine", "princess", "football", "baseball", "master",32- "superman", "trustno1", "whatever", "starwars", "hello", "freedom",33- "qazwsx", "michael", "shadow", "ashley", "qwerty123", "1q2w3e4r",34- "zaq12wsx", "changeme", "peach", "peachpasswords",35-)36-37-// Conventional alphabet sizes per character class, used for the entropy estimate.38-private const val POOL_LOWER = 2639-private const val POOL_UPPER = 2640-private const val POOL_DIGIT = 1041-private const val POOL_SYMBOL = 33 // printable ASCII punctuation, approx.42-43-/**44- * Score [pw] from 0 (very weak) to 4 (strong). Empty input scores 0.45- *46- * The returned value pairs with [strengthLabel] for display. Callers gate on47- * `>= 2` (creation, "fair") or `>= 3` (export backups, "good").48- */49-fun scorePassword(pw: String): Int {50- if (pw.isEmpty()) return 051-52- val normalized = pw.lowercase()53- if (normalized in COMMON_PASSWORDS) return 054- // A single repeated character ("aaaaaaaa") carries almost no entropy.55- if (pw.toSet().size == 1) return 056-57- var pool = 058- if (pw.any { it in 'a'..'z' }) pool += POOL_LOWER59- if (pw.any { it in 'A'..'Z' }) pool += POOL_UPPER60- if (pw.any { it.isDigit() }) pool += POOL_DIGIT61- if (pw.any { !it.isLetterOrDigit() }) pool += POOL_SYMBOL62- if (pool == 0) pool = POOL_LOWER // pathological (e.g. only whitespace)63-64- val bitsPerChar = log2(pool.toDouble())65- var entropy = bitsPerChar * pw.length66-67- // Predictable runs (straight sequences or repeats) cost a bucket. A run is a68- // maximal stretch where each char is equal to, or one step from, the prior.69- if (longestRun(pw) >= 4) entropy -= bitsPerChar * 470-71- return bucket(entropy)72-}73-74-/** Display label for a 0..4 [score]; values outside the range clamp to the ends. */75-fun strengthLabel(score: Int): String = when (score.coerceIn(0, 4)) {76- 0 -> "very weak"77- 1 -> "weak"78- 2 -> "fair"79- 3 -> "good"80- else -> "strong"81-}82-83-// Entropy thresholds (bits) for each bucket boundary. Tuned so:84-// "Tr0ub4dor&3"-class (mixed, ~11+ chars) → 3, longer/4-class → 4,85-// short single-class → 0/1.86-private fun bucket(entropy: Double): Int = when {87- entropy < 28.0 -> 088- entropy < 40.0 -> 189- entropy < 56.0 -> 290- entropy < 72.0 -> 391- else -> 492-}93-94-/** Length of the longest ascending/descending/repeating run of adjacent chars. */95-private fun longestRun(pw: String): Int {96- if (pw.length < 2) return pw.length97- var longest = 198- var current = 199- for (i in 1 until pw.length) {100- val delta = pw[i].code - pw[i - 1].code101- if (delta == 0 || delta == 1 || delta == -1) {102- current++103- if (current > longest) longest = current104- } else {105- current = 1106- }107- }108- return longest109-}110-111-private fun log2(x: Double): Double = Math.log(x) / Math.log(2.0)app/drafts/E1/PasswordStrengthTest.kt+0 −85
@@ -1,85 +0,0 @@1-package com.peachpasswords.ui.security2-3-import org.junit.Assert.assertEquals4-import org.junit.Assert.assertTrue5-import org.junit.Test6-7-/**8- * Behavioural contract for the shared strength scorer. These assertions pin the9- * two gates the app relies on: CreateVault blocks `< 2`, ExportScreen blocks10- * `< 3`. They are intentionally inequality-based where the exact bucket is not11- * load-bearing, and exact where a boundary matters.12- */13-class PasswordStrengthTest {14-15- @Test fun empty_scores_zero() {16- assertEquals(0, scorePassword(""))17- }18-19- @Test fun single_repeated_char_scores_zero() {20- assertEquals(0, scorePassword("aaaaaaaaaaaa"))21- }22-23- @Test fun common_passwords_score_zero_case_insensitive() {24- assertEquals(0, scorePassword("password"))25- assertEquals(0, scorePassword("Password"))26- assertEquals(0, scorePassword("PASSWORD"))27- assertEquals(0, scorePassword("123456"))28- assertEquals(0, scorePassword("qwerty"))29- assertEquals(0, scorePassword("peach"))30- }31-32- @Test fun short_single_class_is_weak() {33- // 8 lowercase letters: real but low-entropy → "weak" (1), below both gates.34- assertTrue(scorePassword("abcdefgh") < 2)35- }36-37- @Test fun straight_sequence_is_penalized() {38- // Pure ascending run collapses toward zero entropy.39- assertTrue(scorePassword("abcdefghij") < 2)40- assertTrue(scorePassword("0123456789") < 2)41- }42-43- @Test fun mixed_8char_three_class_passes_create_gate() {44- // "short1A!" — lower+upper+digit+symbol, length 8 → "fair" (2).45- assertTrue(scorePassword("short1A!") >= 2)46- }47-48- @Test fun mixed_12char_four_class_is_strong_and_passes_export_gate() {49- assertTrue(scorePassword("xK9#mQ2vL8wR") >= 3)50- assertTrue(scorePassword("Password123!") >= 3)51- }52-53- @Test fun long_passphrase_is_strong() {54- assertEquals(4, scorePassword("correcthorsebatterystaple"))55- }56-57- @Test fun score_is_always_in_range() {58- val samples = listOf(59- "", "a", "ab", "abc123", "Tr0ub4dor&3", " ",60- "P@ssw0rd!2024xyz", "hunter2", "my secret diary 2024",61- )62- for (pw in samples) {63- val s = scorePassword(pw)64- assertTrue("score for '$pw' = $s out of range", s in 0..4)65- }66- }67-68- @Test fun labels_cover_full_range_and_clamp() {69- assertEquals("very weak", strengthLabel(0))70- assertEquals("weak", strengthLabel(1))71- assertEquals("fair", strengthLabel(2))72- assertEquals("good", strengthLabel(3))73- assertEquals("strong", strengthLabel(4))74- // Out-of-range inputs clamp rather than crash.75- assertEquals("very weak", strengthLabel(-1))76- assertEquals("strong", strengthLabel(99))77- }78-79- @Test fun more_length_never_lowers_a_clean_password() {80- // Monotonic-ish sanity: extending a varied password should not drop it81- // below the create gate.82- val base = "Xk9#mQ2v"83- assertTrue(scorePassword(base + "L8wR") >= scorePassword(base))84- }85-}app/drafts/E1/StrengthBar.kt+0 −69
@@ -1,69 +0,0 @@1-package com.peachpasswords.ui.security2-3-import androidx.compose.foundation.background4-import androidx.compose.foundation.layout.Box5-import androidx.compose.foundation.layout.Row6-import androidx.compose.foundation.layout.fillMaxWidth7-import androidx.compose.foundation.layout.height8-import androidx.compose.foundation.layout.padding9-import androidx.compose.foundation.shape.RoundedCornerShape10-import androidx.compose.runtime.Composable11-import androidx.compose.ui.Modifier12-import androidx.compose.ui.draw.clip13-import androidx.compose.ui.graphics.Color14-import androidx.compose.ui.unit.dp15-import com.peachpasswords.ui.theme.LocalPeachAccent16-import com.peachpasswords.ui.theme.PeachColors17-18-/**19- * Amber for the mid-range ("weak"/"fair") fill. PeachColors has no amber token,20- * and the spec calls for one distinct from breach (low) and health (high), so it21- * is defined locally rather than hardcoded inline at each call site.22- */23-private val StrengthAmber = Color(0xCCE0A040)24-25-/**26- * Fill color for a given [score], per spec §E1:27- * 0 → breach, 1–2 → amber, 3 → health, 4 → accent.28- */29-@Composable30-fun strengthColor(score: Int): Color {31- val accent = LocalPeachAccent.current32- return when (score.coerceIn(0, 4)) {33- 0 -> PeachColors.Breach34- 1, 2 -> StrengthAmber35- 3 -> PeachColors.HealthOk36- else -> accent37- }38-}39-40-/**41- * Four-segment strength bar, 4dp tall, full width. The number of *lit* segments42- * follows the spec ramp (1/2/3/4/4 for scores 0..4); unlit segments use T3.43- *44- * Filled-segment count differs from the raw score so score 0 still shows one lit45- * (breach-colored) segment as feedback rather than an empty bar.46- */47-@Composable48-fun StrengthBar(score: Int, modifier: Modifier = Modifier) {49- val s = score.coerceIn(0, 4)50- val lit = when (s) {51- 0 -> 152- 1 -> 253- 2 -> 354- else -> 455- }56- val fill = strengthColor(s)57- Row(modifier = modifier.fillMaxWidth()) {58- repeat(4) { i ->59- Box(60- modifier = Modifier61- .weight(1f)62- .height(4.dp)63- .padding(end = if (i < 3) 4.dp else 0.dp)64- .clip(RoundedCornerShape(2.dp))65- .background(if (i < lit) fill else PeachColors.T3),66- )67- }68- }69-}app/src/main/java/com/peachpasswords/ui/lock/LockScreen.kt+19 −1
@@ -56,6 +56,10 @@5656 import com.peachpasswords.ui.components.PeachMark5757 import com.peachpasswords.ui.theme.LocalPeachAccent5858 import com.peachpasswords.ui.theme.PeachColors59+import com.peachpasswords.ui.security.StrengthBar60+import com.peachpasswords.ui.security.scorePassword61+import com.peachpasswords.ui.security.strengthColor62+import com.peachpasswords.ui.security.strengthLabel5963 import com.peachpasswords.ui.theme.PeachType6064 import com.peachpasswords.ui.theme.ShellBackground6165 @@ -73,6 +77,7 @@7377 7478 var password by remember { mutableStateOf("") }7579 var confirm by remember { mutableStateOf("") }80+ val createScore = remember(password) { scorePassword(password) }7681 7782 LaunchedEffect(state.unlocked) {7883 if (state.unlocked) {@@ -166,6 +171,19 @@166171 imeAction = ImeAction.Go,167172 onImeAction = { vm.create(password, confirm) },168173 )174+175+ if (password.isNotEmpty()) {176+ Spacer(Modifier.height(14.dp))177+ StrengthBar(score = createScore)178+ Spacer(Modifier.height(6.dp))179+ Text(180+ text = "strength: ${strengthLabel(createScore)}",181+ color = strengthColor(createScore),182+ style = PeachType.labelMono,183+ modifier = Modifier.fillMaxWidth(),184+ textAlign = TextAlign.Center,185+ )186+ }169187 }170188 171189 Text(@@ -179,7 +197,7 @@179197 Spacer(Modifier.height(4.dp))180198 AccentButton(181199 label = if (state.hasVault) "unlock" else "create vault",182- enabled = !state.loading,200+ enabled = !state.loading && (state.hasVault || createScore >= MIN_CREATE_STRENGTH),183201 onClick = { if (state.hasVault) vm.unlock(password) else vm.create(password, confirm) },184202 )185203 app/src/main/java/com/peachpasswords/ui/lock/LockViewModel.kt+6 −0
@@ -6,6 +6,7 @@66 import androidx.lifecycle.ViewModel77 import androidx.lifecycle.viewModelScope88 import com.peachpasswords.vault.CreateVaultResult9+import com.peachpasswords.ui.security.scorePassword910 import com.peachpasswords.vault.UnlockResult1011 import com.peachpasswords.vault.VaultCrypto1112 import com.peachpasswords.vault.VaultRepository@@ -31,6 +32,9 @@3132 3233 const val MIN_PASSWORD_LENGTH = 123334 35+/** CreateVault is blocked below "fair" (zxcvbn-style score 2). */36+const val MIN_CREATE_STRENGTH = 237+3438 @HiltViewModel3539 class LockViewModel @Inject constructor(3640 private val vault: VaultRepository,@@ -60,6 +64,8 @@6064 when {6165 password.length < MIN_PASSWORD_LENGTH ->6266 return _state.update { it.copy(error = "password must be at least $MIN_PASSWORD_LENGTH characters") }67+ scorePassword(password) < MIN_CREATE_STRENGTH ->68+ return _state.update { it.copy(error = "Password too weak — try a longer or more varied password") }6369 password != confirm ->6470 return _state.update { it.copy(error = "passwords do not match") }6571 }app/src/main/java/com/peachpasswords/ui/security/PasswordStrength.kt+111 −0
@@ -0,0 +1,111 @@1+package com.peachpasswords.ui.security2+3+/**4+ * Dependency-free password strength scoring (0..4), modelled on zxcvbn's5+ * *intent* (entropy from length + character variety, minus penalties for6+ * predictable structure) without bundling the dictionary-heavy library.7+ *8+ * This is a UI affordance and a creation gate — NOT a cryptographic strength9+ * guarantee. The real defence is Argon2id KDF at the vault layer. The score10+ * exists to steer users away from trivially guessable master passwords.11+ *12+ * Scoring model13+ * -------------14+ * We estimate a rough bit-entropy as `log2(poolSize) * length`, where `poolSize`15+ * is the count of character classes present (lower/upper/digit/symbol) mapped to16+ * a conventional alphabet size, then bucket entropy into 0..4. Penalties knock17+ * the score down for the patterns zxcvbn weights most heavily:18+ * - exact match against a small common-password list → forced to 019+ * - the password is a single repeated character → forced to 020+ * - a long straight run (sequence or repeat) dominates → −1 bucket21+ * The buckets are calibrated so the prior heuristic's behaviour is preserved:22+ * mixed-class passwords of length >= 12 still reach "good" (3), and adding a23+ * 4th class / more length reaches "strong" (4). This keeps the existing24+ * ExportScreen gate (score >= 3) behaving as before.25+ */26+27+private val COMMON_PASSWORDS: Set<String> = setOf(28+ "password", "passw0rd", "password1", "password123", "123456", "1234567",29+ "12345678", "123456789", "1234567890", "qwerty", "qwertyuiop", "abc123",30+ "111111", "000000", "iloveyou", "admin", "letmein", "welcome", "monkey",31+ "dragon", "sunshine", "princess", "football", "baseball", "master",32+ "superman", "trustno1", "whatever", "starwars", "hello", "freedom",33+ "qazwsx", "michael", "shadow", "ashley", "qwerty123", "1q2w3e4r",34+ "zaq12wsx", "changeme", "peach", "peachpasswords",35+)36+37+// Conventional alphabet sizes per character class, used for the entropy estimate.38+private const val POOL_LOWER = 2639+private const val POOL_UPPER = 2640+private const val POOL_DIGIT = 1041+private const val POOL_SYMBOL = 33 // printable ASCII punctuation, approx.42+43+/**44+ * Score [pw] from 0 (very weak) to 4 (strong). Empty input scores 0.45+ *46+ * The returned value pairs with [strengthLabel] for display. Callers gate on47+ * `>= 2` (creation, "fair") or `>= 3` (export backups, "good").48+ */49+fun scorePassword(pw: String): Int {50+ if (pw.isEmpty()) return 051+52+ val normalized = pw.lowercase()53+ if (normalized in COMMON_PASSWORDS) return 054+ // A single repeated character ("aaaaaaaa") carries almost no entropy.55+ if (pw.toSet().size == 1) return 056+57+ var pool = 058+ if (pw.any { it in 'a'..'z' }) pool += POOL_LOWER59+ if (pw.any { it in 'A'..'Z' }) pool += POOL_UPPER60+ if (pw.any { it.isDigit() }) pool += POOL_DIGIT61+ if (pw.any { !it.isLetterOrDigit() }) pool += POOL_SYMBOL62+ if (pool == 0) pool = POOL_LOWER // pathological (e.g. only whitespace)63+64+ val bitsPerChar = log2(pool.toDouble())65+ var entropy = bitsPerChar * pw.length66+67+ // Predictable runs (straight sequences or repeats) cost a bucket. A run is a68+ // maximal stretch where each char is equal to, or one step from, the prior.69+ if (longestRun(pw) >= 4) entropy -= bitsPerChar * 470+71+ return bucket(entropy)72+}73+74+/** Display label for a 0..4 [score]; values outside the range clamp to the ends. */75+fun strengthLabel(score: Int): String = when (score.coerceIn(0, 4)) {76+ 0 -> "very weak"77+ 1 -> "weak"78+ 2 -> "fair"79+ 3 -> "good"80+ else -> "strong"81+}82+83+// Entropy thresholds (bits) for each bucket boundary. Tuned so:84+// "Tr0ub4dor&3"-class (mixed, ~11+ chars) → 3, longer/4-class → 4,85+// short single-class → 0/1.86+private fun bucket(entropy: Double): Int = when {87+ entropy < 28.0 -> 088+ entropy < 40.0 -> 189+ entropy < 56.0 -> 290+ entropy < 72.0 -> 391+ else -> 492+}93+94+/** Length of the longest ascending/descending/repeating run of adjacent chars. */95+private fun longestRun(pw: String): Int {96+ if (pw.length < 2) return pw.length97+ var longest = 198+ var current = 199+ for (i in 1 until pw.length) {100+ val delta = pw[i].code - pw[i - 1].code101+ if (delta == 0 || delta == 1 || delta == -1) {102+ current++103+ if (current > longest) longest = current104+ } else {105+ current = 1106+ }107+ }108+ return longest109+}110+111+private fun log2(x: Double): Double = Math.log(x) / Math.log(2.0)app/src/main/java/com/peachpasswords/ui/security/StrengthBar.kt+69 −0
@@ -0,0 +1,69 @@1+package com.peachpasswords.ui.security2+3+import androidx.compose.foundation.background4+import androidx.compose.foundation.layout.Box5+import androidx.compose.foundation.layout.Row6+import androidx.compose.foundation.layout.fillMaxWidth7+import androidx.compose.foundation.layout.height8+import androidx.compose.foundation.layout.padding9+import androidx.compose.foundation.shape.RoundedCornerShape10+import androidx.compose.runtime.Composable11+import androidx.compose.ui.Modifier12+import androidx.compose.ui.draw.clip13+import androidx.compose.ui.graphics.Color14+import androidx.compose.ui.unit.dp15+import com.peachpasswords.ui.theme.LocalPeachAccent16+import com.peachpasswords.ui.theme.PeachColors17+18+/**19+ * Amber for the mid-range ("weak"/"fair") fill. PeachColors has no amber token,20+ * and the spec calls for one distinct from breach (low) and health (high), so it21+ * is defined locally rather than hardcoded inline at each call site.22+ */23+private val StrengthAmber = Color(0xCCE0A040)24+25+/**26+ * Fill color for a given [score], per spec §E1:27+ * 0 → breach, 1–2 → amber, 3 → health, 4 → accent.28+ */29+@Composable30+fun strengthColor(score: Int): Color {31+ val accent = LocalPeachAccent.current32+ return when (score.coerceIn(0, 4)) {33+ 0 -> PeachColors.Breach34+ 1, 2 -> StrengthAmber35+ 3 -> PeachColors.HealthOk36+ else -> accent37+ }38+}39+40+/**41+ * Four-segment strength bar, 4dp tall, full width. The number of *lit* segments42+ * follows the spec ramp (1/2/3/4/4 for scores 0..4); unlit segments use T3.43+ *44+ * Filled-segment count differs from the raw score so score 0 still shows one lit45+ * (breach-colored) segment as feedback rather than an empty bar.46+ */47+@Composable48+fun StrengthBar(score: Int, modifier: Modifier = Modifier) {49+ val s = score.coerceIn(0, 4)50+ val lit = when (s) {51+ 0 -> 152+ 1 -> 253+ 2 -> 354+ else -> 455+ }56+ val fill = strengthColor(s)57+ Row(modifier = modifier.fillMaxWidth()) {58+ repeat(4) { i ->59+ Box(60+ modifier = Modifier61+ .weight(1f)62+ .height(4.dp)63+ .padding(end = if (i < 3) 4.dp else 0.dp)64+ .clip(RoundedCornerShape(2.dp))65+ .background(if (i < lit) fill else PeachColors.T3),66+ )67+ }68+ }69+}app/src/main/java/com/peachpasswords/ui/vault/ExportScreen.kt+5 −42
@@ -50,6 +50,9 @@5050 import com.peachpasswords.ui.theme.LocalPeachAccent5151 import com.peachpasswords.ui.theme.PeachColors5252 import com.peachpasswords.ui.theme.PeachType53+import com.peachpasswords.ui.security.StrengthBar54+import com.peachpasswords.ui.security.scorePassword55+import com.peachpasswords.ui.security.strengthLabel5356 import com.peachpasswords.ui.theme.ShellBackground5457 import com.peachpasswords.vault.VaultEntry5558 import com.peachpasswords.vault.VaultRepository@@ -73,30 +76,8 @@7376 * Empty input scores 0. Result is clamped to 0..4. The C2 gate requires >= 37477 * ("good"); see [strengthLabel].7578 */76-internal fun scorePassword(pw: String): Int {77- if (pw.isEmpty()) return 078- var classes = 079- if (pw.any { it in 'a'..'z' }) classes++80- if (pw.any { it in 'A'..'Z' }) classes++81- if (pw.any { it.isDigit() }) classes++82- if (pw.any { !it.isLetterOrDigit() }) classes++79+// Password strength scoring now lives in com.peachpasswords.ui.security (E1, shared).8380 84- var score = 085- if (pw.length >= 8) score++86- if (pw.length >= 12) score++87- if (classes >= 3) score++88- if (classes >= 4) score++89- return score.coerceIn(0, 4)90-}91-92-internal fun strengthLabel(score: Int): String = when (score) {93- 0 -> "very weak"94- 1 -> "weak"95- 2 -> "fair"96- 3 -> "good"97- else -> "strong"98-}99-10081 // ─────────────────────────────────────────────────────────────────────────────10182 // ViewModel10283 // ─────────────────────────────────────────────────────────────────────────────@@ -292,7 +273,7 @@292273 PeachUnderlineField(value = confirm, onValueChange = { confirm = it; output = null; status = null }, isPassword = true)293274 294275 Spacer(Modifier.size(16.dp))295- StrengthBar(score = score, accent = accent)276+ StrengthBar(score = score)296277 Spacer(Modifier.size(6.dp))297278 Row(modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {298279 Text(@@ -340,24 +321,6 @@340321 it,341322 color = if (it == ExportStrings.SAVE_FAILED) PeachColors.Breach else accent,342323 style = PeachType.labelMono,343- )344- }345- }346-}347-348-@Composable349-private fun StrengthBar(score: Int, accent: Color) {350- // Four segments; filled count == score. Color shifts to accent once "good".351- val fillColor = if (score >= 3) accent else PeachColors.T2352- Row(modifier = Modifier.fillMaxWidth()) {353- repeat(4) { i ->354- Box(355- modifier = Modifier356- .weight(1f)357- .height(4.dp)358- .padding(end = if (i < 3) 4.dp else 0.dp)359- .clip(RoundedCornerShape(2.dp))360- .background(if (i < score) fillColor else PeachColors.T3),361324 )362325 }363326 }app/src/test/java/com/peachpasswords/ui/security/PasswordStrengthTest.kt+85 −0
@@ -0,0 +1,85 @@1+package com.peachpasswords.ui.security2+3+import org.junit.Assert.assertEquals4+import org.junit.Assert.assertTrue5+import org.junit.Test6+7+/**8+ * Behavioural contract for the shared strength scorer. These assertions pin the9+ * two gates the app relies on: CreateVault blocks `< 2`, ExportScreen blocks10+ * `< 3`. They are intentionally inequality-based where the exact bucket is not11+ * load-bearing, and exact where a boundary matters.12+ */13+class PasswordStrengthTest {14+15+ @Test fun empty_scores_zero() {16+ assertEquals(0, scorePassword(""))17+ }18+19+ @Test fun single_repeated_char_scores_zero() {20+ assertEquals(0, scorePassword("aaaaaaaaaaaa"))21+ }22+23+ @Test fun common_passwords_score_zero_case_insensitive() {24+ assertEquals(0, scorePassword("password"))25+ assertEquals(0, scorePassword("Password"))26+ assertEquals(0, scorePassword("PASSWORD"))27+ assertEquals(0, scorePassword("123456"))28+ assertEquals(0, scorePassword("qwerty"))29+ assertEquals(0, scorePassword("peach"))30+ }31+32+ @Test fun short_single_class_is_weak() {33+ // 8 lowercase letters: real but low-entropy → "weak" (1), below both gates.34+ assertTrue(scorePassword("abcdefgh") < 2)35+ }36+37+ @Test fun straight_sequence_is_penalized() {38+ // Pure ascending run collapses toward zero entropy.39+ assertTrue(scorePassword("abcdefghij") < 2)40+ assertTrue(scorePassword("0123456789") < 2)41+ }42+43+ @Test fun mixed_8char_three_class_passes_create_gate() {44+ // "short1A!" — lower+upper+digit+symbol, length 8 → "fair" (2).45+ assertTrue(scorePassword("short1A!") >= 2)46+ }47+48+ @Test fun mixed_12char_four_class_is_strong_and_passes_export_gate() {49+ assertTrue(scorePassword("xK9#mQ2vL8wR") >= 3)50+ assertTrue(scorePassword("Password123!") >= 3)51+ }52+53+ @Test fun long_passphrase_is_strong() {54+ assertEquals(4, scorePassword("correcthorsebatterystaple"))55+ }56+57+ @Test fun score_is_always_in_range() {58+ val samples = listOf(59+ "", "a", "ab", "abc123", "Tr0ub4dor&3", " ",60+ "P@ssw0rd!2024xyz", "hunter2", "my secret diary 2024",61+ )62+ for (pw in samples) {63+ val s = scorePassword(pw)64+ assertTrue("score for '$pw' = $s out of range", s in 0..4)65+ }66+ }67+68+ @Test fun labels_cover_full_range_and_clamp() {69+ assertEquals("very weak", strengthLabel(0))70+ assertEquals("weak", strengthLabel(1))71+ assertEquals("fair", strengthLabel(2))72+ assertEquals("good", strengthLabel(3))73+ assertEquals("strong", strengthLabel(4))74+ // Out-of-range inputs clamp rather than crash.75+ assertEquals("very weak", strengthLabel(-1))76+ assertEquals("strong", strengthLabel(99))77+ }78+79+ @Test fun more_length_never_lowers_a_clean_password() {80+ // Monotonic-ish sanity: extending a varied password should not drop it81+ // below the create gate.82+ val base = "Xk9#mQ2v"83+ assertTrue(scorePassword(base + "L8wR") >= scorePassword(base))84+ }85+}