{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "password-strength",
  "title": "Password strength",
  "description": "Entropy estimate with a tiny LZ77-style compressor, and strength levels.",
  "files": [
    {
      "path": "lib/password-strength.ts",
      "content": "function poolSize(password: string): number {\n  let pool = 0;\n  if (/[a-z]/.test(password)) pool += 26;\n  if (/[A-Z]/.test(password)) pool += 26;\n  if (/\\d/.test(password)) pool += 10;\n  if (/[^A-Za-z0-9\\s]/.test(password)) pool += 32;\n  if (/\\s/.test(password)) pool += 1;\n  // Any non-ASCII codepoint contributes a rough Unicode bonus.\n  if (/[^\\x20-\\x7e]/.test(password)) pool += 100;\n  return pool;\n}\n\n// Estimate entropy with a tiny LZ77-style compressor. A naive\n// `length * log2(poolSize)` formula rates `aaaaaaaa` and `k7!mQp#2` the same;\n// compression correctly charges near-zero bits for runs and back-references,\n// and a consecutive-character penalty catches sequences like `abcdef` or\n// `111111` that LZ alone wouldn't discount.\nexport function estimateEntropy(password: string): number {\n  if (!password) return 0;\n\n  const perChar = Math.log2(poolSize(password));\n  let bits = 0;\n  let i = 0;\n\n  while (i < password.length) {\n    // 1. Longest back-reference into the prefix (LZ77).\n    let matchLen = 0;\n    for (let off = 1; off <= i; off++) {\n      let len = 0;\n      while (\n        i + len < password.length &&\n        password[i - off + len] === password[i + len]\n      )\n        len++;\n      if (len > matchLen) matchLen = len;\n    }\n    if (matchLen >= 2) {\n      // Encoding: log2(offset space) + log2(length space).\n      bits += Math.log2(i + 1) + Math.log2(matchLen + 1);\n      i += matchLen;\n      continue;\n    }\n\n    // 2. Consecutive same or adjacent code-point (`aa`, `ab`, `12`).\n    if (i > 0) {\n      const delta = Math.abs(\n        password.charCodeAt(i) - password.charCodeAt(i - 1),\n      );\n      if (delta <= 1) {\n        bits += 1 + delta;\n        i += 1;\n        continue;\n      }\n    }\n\n    // 3. Otherwise pay the full per-character cost.\n    bits += perChar;\n    i += 1;\n  }\n\n  return bits;\n}\n\nexport const STRENGTH_LEVELS = [\n  { label: \"Too weak\", maxBits: 28 },\n  { label: \"Weak\", maxBits: 40 },\n  { label: \"Fair\", maxBits: 60 },\n  { label: \"Good\", maxBits: 80 },\n  { label: \"Strong\", maxBits: Infinity },\n] as const;\n\nexport function scoreFromEntropy(entropy: number): number {\n  return STRENGTH_LEVELS.findIndex((l) => entropy < l.maxBits);\n}\n",
      "type": "registry:lib"
    }
  ],
  "type": "registry:lib"
}