Last active
March 9, 2020 20:14
-
-
Save zhooravell/fb06820a8c799e3a9ee1049312ca3b50 to your computer and use it in GitHub Desktop.
GoLang password complexity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"errors" | |
"unicode" | |
) | |
func ValidPassword(p string) error { | |
if len(p) < 7 { | |
return errors.New("invalid password length") | |
} | |
var ( | |
uppercasePresent bool | |
lowercasePresent bool | |
numberPresent bool | |
specialCharPresent bool | |
) | |
for _, r := range p { | |
switch { | |
case unicode.IsNumber(r): | |
numberPresent = true | |
case unicode.IsUpper(r): | |
uppercasePresent = true | |
case unicode.IsLower(r): | |
lowercasePresent = true | |
case unicode.IsPunct(r) || unicode.IsSymbol(r): | |
specialCharPresent = true | |
} | |
} | |
if !lowercasePresent { | |
return errors.New("lowercase letter missing") | |
} | |
if !uppercasePresent { | |
return errors.New("uppercase letter missing") | |
} | |
if !numberPresent { | |
return errors.New("numeric character missing") | |
} | |
if !specialCharPresent { | |
return errors.New("special character missing") | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment