30 lines
857 B
Go
30 lines
857 B
Go
package administration
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"golang.org/x/crypto/scrypt"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
var phonePattern = regexp.MustCompile(`^\+?[0-9]{6,20}$`)
|
|
|
|
func NormalizePhone(value string) string {
|
|
r := strings.NewReplacer(" ", "", "\t", "", "\n", "", "\r", "", "(", "", ")", "", "-", "")
|
|
return r.Replace(strings.TrimSpace(value))
|
|
}
|
|
func ValidPhone(value string) bool { return phonePattern.MatchString(NormalizePhone(value)) }
|
|
func HashPassword(password string) (PasswordHash, error) {
|
|
salt := make([]byte, 16)
|
|
if _, err := rand.Read(salt); err != nil {
|
|
return PasswordHash{}, err
|
|
}
|
|
encoded := hex.EncodeToString(salt)
|
|
derived, err := scrypt.Key([]byte(password), []byte(encoded), 16384, 8, 1, 64)
|
|
if err != nil {
|
|
return PasswordHash{}, err
|
|
}
|
|
return PasswordHash{Hash: hex.EncodeToString(derived), Salt: encoded}, nil
|
|
}
|