Skip to content

Instantly share code, notes, and snippets.

@xigang
Created November 15, 2016 03:02
Show Gist options
  • Save xigang/fd09a381960387f1e23563358e86c508 to your computer and use it in GitHub Desktop.
Save xigang/fd09a381960387f1e23563358e86c508 to your computer and use it in GitHub Desktop.
校验密码。密码强度要求:必须包含大写字母、小写字母、数字、特殊字符(!@#$%^&*_-)中的至少三种
func PasswordCheck(passwd string) error {
indNum := [4]int{0, 0, 0, 0}
spCode := []byte{'!', '@', '#', '$', '%', '^', '&', '*', '_', '-'}
if len(passwd) < 6 {
return errors.New("password too short")
}
passwdByte := []byte(passwd)
for _, i := range passwdByte {
if i >= 'A' && i <= 'Z' {
indNum[0] = 1
continue
}
if i >= 'a' && i <= 'z' {
indNum[1] = 1
continue
}
if i >= '0' && i <= '9' {
indNum[2] = 1
continue
}
notEnd := 0
for _, s := range spCode {
if i == s {
indNum[3] = 1
notEnd = 1
break
}
}
if notEnd != 1 {
return errors.New("Unsupport code")
}
}
codeCount := 0
for _, i := range indNum {
codeCount += i
}
if codeCount < 3 {
return errors.New("Too simple password")
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment