数字仅密码强度

本文关键字:密码 数字 | 更新日期: 2023-09-27 18:36:16

在我的组织中,用户必须仅从数字生成密码(键盘用于访问),最小长度为 8 个数字。如何确保用户生成的密码不会太弱(在服务器代理密码更改请求上使用 C#),应用以下规则:

  • 3 以下数字(甚至是密码的一部分)不连续或重复(9451238401 或 543555784)

数字仅密码强度

正则表达式为:

^((?!(?<ch>.)'k<ch>'k<ch>)(?!012|123|234|345|456|567|678|789|890)[0-9]){8,}$

(?!(?<ch>.)'k<ch>'k<ch>)将检查重复三次的同一字符。请注意,对于各种连续序列,我必须将它们放在可能的序列列表中,(?!012|123|234|345|456|567|678|789|890) . [0-9]是将被接受为有效的字符。{8,}适用于最小长度。

如果你

想要一个通用的方法,告诉你重复的、升序的和降序数字的数量:

static void checkStrength(string text, out int maxRepeats, out int maxAscending, out int maxDescending)
{ 
    maxRepeats     = 0;
    maxAscending   = 0;
    maxDescending  = 0;
    int currRepeats    = 0;
    int currAscending  = 0;
    int currDescending = 0;
    for (int i = 1; i < text.Length; ++i)
    {
        char curr = text[i];
        char prev = text[i-1];
        if (curr - prev == -1)
            maxDescending = Math.Max(maxDescending, ++currDescending);
        else
            currDescending = 1;
        if (curr - prev == 1)
            maxAscending = Math.Max(maxAscending, ++currAscending);
        else
            currAscending = 1;
        if (curr == prev)
            maxRepeats = Math.Max(maxRepeats, ++currRepeats);
        else
            currRepeats = 1;
    }
}

您必须调用它,然后对结果执行所需的操作:

int maxRepeats, maxAscending, maxDescending;
checkStrength(text, out maxRepeats, out maxAscending, out maxDescending);
if (maxRepeats > REPEAT_LIMIT || maxAscending > ASCENDING_LIMIT || maxDescending > DESCENDING_LIMIT)
{
    // ... report error or whatever
}

如果你不需要改变允许的重复或升序数字的数量,那么xanatos的正则表达式显然是迄今为止最短的代码。仅当需要在运行时更改允许的计数时,才需要此代码。