Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

password-analysis

aalmogg15MIT1.0.1TypeScript support: included

A library for analyzing the strength of a password

password-analysis, password, password-strength, password-validator, password-checker, password-security, strong-password, password-meter, password-strength-checker, secure-passwords, react-password-analysis, password-validation, password-analyzer, password-library, authentication, security, user-authentication, frontend-security, npm-password-tool, password-complexity, password-entropy, password-policy

readme

Password Analysis For React

password-analysis is a password strength analyzer library for React that evaluates password strength based on various metrics such as length, character diversity, and commonness. It provides a score and description of password strength to help users create secure passwords.

Features

  • Length check: Stronger passwords are longer.
  • Character diversity: Considers the use of uppercase, lowercase, numbers, and special characters.
  • Common password check: Flags common or weak passwords that are easy to guess.
  • Score system: Generates a score to categorize the strength of the password.

Installation

You can install the library via npm or yarn:

npm install password-analysis

or

yarn add password-analysis

Usage

Import the analyzePassword function

import { analyzePassword } from 'password-analysis';

Example of password analysis

import { analyzePassword } from 'password-analysis';

const password = "P@ssw0rd1234!";
const result = analyzePassword(password);

console.log(result);

Result Format The analyzePassword function returns an object with the following structure:

{
    "score": 58,
    "strength": "medium",
    "metrics": {
        "length": 13,
        "uniqueChars": 12,
        "hasUpperCase": true,
        "hasLowerCase": true,
        "hasNumbers": true,
        "hasSpecialChars": true,
        "isCommon": false
    }
}

Score Description

Score (0-70): The strength of the password, with 70 being the strongest.

  • 0-20: Weak password.
  • 21-50: Medium strength password.
  • 51-70: Strong password.
  • 70: Very strong password.

How is the score calculated?

The score is based on the following factors:

  1. Length (0–20 points): Passwords are stronger when they are longer.
  2. Unique Characters (0–10 points): More unique characters increase the score.
  3. Uppercase Letters (0–10 points): Presence of uppercase letters increases the score.
  4. Lowercase Letters (0–10 points): Presence of lowercase letters increases the score.
  5. Numbers (0–10 points): Inclusion of numbers increases the score.
  6. Special Characters (0–15 points): Passwords with special characters (e.g., !@#) increase the score significantly.
  7. Common Passwords (penalty): Passwords found in common password lists are penalized.

Example Results

  1. Weak Password:
    const password = "123456";
    const result = analyzePassword(password);
    console.log(result);

Output:

{
    "score": 10,
    "strength": "weak",
    "metrics": {
        "length": 6,
        "uniqueChars": 3,
        "hasUpperCase": false,
        "hasLowerCase": false,
        "hasNumbers": true,
        "hasSpecialChars": false,
        "isCommon": true
    }
}
  • Score: 10 (Weak password)
  • Strength: Weak

  • Medium Strength Password:

    const password = "P@ssw0rd1234!";
    const result = analyzePassword(password);
    console.log(result);

Output:

{
    "score": 58,
    "strength": "medium",
    "metrics": {
        "length": 13,
        "uniqueChars": 12,
        "hasUpperCase": true,
        "hasLowerCase": true,
        "hasNumbers": true,
        "hasSpecialChars": true,
        "isCommon": false
    }
}
  • Score: 58 (Medium strength)
  • Strength: Medium

  • Strong Password:

    const password = "A@z9d3Qr!lP0sXy7";
    const result = analyzePassword(password);
    console.log(result);

Output:

{
    "score": 70,
    "strength": "strong",
    "metrics": {
        "length": 18,
        "uniqueChars": 18,
        "hasUpperCase": true,
        "hasLowerCase": true,
        "hasNumbers": true,
        "hasSpecialChars": true,
        "isCommon": false
    }
}
  • Score: 70 (Strong password)
  • Strength: Strong

API

analyzePassword(password: string): AnalysisResult

Parameters:

  • password (string): The password to analyze.

Returns:

  • An object of type AnalysisResult containing:
    • score (number): The password score (0-70).
    • strength (string): The strength of the password (weak, medium, strong).
    • metrics (object):
      • length (number): Length of the password.
      • uniqueChars (number): Number of unique characters.
      • hasUpperCase (boolean): Whether the password contains uppercase letters.
      • hasLowerCase (boolean): Whether the password contains lowercase letters.
      • hasNumbers (boolean): Whether the password contains numbers.
      • hasSpecialChars (boolean): Whether the password contains special characters.
      • isCommon (boolean): Whether the password is common.

License

MIT