美文网首页
c语言计算密码强度

c语言计算密码强度

作者: 一路向后 | 来源:发表于2021-04-08 22:05 被阅读0次

    1.源码实现

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    /* 1-小写字母, 2-大写字母, 3-数字, 4-符号, 5-其它 */
    int initmap(int *map)
    {
        int i;
    
        for(i=0; i<256; i++)
        {
            map[i] = 5;
    
            if(i>=0x21 && i<=0x2F)
            {
                map[i] = 4;
            }
            else if(i>=0x3a && i<=0x40)
            {
                map[i] = 4;
            }
            else if(i>=0x5b && i<=0x60)
            {
                map[i] = 4;
            }
            else if(i>=0x7b && i<=0x7e)
            {
                map[i] = 4;
            }
        }
    
        for(i=0; i<26; i++)
        {
            map[i+'a'] = 1;
            map[i+'A'] = 2;
        }
    
        for(i=0; i<10; i++)
        {
            map[i+'0'] = 3;
        }
    
        return 0;
    }
    
    int passinfo(int *map, char *passwd, int *num)
    {
        int i = 0;
    
        for(i=0; passwd[i]!=0x00; i++)
        {
            num[map[passwd[i]]]++;
        }
    
        for(i=1; i<6; i++)
        {
            num[0] += num[i];
        }
    
        return 0;
    }
    
    int getscore(int *map, int *num)
    {
        int score = 0;
        int len;
    
        len = num[0];
        if(len <= 4)
        {
            score += 5;
        }
        else if(len <= 7)
        {
            score += 10;
        }
        else
        {
            score += 25;
        }
    
    #ifdef SCORE_DEBUG
        printf("score=%d\n", score);
    #endif
    
        if(!(num[1]+num[2]))
        {
            score += 0;
        }
        else if(!(num[1] && num[2]))
        {
            score += 10;
        }
        else
        {
            score += 20;
        }
    
    #ifdef SCORE_DEBUG
        printf("score=%d\n", score);
    #endif
    
        if(!num[3])
        {
            score += 0;
        }
        else if(num[3] == 1)
        {
            score += 10;
        }
        else
        {
            score += 20;
        }
    
    #ifdef SCORE_DEBUG
        printf("score=%d\n", score);
    #endif
    
        if(!num[4])
        {
            score += 0;
        }
        else if(num[4] == 1)
        {
            score += 10;
        }
        else
        {
            score += 25;
        }
    
    #ifdef SCORE_DEBUG
        printf("score=%d\n", score);
    #endif
    
        if(num[1] && num[2] && num[3] && num[4])
        {
            score += 5;
        }
        else if((num[1]||num[2]) && num[3] && num[4])
        {
            score += 3;
        }
        else if((num[1]||num[2]) && num[3])
        {
            score += 2;
        }
    
    #ifdef SCORE_DEBUG
        printf("score=%d\n", score);
    #endif
    
        return score;
    }
    
    int output(short score)
    {
        if(score >= 90)
        {
            printf("VERY_SECURE\n");
        }
        else if(score >= 80)
        {
            printf("SECURE\n");
        }
        else if(score >= 70)
        {
            printf("VERY_STRONG\n");
        }
        else if(score >= 60)
        {
            printf("STRONG\n");
        }
        else if(score >= 50)
        {
            printf("AVERAGE\n");
        }
        else if(score >= 25)
        {
            printf("WEAK\n");
        }
        else if(score >= 0)
        {
            printf("VERY_WEAK\n");
        }
    
        return 0;
    }
    
    int main()
    {
        char passwd[1024];
        int map[256];
        int num[10];
        int score;
    
        initmap(map);
    
        memset(passwd, 0x00, sizeof(passwd));
        memset(num, 0x00, sizeof(num));
    
        while(scanf("%s", passwd) != EOF)
        {
            passinfo(map, passwd, num);
    
            score = getscore(map, num);
    
            output(score);
    
            memset(passwd, 0x00, sizeof(passwd));
            memset(num, 0x00, sizeof(num));
        }
    
        return 0;
    }
    

    2.编译源码

    $ gcc -o example examle.c -std=c89
    

    4.运行及其结果

    $ ./example
    38$@NoNoNo
    VERY_SECURE
    123
    WEAK
    

    相关文章

      网友评论

          本文标题:c语言计算密码强度

          本文链接:https://www.haomeiwen.com/subject/gtkbkltx.html