Ed *_*d L 43 security passwords algorithm
在注册或更改密码表单中,确保用户提供密码的最佳方法是什么?
我有一个想法(在python中)
def validate_password(passwd):
conditions_met = 0
conditions_total = 3
if len(passwd) >= 6:
if passwd.lower() != passwd: conditions_met += 1
if len([x for x in passwd if x.isdigit()]) > 0: conditions_met += 1
if len([x for x in passwd if not x.isalnum()]) > 0: conditions_met += 1
result = False
print conditions_met
if conditions_met >= 2: result = True
return result
Run Code Online (Sandbox Code Playgroud)
Vir*_*dia 17
根据语言,我通常使用正则表达式来检查它是否具有:
您可以要求以上所有,或使用强度计类型的脚本.对于我的力量计,如果密码长度合适,则评估如下:
您可以调整以上内容以满足您的需求.
use*_*116 10
面向对象的方法是一组规则.为每个规则分配权重并迭代它们.在伪代码中:
abstract class Rule {
float weight;
float calculateScore( string password );
}
Run Code Online (Sandbox Code Playgroud)
计算总分:
float getPasswordStrength( string password ) {
float totalWeight = 0.0f;
float totalScore = 0.0f;
foreach ( rule in rules ) {
totalWeight += weight;
totalScore += rule.calculateScore( password ) * rule.weight;
}
return (totalScore / totalWeight) / rules.count;
}
Run Code Online (Sandbox Code Playgroud)
一个示例规则算法,基于存在的字符类数量:
float calculateScore( string password ) {
float score = 0.0f;
// NUMBER_CLASS is a constant char array { '0', '1', '2', ... }
if ( password.contains( NUMBER_CLASS ) )
score += 1.0f;
if ( password.contains( UPPERCASE_CLASS ) )
score += 1.0f;
if ( password.contains( LOWERCASE_CLASS ) )
score += 1.0f;
// Sub rule as private method
if ( containsPunctuation( password ) )
score += 1.0f;
return score / 4.0f;
}
Run Code Online (Sandbox Code Playgroud)
1:消除常用
密码对照常用密码列表检查输入的密码(例如,参见泄露的 LinkedIn 密码列表中的前 100.000 个密码:http : //www.adeptus-mechanicus.com/codex/linkhap/combo_not.zip ),请确保包含leetpeek 替换:A@、E3、B8、S5 等。
在转到下面的第 2 部分之前,从输入的短语中删除与此列表匹配的部分密码。
2:不要对用户强加任何规则
密码的黄金法则是越长越好。
忘记强制使用大写、数字和符号,因为(绝大多数)用户会: - 将第一个字母设为大写;- 把数字1
放在最后;-!
如果需要符号,则在其后放一个。
而是检查密码强度
有关体面的起点,请参阅:http : //www.passwordmeter.com/
我建议至少遵循以下规则:
Additions (better passwords)
-----------------------------
- Number of Characters Flat +(n*4)
- Uppercase Letters Cond/Incr +((len-n)*2)
- Lowercase Letters Cond/Incr +((len-n)*2)
- Numbers Cond +(n*4)
- Symbols Flat +(n*6)
- Middle Numbers or Symbols Flat +(n*2)
- Shannon Entropy Complex *EntropyScore
Deductions (worse passwords)
-----------------------------
- Letters Only Flat -n
- Numbers Only Flat -(n*16)
- Repeat Chars (Case Insensitive) Complex -
- Consecutive Uppercase Letters Flat -(n*2)
- Consecutive Lowercase Letters Flat -(n*2)
- Consecutive Numbers Flat -(n*2)
- Sequential Letters (3+) Flat -(n*3)
- Sequential Numbers (3+) Flat -(n*3)
- Sequential Symbols (3+) Flat -(n*3)
- Repeated words Complex -
- Only 1st char is uppercase Flat -n
- Last (non symbol) char is number Flat -n
- Only last char is symbol Flat -n
Run Code Online (Sandbox Code Playgroud)
仅仅关注密码表是不够的,因为它的幼稚算法肯定Password1!是好的,而它却异常薄弱。确保在评分时忽略首字母大写以及尾随数字和符号(根据最后 3 条规则)。
计算香农熵
请参阅:在 Python 中计算熵的最快方法
3:不允许任何太弱的密码
与其强迫用户屈服于自欺欺人的规则,不如允许任何能够给出足够高分数的密码。多高取决于您的用例。
最重要的是,
当您接受密码并将其存储在数据库中时,请确保对其进行加盐和散列!.