INTEGRATION.md

8.8 KB · raw

rendered · source

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:

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


1. CreateVault strength bar + gate (LockScreen / LockViewModel)

LockViewModel.kt

Add the import:

import com.peachpasswords.ui.security.scorePassword

There is a module-level constant MIN_PASSWORD_LENGTH = 12. Add a strength gate alongside it:

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:

        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):

        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:

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:

    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:

                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):

                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:

                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):

                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:

internal fun scorePassword(pw: String): Int { ... }
internal fun strengthLabel(score: Int): String = when (score) { ... }

Delete the local StrengthBar composable

Remove:

@Composable
private fun StrengthBar(score: Int, accent: Color) {
    // Four segments; filled count == score. ...
}

Add imports

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:

        Spacer(Modifier.size(16.dp))
        StrengthBar(score = score, accent = accent)

Change to:

        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.