Skip to content

Instantly share code, notes, and snippets.

@zhooravell
Last active March 9, 2020 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhooravell/fb06820a8c799e3a9ee1049312ca3b50 to your computer and use it in GitHub Desktop.
Save zhooravell/fb06820a8c799e3a9ee1049312ca3b50 to your computer and use it in GitHub Desktop.
GoLang password complexity
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