C語言實現(xiàn)密碼強度打分(0-100)

代碼搬運自Jonathan Zdziarski著《iOS應用安全攻防實戰(zhàn)》,這是本好書,讓我受益匪淺。

強壯的密碼一般遵循以下規(guī)則:

  • 盡量長的密碼長度
  • 包含大小寫字母
  • 組合中包含數(shù)字
  • 特殊字符,比如,#和標點
  • 避免特定的鍵盤輸入模式,比如,水平劃過QWERTY鍵盤
  • 避免使用在通話語言字典中可找到的單詞
  • 不含日期或者其他結(jié)構化數(shù)據(jù)

我們可以寫一個簡單的密碼檢查方法,檢查密碼長度,是否包含大小寫混合和特殊符號,還可以測量兩次輸入的鍵盤距離。

具體實現(xiàn)

#include <stdio.h>
#include <string.h>
#include <sys/param.h>
#include <ctype.h>
#include <stdlib.h>

int key_distance(char a, char b) {
    const char *qwerty_lc = "`1234567890-="
    "qwertyuiop[]\\"
    " asdfghjkl;' "
    "  zxcvbnm,./ ";
    const char *qwerty_uc = "~!@#$%^&*()_+"
    "QWERTYUIOP{}|"
    " ASDFGHJKL:\" "
    "  ZXCVBNM<>? ";
    int pos_a, pos_b, dist;
    
    if(strchr(qwerty_lc, a))
        pos_a = strchr(qwerty_lc, a) - qwerty_lc;
    else if (strchr(qwerty_uc, a))
        pos_a = strchr(qwerty_uc, a) - qwerty_uc;
    else
        return -2;
    
    if (strchr(qwerty_lc, b))
        pos_b = strchr(qwerty_lc, b) - qwerty_lc;
    else if (strchr(qwerty_uc, b))
        pos_b = strchr(qwerty_uc, b) - qwerty_uc;
    else
        return -1;
    dist = abs((pos_a/13) - (pos_b)/13)  /* 行距離 */
    + abs(pos_a % 13 - pos_b % 13); /* 列距離 */
    return dist;
    
    
}

int score_passphrase(const char *passphrase) {
    int total_score = 0;
    int unit_score;
    int distances[strlen(passphrase)];
    int i;
    
    /* 密碼長度 */
    unit_score = strlen(passphrase) / 4;
    total_score += MIN(3, unit_score);
    
    /* 大寫字母 */
    for (unit_score = i = 0; passphrase[i]; ++i) {
        if (isupper(passphrase[i])) {
            unit_score++;
        }
    }
    total_score += MIN(3, unit_score);
    
    /* 小寫字母 */
    for (unit_score = i = 0; passphrase[i]; ++i) {
        if (islower(passphrase[i])) {
            unit_score++;
        }
    }
    total_score += MIN(3, unit_score);
    
    /* 數(shù)字 */
    for (unit_score = i = 0; passphrase[i]; ++i) {
        if (isdigit(passphrase[i])) {
            unit_score++;
        }
    }
    total_score += MIN(3, unit_score);
    
    /* 特殊字符 */
    for (unit_score = i = 0; passphrase[i]; ++i) {
        if (!isalnum(passphrase[i])) {
            unit_score++;
        }
    }
    total_score += MIN(3, unit_score);
    
    /* 鍵盤距離 */
    distances[0] = 0;
    for (unit_score = i = 0; passphrase[i]; ++i) {
        if (passphrase[i+1]) {
            int dist = key_distance(passphrase[i], passphrase[i+1]);
            if (dist > 1) {
                int j, exists = 0;
                for (j=0; distances[j]; ++j) {
                    if (distances[j] == dist) {
                        exists = 1;
                    }
                }
                if (!exists) {
                    distances[j] = dist;
                    distances[j+1] = 0;
                    unit_score++;
                }
            }
        }
    }
    total_score += MIN(3, unit_score);
    return ((total_score / 18.0) * 100);
    
    
}

使用

結(jié)果為 0~100 的數(shù),分數(shù)越高,密碼強度越強。

NSString *passWord = @"qwikdhksja*lkfh23dksahf1";
int score = score_passphrase([passWord UTF8String]);
NSLog(@"%d",score); //結(jié)果為72
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容