Refactor key_strength check for simplification and optimization (#725)
What changed, and why it matters
This commit is a straightforward code cleanup in the password-strength meter. It replaces four separate scans over the password with a single loop and adds an early-exit optimization once all four character categories have been found. The actual strength rules (minimum length of 8 and presence of uppercase, lowercase, digit, and special characters) are unchanged. There is no security bug being fixed here.
No security action required. Treat as a normal maintainability/performance refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change refactors key_strength() in src/krux/pages/encryption_ui.py. Previously it used any() generators and a helper is_alnum() to detect uppercase, lowercase, digits, and special characters. The patch merges those checks into one for loop with mutually exclusive branches and breaks early when all four flags are set. The scoring logic (score = sum([...])) and the Weak/Medium/Strong thresholds remain identical. No cryptographic or input-validation behavior changes.
Changed components
src/krux/pages/encryption_ui.pyInspect captured patch +15 / −9
diff --git a/src/krux/pages/encryption_ui.py b/src/krux/pages/encryption_ui.py
index b6c3e9b..a377584 100644
--- a/src/krux/pages/encryption_ui.py
+++ b/src/krux/pages/encryption_ui.py
@@ -396,15 +396,21 @@ class EncryptionKey(Page):
if len(key_string) < 8:
return t("Weak")
- # Helper function to check if character is alphanumeric
- def is_alnum(c):
- return ("a" <= c <= "z") or ("A" <= c <= "Z") or ("0" <= c <= "9")
-
- # Check for presence of character types
- has_upper = any(c.isupper() for c in key_string)
- has_lower = any(c.islower() for c in key_string)
- has_digit = any(c.isdigit() for c in key_string)
- has_special = any(not is_alnum(c) for c in key_string)
+ has_upper = has_lower = has_digit = has_special = False
+
+ for c in key_string:
+ if "a" <= c <= "z":
+ has_lower = True
+ elif "A" <= c <= "Z":
+ has_upper = True
+ elif "0" <= c <= "9":
+ has_digit = True
+ else:
+ has_special = True
+
+ # small optimization: stop if all found
+ if has_upper and has_lower and has_digit and has_special:
+ break
# Count how many character types are present
score = sum([has_upper, has_lower, has_digit, has_special])
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.