Shell脚本如何检查密码强度等级

wen 实用脚本 1

本文目录导读:

Shell脚本如何检查密码强度等级

  1. 密码强度评估标准
  2. Shell脚本实现
  3. 更详细的版本(带分数显示)
  4. 使用方法
  5. 输出示例

我们可以通过一个Shell脚本来检查密码强度,通常基于以下标准评估等级(弱、中、强):

密码强度评估标准

  • :长度不足8位或仅包含一种字符类型
  • :长度≥8位且包含至少两种字符类型
  • :长度≥12位且包含至少三种字符类型(大写、小写、数字、特殊字符)

Shell脚本实现

#!/bin/bash
check_password_strength() {
    local password="$1"
    local length=${#password}
    local score=0
    # 检查长度得分
    if [ $length -ge 12 ]; then
        score=$((score + 2))
    elif [ $length -ge 8 ]; then
        score=$((score + 1))
    fi
    # 检查包含的字符类型
    local types=0
    # 检查小写字母
    if [[ "$password" =~ [a-z] ]]; then
        types=$((types + 1))
    fi
    # 检查大写字母
    if [[ "$password" =~ [A-Z] ]]; then
        types=$((types + 1))
    fi
    # 检查数字
    if [[ "$password" =~ [0-9] ]]; then
        types=$((types + 1))
    fi
    # 检查特殊字符
    if [[ "$password" =~ [^a-zA-Z0-9] ]]; then
        types=$((types + 1))
    fi
    # 根据字符类型数加分
    if [ $types -ge 3 ]; then
        score=$((score + 2))
    elif [ $types -ge 2 ]; then
        score=$((score + 1))
    fi
    # 输出密码强度
    if [ $score -ge 4 ]; then
        echo "强密码"
    elif [ $score -ge 2 ]; then
        echo "中密码"
    else
        echo "弱密码"
    fi
}
# 主程序
echo "请输入要检查的密码:"
read -s password
echo ""
result=$(check_password_strength "$password")
echo "密码强度: $result"
# 显示详细信息
echo -e "\n详细分析:"
echo "密码长度: ${#password}"
if [[ "$password" =~ [a-z] ]]; then echo "✓ 包含小写字母"; else echo "✗ 缺少小写字母"; fi
if [[ "$password" =~ [A-Z] ]]; then echo "✓ 包含大写字母"; else echo "✗ 缺少大写字母"; fi
if [[ "$password" =~ [0-9] ]]; then echo "✓ 包含数字"; else echo "✗ 缺少数字"; fi
if [[ "$password" =~ [^a-zA-Z0-9] ]]; then echo "✓ 包含特殊字符"; else echo "✗ 缺少特殊字符"; fi

更详细的版本(带分数显示)

#!/bin/bash
strength_checker() {
    local pass="$1"
    local len=${#pass}
    local types=0
    # 字符类型检查
    [[ "$pass" =~ [a-z] ]] && ((types++))
    [[ "$pass" =~ [A-Z] ]] && ((types++))
    [[ "$pass" =~ [0-9] ]] && ((types++))
    [[ "$pass" =~ [^a-zA-Z0-9] ]] && ((types++))
    # 计算分数
    local score=0
    # 长度评分(满分40分)
    if [ $len -ge 50 ]; then
        score=$((score + 40))
    elif [ $len -ge 20 ]; then
        score=$((score + 35))
    elif [ $len -ge 15 ]; then
        score=$((score + 30))
    elif [ $len -ge 12 ]; then
        score=$((score + 25))
    elif [ $len -ge 8 ]; then
        score=$((score + 15))
    else
        score=$((score + 5))
    fi
    # 字符类型评分(满分40分)
    case $types in
        4) score=$((score + 40)) ;;
        3) score=$((score + 30)) ;;
        2) score=$((score + 15)) ;;
        1) score=$((score + 5)) ;;
        0) score=$((score + 0)) ;;
    esac
    # 额外的复杂性评分(满分20分)
    # 检查是否有连续重复字符(扣分)
    if [[ "$pass" =~ (.)\1{2,} ]]; then
        score=$((score - 10))
    fi
    # 检查是否包含常见模式
    if [[ "$pass" =~ ^[a-zA-Z]+$ ]] || [[ "$pass" =~ ^[0-9]+$ ]]; then
        score=$((score - 10))
    fi
    # 确保分数在0-100之间
    [ $score -lt 0 ] && score=0
    [ $score -gt 100 ] && score=100
    echo "$score"
}
get_strength_level() {
    local score=$1
    if [ $score -ge 80 ]; then
        echo "非常强"
    elif [ $score -ge 60 ]; then
        echo "强"
    elif [ $score -ge 40 ]; then
        echo "中等"
    elif [ $score -ge 20 ]; then
        echo "弱"
    else
        echo "非常弱"
    fi
}
# 使用示例
echo "=== 密码强度检查工具 ==="
echo "请输入密码:"
read -s password
echo ""
score=$(strength_checker "$password")
level=$(get_strength_level $score)
echo "密码: [已隐藏]"
echo "长度: ${#password}"
echo "分数: $score/100"
echo "强度等级: $level"

使用方法

  1. 将脚本保存为 check_password.sh
  2. 赋予执行权限:chmod +x check_password.sh
  3. 运行:./check_password.sh
  4. 输入密码(不会被显示)

输出示例

=== 密码强度检查工具 ===
请输入密码:
密码: [已隐藏]
长度: 14
分数: 75/100
强度等级: 强

这个脚本提供了密码强度的详细评估,包括分数和等级,帮助用户了解密码的安全性。

抱歉,评论功能暂时关闭!