password-analysis
Version:
A library for analyzing the strength of a password
53 lines (52 loc) • 1.61 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.analyzePassword = analyzePassword;
const commonPasswords_json_1 = __importDefault(require("./commonPasswords.json"));
// Analyze password and return the AnalysisResult type
function analyzePassword(password) {
if (!password) {
throw new Error("Password cannot be empty.");
}
const common = commonPasswords_json_1.default.includes(password);
const lengthScore = Math.min(password.length, 20);
const uniqueChars = new Set(password).size;
const hasUpperCase = /[A-Z]/.test(password);
const hasLowerCase = /[a-z]/.test(password);
const hasNumbers = /\d/.test(password);
const hasSpecialChars = /[!@#$%^&*(),.?":{}|<>]/.test(password);
let score = lengthScore;
if (hasUpperCase)
score += 10;
if (hasLowerCase)
score += 10;
if (hasNumbers)
score += 10;
if (hasSpecialChars)
score += 15;
if (common) {
score -= 25;
}
const strength = score >= 75
? "strong"
: score >= 50
? "medium"
: score >= 25
? "weak"
: "very weak";
return {
score,
strength,
metrics: {
length: password.length,
uniqueChars,
hasUpperCase,
hasLowerCase,
hasNumbers,
hasSpecialChars,
isCommon: common,
},
};
}