# E1 — zxcvbn Strength Meter — Integration Self-contained, dependency-free strength scorer + reusable 4-segment bar. **No gradle dependency added.** Pure-Kotlin heuristic (length + character-class entropy estimate − sequence/repeat/common-password penalties), documented inline in `PasswordStrength.kt`. ## Files to drop in | Draft | Destination | |-------|-------------| | `PasswordStrength.kt` | `app/src/main/java/com/peachpasswords/ui/security/PasswordStrength.kt` | | `StrengthBar.kt` | `app/src/main/java/com/peachpasswords/ui/security/StrengthBar.kt` | | `PasswordStrengthTest.kt` | `app/src/test/java/com/peachpasswords/ui/security/PasswordStrengthTest.kt` | New package: `com.peachpasswords.ui.security`. Public API: ```kotlin fun scorePassword(pw: String): Int // 0..4 fun strengthLabel(score: Int): String // "very weak".."strong" @Composable fun StrengthBar(score: Int, modifier: Modifier = Modifier) @Composable fun strengthColor(score: Int): Color // 0 breach,1-2 amber,3 health,4 accent ``` No ViewModel, repository, or nav changes required. ## Behaviour notes / risks - **Stricter than the old Export scorer.** The previous `scorePassword` in ExportScreen was length+class only, so `"password"` scored ~2. The shared scorer forces common passwords and single-repeat strings to **0**. This is the intended improvement, but it means the Export gate (`score >= 3`) now rejects a few inputs it used to accept. Confirmed still-passing for real backup passwords (e.g. `Password123!`, `xK9#mQ2vL8wR` → 4). - **Amber token.** `PeachColors` has no amber; `StrengthBar.kt` defines a local `StrengthAmber = Color(0xCCE0A040)` (documented). If the orchestrator prefers a theme token, add one to `PeachColors` and swap the reference. - The 62-test suite is unaffected; `PasswordStrengthTest` adds 12 new tests. The existing `ExportRoundTripTest` does not assert on scoring. --- ## 1. CreateVault strength bar + gate (LockScreen / LockViewModel) ### LockViewModel.kt Add the import: ```kotlin import com.peachpasswords.ui.security.scorePassword ``` There is a module-level constant `MIN_PASSWORD_LENGTH = 12`. Add a strength gate alongside it: ```kotlin const val MIN_PASSWORD_LENGTH = 12 /** CreateVault is blocked below "fair" (zxcvbn-style score 2). */ const val MIN_CREATE_STRENGTH = 2 ``` **Anchor** — the existing `create()` validation block: ```kotlin when { password.length < MIN_PASSWORD_LENGTH -> return _state.update { it.copy(error = "password must be at least $MIN_PASSWORD_LENGTH characters") } password != confirm -> return _state.update { it.copy(error = "passwords do not match") } } ``` Replace with (adds the strength branch — order: length first, then strength, then match): ```kotlin when { password.length < MIN_PASSWORD_LENGTH -> return _state.update { it.copy(error = "password must be at least $MIN_PASSWORD_LENGTH characters") } scorePassword(password) < MIN_CREATE_STRENGTH -> return _state.update { it.copy(error = "Password too weak — try a longer or more varied password") } password != confirm -> return _state.update { it.copy(error = "passwords do not match") } } ``` > The view also disables the button below, so this branch is a backstop for the > IME "Go" path. ### LockScreen.kt Add imports: ```kotlin import com.peachpasswords.ui.security.MIN_CREATE_STRENGTH // optional, if importing the const import com.peachpasswords.ui.security.StrengthBar import com.peachpasswords.ui.security.strengthColor import com.peachpasswords.ui.security.strengthLabel import com.peachpasswords.ui.security.scorePassword ``` (If `MIN_CREATE_STRENGTH` lives in `LockViewModel`'s package — same package as LockScreen — no import is needed for it.) Compute the score once in the composable body, near the `password`/`confirm` state: ```kotlin var password by remember { mutableStateOf("") } var confirm by remember { mutableStateOf("") } val createScore = remember(password) { scorePassword(password) } // ADD ``` **Anchor** — the create-mode confirm field block, ending at its closing brace: ```kotlin if (!state.hasVault) { Spacer(Modifier.height(16.dp)) FieldLabel("confirm password") Spacer(Modifier.height(4.dp)) UnderlineField( value = confirm, onChange = { confirm = it; vm.clearError() }, placeholder = "••••••••••", textStyle = mono18Spaced(), imeAction = ImeAction.Go, onImeAction = { vm.create(password, confirm) }, ) } ``` Insert the strength bar + label **inside** the `if (!state.hasVault)` block, after the confirm `UnderlineField` (so the meter sits under both inputs): ```kotlin if (!state.hasVault) { Spacer(Modifier.height(16.dp)) FieldLabel("confirm password") Spacer(Modifier.height(4.dp)) UnderlineField( value = confirm, onChange = { confirm = it; vm.clearError() }, placeholder = "••••••••••", textStyle = mono18Spaced(), imeAction = ImeAction.Go, onImeAction = { vm.create(password, confirm) }, ) if (password.isNotEmpty()) { // ADD ▼ Spacer(Modifier.height(14.dp)) StrengthBar(score = createScore) Spacer(Modifier.height(6.dp)) Text( text = "strength: ${strengthLabel(createScore)}", color = strengthColor(createScore), style = PeachType.labelMono, modifier = Modifier.fillMaxWidth(), textAlign = TextAlign.Center, ) } // ADD ▲ } ``` **Gate the button.** Anchor — the create-mode AccentButton: ```kotlin AccentButton( label = if (state.hasVault) "unlock" else "create vault", enabled = !state.loading, onClick = { if (state.hasVault) vm.unlock(password) else vm.create(password, confirm) }, ) ``` Change `enabled` so create mode also requires `score >= 2` (unlock mode keeps the old behaviour): ```kotlin AccentButton( label = if (state.hasVault) "unlock" else "create vault", enabled = !state.loading && (state.hasVault || createScore >= MIN_CREATE_STRENGTH), onClick = { if (state.hasVault) vm.unlock(password) else vm.create(password, confirm) }, ) ``` > `MIN_CREATE_STRENGTH` is in the `com.peachpasswords.ui.security` package above; > if you'd rather not import it, inline the literal `2` or move the const into > `LockViewModel.kt` (same package as LockScreen, no import needed). --- ## 2. Swap ExportScreen to the shared scorer `ExportScreen.kt` currently has its own `scorePassword` / `strengthLabel` (lines ~64-98) and a private `StrengthBar` composable (~348-364). Replace all three with the shared ones. ### Delete the local scorer block Remove the entire commented "Password strength scorer" section — both functions: ```kotlin internal fun scorePassword(pw: String): Int { ... } internal fun strengthLabel(score: Int): String = when (score) { ... } ``` ### Delete the local StrengthBar composable Remove: ```kotlin @Composable private fun StrengthBar(score: Int, accent: Color) { // Four segments; filled count == score. ... } ``` ### Add imports ```kotlin import com.peachpasswords.ui.security.StrengthBar import com.peachpasswords.ui.security.scorePassword import com.peachpasswords.ui.security.strengthLabel ``` ### Fix the one call site that passes `accent` The shared `StrengthBar` takes only `(score, modifier)` — it reads accent from `LocalPeachAccent` itself. Anchor: ```kotlin Spacer(Modifier.size(16.dp)) StrengthBar(score = score, accent = accent) ``` Change to: ```kotlin Spacer(Modifier.size(16.dp)) StrengthBar(score = score) ``` `PeachScriptExportView` already has `val accent = LocalPeachAccent.current` and still uses it for the "strength: …" label and buttons, so leave that line. The gate (`val strongEnough = score >= 3`) is unchanged and works with the new scorer.