zxcvbn-typescript

5.0.1 · active · verified Tue Apr 21

The `zxcvbn-typescript` library offers a realistic and robust approach to password strength estimation, ported to TypeScript from Dan Wheeler's original zxcvbn project. It evaluates password quality by analyzing various patterns, including common words, names, dates, sequences, keyboard patterns, and leetspeak, providing a numerical score and targeted verbal feedback to guide users. The current stable version is 5.0.1. With its v5.0.0 release, the library was fully converted to TypeScript, enhancing type safety and maintainability for modern development environments. While a strict release cadence isn't defined, updates typically align with algorithm refinements or significant refactors. Its key differentiator remains its comprehensive pattern-matching capabilities, which often provide more nuanced security assessments than simpler entropy-based methods.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to import the `zxcvbn` function, evaluate a password, and interpret the returned strength score, guess estimations, and verbal feedback, including handling optional user inputs for enhanced security.

import zxcvbn from 'zxcvbn-typescript';
import type { ZXCVBNResult } from 'zxcvbn-typescript';

/**
 * Evaluates a password's strength and logs detailed feedback.
 * @param password The password string to evaluate.
 * @param userInputs Optional array of strings (e.g., username, email) to penalize if found in the password.
 * @returns The detailed `ZXCVBNResult` object.
 */
function evaluatePassword(password: string, userInputs: string[] = []): ZXCVBNResult {
  const result = zxcvbn(password, userInputs);
  
  console.log(`Password: "${password}"`);
  console.log(`Score (0-4): ${result.score}`);
  console.log(`Estimated guesses: ${result.guesses.toLocaleString()}`);
  console.log(`Feedback: ${result.feedback.warning || 'Looks good!'}`);
  result.feedback.suggestions.forEach(suggestion => {
    console.log(`- Suggestion: ${suggestion}`);
  });

  // Accessing various crack time estimations
  console.log(`\nCrack Times:`);
  console.log(`  Online (throttled): ${result.crack_times_display.online_throttling_100_per_hour}`);
  console.log(`  Offline (slow hash): ${result.crack_times_display.offline_slow_hashing_1e4_per_second}`);
  
  return result;
}

// Example usage with different password strengths and user inputs
const weakPassword = 'password123';
const moderatePassword = 'MySecretPassword1!';
const strongPassword = 'correct horse battery staple';
const userEmail = 'test@example.com';
const userName = 'JohnDoe';

console.log('--- Evaluating Weak Password ---');
evaluatePassword(weakPassword, [userEmail, userName]);

console.log('\n--- Evaluating Moderate Password ---');
evaluatePassword(moderatePassword, ['mysecret', 'password']);

console.log('\n--- Evaluating Strong Password ---');
evaluatePassword(strongPassword, ['correcthorse']);

view raw JSON →