正则密码 Regex and Password complexity policy
Regex for password must contain at least eight characters, at least one number and both lower and uppercase letters and special characters
回答1
Minimum eight characters, at least one letter and one number:
"^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$"
Minimum eight characters, at least one letter, one number and one special character:
"^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{8,}$"
Minimum eight characters, at least one uppercase letter, one lowercase letter and one number:
"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$"
Minimum eight characters, at least one uppercase letter, one lowercase letter, one number and one special character:
"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$"
Minimum eight and maximum 10 characters, at least one uppercase letter, one lowercase letter, one number and one special character:
"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,10}$"
回答2
You may use this regex with multiple lookahead assertions (conditions):
^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$
This regex will enforce these rules:
- At least one upper case English letter,
(?=.*?[A-Z])
- At least one lower case English letter,
(?=.*?[a-z])
- At least one digit,
(?=.*?[0-9])
- At least one special character,
(?=.*?[#?!@$%^&*-])
- Minimum eight in length
.{8,}
(with the anchors)
Password Special Characters
The same list as string (between double quotes): " !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"
Various operating systems and applications may apply limitations to this set:
可用的筛选密码复杂度的正则
如果密码规则,要求至少一个数字,至少一个小写字母,至少一个大写字幕,至少一个特殊字符的话,并且最小长度为10,最大长度为50的话。
(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[ !"#$%&'()*+,-.\/:;<=>?@[\]^_`{|}~]).{10,50}
看图解析的话,预查匹配可以先跳过,直接看后面的, .{10,50}任意字符,最小10个,最多50个。然后再进行非获取匹配。
Example:
/(?=.[A-Z])(?=.[a-z])(?=.*[0-9])[a-zA-Z0-9]{8,15}/
密码长度限制
https://cheatsheetseries.owasp.org/cheatsheets/Authentication_Cheat_Sheet.html
Implement Proper Password Strength Controls
A key concern when using passwords for authentication is password strength. A "strong" password policy makes it difficult or even improbable for one to guess the password through either manual or automated means. The following characteristics define a strong password:
-
Password Length
- Minimum length of the passwords should be enforced by the application. Passwords shorter than 8 characters are considered to be weak (NIST SP800-63B).
- Maximum password length should not be set too low, as it will prevent users from creating passphrases. Typical maximum length is 128 characters. It is important to set a maximum password length to prevent long password Denial of Service attacks.
-
Some password hashes such as Bcrypt truncate the input, so a shorter maximum length may be required, as discussed in the Password Storage Cheat Sheet.
When selecting a maximum password length, consider whether the hashing algorithm to be used has any limitations because some have a maximum password length.
-
Do not truncate passwords. Make sure that every character the user types in is actually included in the password.
-
Allow usage of all characters including unicode and whitespace. There should be no password composition rules limiting the type of characters permitted.
-
Ensure credential rotation when a password leak, or at the time of compromise identification.
-
Include password strength meter to help users create a more complex password and block common and previously breached passwords
- zxcvbn library can be used for this purpose. (Note that this library is no longer maintained)
- Pwned Passwords is a service where passwords can be checked against previously breached passwords. You can host it yourself or use API.
Long password denial of service
By sending a very long password (1.000.000 characters) it's possible to cause a denial a service attack on the server. This may lead to the website becoming unavailable or unresponsive. Usually this problem is caused by a vulnerable password hashing implementation. When a long password is sent, the password hashing process will result in CPU and memory exhaustion.
This vulnerability was detected by sending passwords with various lengths and comparing the measured response times. Consult details for more information.
Remediation
The password hashing implementation must be fixed to limit the maximum length of accepted passwords.
错误的正则
(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]).{8,25}这个正则需要修改为(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]).{8,15}才可以
(?=是用来做正向匹配的) (?=pattern)
非获取匹配,正向肯定预查,在任何匹配pattern的字符串开始处匹配查找字符串,该匹配不需要获取供以后使用。
例如,“Windows(?=95|98|NT|2000)”能匹配“Windows2000”中的“Windows”,但不能匹配“Windows3.1”中的“Windows”。
预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索,而不是从包含预查的字符之后开始。
https://regexr.com/3bfsi 别人提供的密码正则
https://owasp.org/www-community/password-special-characters
密码中允许使用的特殊字符
https://docs.oracle.com/cd/E11223_01/doc.910/e11197/app_special_char.htm#MCMAD416
(pattern)匹配pattern并获取这一匹配。
(?=pattern) 非获取匹配,正向肯定预查,在任何匹配pattern的字符串开始处匹配查找字符串,该匹配不需要获取供以后使用。
例如,“Windows(?=95|98|NT|2000)”能匹配“Windows2000”中的“Windows”,但不能匹配“Windows3.1”中的“Windows”。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索,而不是从包含预查的字符之后开始。
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2018-04-20 system.web section group下的section
2018-04-20 Configuration.SectionGroups
2016-04-20 What is the difference between task and thread?