1.0.1 • Published 6 months ago
password-analysis v1.0.1
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:
- Length (0–20 points): Passwords are stronger when they are longer.
- Unique Characters (0–10 points): More unique characters increase the score.
- Uppercase Letters (0–10 points): Presence of uppercase letters increases the score.
- Lowercase Letters (0–10 points): Presence of lowercase letters increases the score.
- Numbers (0–10 points): Inclusion of numbers increases the score.
- Special Characters (0–15 points): Passwords with special characters (e.g., !@#) increase the score significantly.
- Common Passwords (penalty): Passwords found in common password lists are penalized.
Example Results
- 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