基本概念理解
通過添加自定義限制來修改應(yīng)用的強密碼規(guī)則。
雖然自動填充密碼生成的強密碼安全性已經(jīng)很好了,但我們的應(yīng)用可能需要自定義的限制才能與其他技術(shù)保持兼容。
遵循UITextInputTraits協(xié)議UIKit控件(UITextField、UITextView),通過設(shè)置
passwordRules屬性或網(wǎng)頁中HTML input元素中的passwordrules屬性來自定義自動生成的密碼的密碼規(guī)則。
這兩個屬性的值必須遵循以下形式的相同限制:
required: (<identifier> | <character-class>), ..., (<identifier> | <character-class>); allowed: (<identifier> | <character-class>), ..., (<identifier> | <character-class>); max-consecutive: <non-negative-integer>
使用關(guān)鍵字組合來指定規(guī)則:
- required&allowed關(guān)鍵字:如果所有密碼都必須遵循某個限制條件,則使用required關(guān)鍵字; 如果限制條件是指定允許字符的子集,則允許allowed關(guān)鍵字。 如果不包含allowed關(guān)鍵字,則允許使用所有必需字符。 如果包含這兩個屬性,則允許使用所有允許和必需的字符。 如果兩者都未指定,則允許所有ASCII可打印字符
- 限制條件:可以使用大寫(A-Z),小寫(a-z),數(shù)字(0-9),特殊字符(-?!@#$%^&* _+=`|(){}[:;“'<>,.?]和空格),ascii-printable(所有ASCII可打印字符)或unicode(所有unicode字符)
- 最大長度關(guān)鍵字:使用max-consecutive指定密碼中連續(xù)字符的最大長度。 如果規(guī)則中有多個最大連續(xù)屬性,則將應(yīng)用屬性的最小值。 如果沒有此屬性,密碼可以是任何長度。
- 自定義字符類:<character-class>是自定義字符類。 此屬性包含由方括號括起的ASCII字符列表。 例如,[abc]僅允許字符“a”,“b”和“c”。
- Non-negative類:<非負整數(shù)>是有效的非負整數(shù)。 此屬性用于指定max-consecutive屬性,因為最大長度不能為負。
應(yīng)用默認密碼規(guī)則:沒有任何自定義的密碼規(guī)則。 它允許所有ASCII可打印字符,寫為allowed:ascii-printable。
可以組合這些關(guān)鍵字來形成規(guī)則。 原則上會忽略重復的屬性值,空字符類和沒有值的屬性。 如果使用密碼規(guī)則,請不要指定pattern屬性。 如果您有兩個密碼字段(一個用于密碼,一個用于確認),則當用戶輸入新密碼時,您無需為這兩個字段指定密碼規(guī)則。
Warning
您對密碼的限制越多,被攻破的可能性就越高。 最難猜測的密碼規(guī)則就是allowed:unicode。
假如指定的密碼長度不能小于12,允許的字符必須至少包含以下兩個類:ASCII大寫字母,ASCII小寫字母和數(shù)字。 如果在為UITextField、UITextView設(shè)置了密碼規(guī)則之后,但是密碼限制條件不符合這些準則,則用戶代理將忽略這些屬性。
構(gòu)建密碼規(guī)則
例如,假設(shè)您要求密碼至少包含八個字符,這些字符由大寫和小寫字母以及至少一個數(shù)字和最多兩個連續(xù)字符組成
let newPasswordTextField = UITextField()
newPasswordTextField.passwordRules = UITextInputPasswordRules(descriptor: "required: upper; required: lower; required: digit; max-consecutive: 2; minlength: 8;")
在上面的前提下變更一下對于數(shù)字部分的要求:至少一個數(shù)字或一個特殊字符,兩者有其一即可
let newPasswordTextField = UITextField()
newPasswordTextField.passwordRules = UITextInputPasswordRules(descriptor: "required: upper; required: lower; required: digit, [-().&@?'#,/"+]; max-consecutive: 2; minlength: 8;")
至少需要一組特殊字符( - ()。&@?'#,/“+):
let newPasswordTextField = UITextField()
newPasswordTextField.passwordRules = UITextInputPasswordRules(descriptor: "required: upper; required: lower; required: digit; required: [-().&@?'#,/"+]; max-consecutive: 2; minlength: 8;")
或者,要選擇允許一個特殊字符
let newPasswordTextField = UITextField()
newPasswordTextField.passwordRules = UITextInputPasswordRules(descriptor: "required: upper; required: lower; required: digit; allowed: [-().&@?'#,/"+]; max-consecutive: 2; minlength: 8;")
另外一個例子是,要允許密碼包含任意組合的字母,數(shù)字和特殊字符
let newPasswordTextField = UITextField()
newPasswordTextField.passwordRules = UITextInputPasswordRules(descriptor: "allowed: upper, lower, digit, [-().&@?’#,/"+]; minlength: 8;")
指定多個字符類相當于指定一個字符類,該字符類表示所有字符類中字符的并集。
這種等效性的例外是必需的。 密碼必須在每個指定的必需屬性中包含至少一個字符。 例如:
allowed: upper; allowed: lower <=> allowed: upper, lower
required: upper; required: lower <=> required: upper; required: lower
完整事例
UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(100, 30, 200, 50)];
field.textColor = [UIColor blackColor];
field.backgroundColor = [UIColor yellowColor];
field.secureTextEntry = YES;
if (@available(iOS 12.0, *)) {
field.textContentType = UITextContentTypeNewPassword;
UITextInputPasswordRules *passwordRules = [UITextInputPasswordRules passwordRulesWithDescriptor:@"required: upper; required: lower; required: digit; max-consecutive: 2; minlength: 8;"];
field.passwordRules = passwordRules;
} else {
// Fallback on earlier versions
field.textContentType = UITextContentTypePassword;
}
[self.view addSubview:field];
passwordRules屬性用于傳達服務(wù)密碼的要求,以確保iOS可以為用戶生成兼容的密碼。 它僅在secureTextEntry為YES時有效。 如果iOS生成的密碼已與您的服務(wù)兼容,則無需使用此屬性。 您可以在“密碼規(guī)則”文檔指南中了解有關(guān)這些規(guī)則的用途和語法的更多信息。