tsk-30: Update password (#33)
Closes #30 Reviewed-on: phoenix/textsender-auth#33 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
@@ -1,15 +1,19 @@
|
||||
package utility
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
type HashMash struct {
|
||||
Password string
|
||||
password string
|
||||
}
|
||||
|
||||
func (h *HashMash) HashPassword() (string, error) {
|
||||
bytes, err := bcrypt.GenerateFromPassword([]byte(h.Password), bcrypt.DefaultCost)
|
||||
bytes, err := bcrypt.GenerateFromPassword([]byte(h.password), bcrypt.DefaultCost)
|
||||
return string(bytes), err
|
||||
}
|
||||
|
||||
@@ -18,6 +22,41 @@ func (h *HashMash) CheckPasswordHash(password string, hash string) bool {
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func (h *HashMash) SetPassword(password string) {
|
||||
h.Password = password
|
||||
func (h *HashMash) SetPassword(password string) error {
|
||||
if len(password) < 8 {
|
||||
return fmt.Errorf("Password length is not enought")
|
||||
} else if len(password) > 32 {
|
||||
return fmt.Errorf("Password length is too long")
|
||||
} else {
|
||||
specialCharacters := "!@#$%^&*?"
|
||||
numbers := "0123456789"
|
||||
if strings.ContainsAny(password, specialCharacters) && strings.ContainsAny(password, numbers) {
|
||||
var hasAtleastOneUpper, hasAtleastOneLower bool
|
||||
|
||||
for _, c := range password {
|
||||
if unicode.IsUpper(c) {
|
||||
hasAtleastOneUpper = true
|
||||
} else if unicode.IsLower(c) {
|
||||
hasAtleastOneLower = true
|
||||
}
|
||||
|
||||
if hasAtleastOneLower && hasAtleastOneUpper {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if hasAtleastOneUpper && hasAtleastOneLower {
|
||||
h.password = password
|
||||
return nil
|
||||
} else {
|
||||
if !hasAtleastOneUpper {
|
||||
return fmt.Errorf("Password requires at least one upper case letter")
|
||||
} else {
|
||||
return fmt.Errorf("Password requires at least one lower case letter")
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return fmt.Errorf("Password should contain special characters and numbers")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user