一、应用概述
1.1 应用简介
密码生成器(Password Generator)是一款安全密码生成工具。支持自定义密码长度、字符类型(大写/小写/数字/符号)、排除相似字符,并提供密码强度评估。该应用深入展示了ArkTS框架中的字符集处理、随机算法、密码强度计算和用户体验设计技术。
1.2 核心功能
| 功能模块 | 功能描述 | 技术实现 |
|---|---|---|
| 长度设置 | 密码长度4-32位 | 滑动条控制 |
| 字符类型 | 大写/小写/数字/符号 | 布尔开关 |
| 排除选项 | 排除相似/歧义字符 | 字符过滤 |
| 批量生成 | 一次生成多个密码 | 循环生成 |
| 强度评估 | 密码强度等级 | 评分算法 |
| 历史记录 | 生成历史保存 | 数组存储 |
二、密码生成算法
2.1 核心生成器
typescript
generatePassword(length: number, options: PasswordOptions): string {
let chars = '';
if (options.includeUppercase) chars += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
if (options.includeLowercase) chars += 'abcdefghijklmnopqrstuvwxyz';
if (options.includeNumbers) chars += '0123456789';
if (options.includeSymbols) chars += '!@#$%^&*()_+-=[]{}|;:,.<>?';
if (options.excludeSimilar) chars = this.removeChars(chars, 'il1Lo0O');
if (options.excludeAmbiguous) chars = this.removeChars(chars, '{}[]()/\\\'"`~,;:.<>');
if (chars.length === 0) chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
let password = '';
for (let i = 0; i < length; i++) {
password += chars[Math.floor(Math.random() * chars.length)];
}
return password;
}
三、密码强度评估
3.1 评分算法
typescript
evaluateStrength(password: string): PasswordStrength {
let score = 0;
if (password.length >= 8) score += 25;
if (password.length >= 12) score += 15;
if (password.length >= 16) score += 10;
if (/[A-Z]/.test(password)) score += 10;
if (/[a-z]/.test(password)) score += 10;
if (/[0-9]/.test(password)) score += 10;
if (/[^A-Za-z0-9]/.test(password)) score += 15;
if (password.length >= 12 && /[A-Z]/.test(password) && /[a-z]/.test(password) && /[0-9]/.test(password) && /[^A-Za-z0-9]/.test(password)) score += 15;
score = Math.min(100, score);
let level: string, color: string;
if (score < 40) { level = '弱'; color = '#FF5252'; }
else if (score < 70) { level = '中等'; color = '#FF9800'; }
else { level = '强'; color = '#4CAF50'; }
return { score, level, color };
}
四、总结
4.1 核心技术
- 随机密码生成算法
- 字符集管理
- 密码强度评估
- 批量生成
- 历史记录管理
4.2 扩展方向
- 密码安全性分析
- 密码过期提醒
- 密码管理器集成
- 自定义字符集
- 可读性密码生成