-
Notifications
You must be signed in to change notification settings - Fork 16
/
utils.go
28 lines (22 loc) 路 809 Bytes
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package utils
import (
"regexp"
"golang.org/x/crypto/bcrypt"
)
var emailRegex = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
// IsEmailValid checks if the email provided passes the required structure and length.
func IsEmailValid(e string) bool {
if len(e) < 3 && len(e) > 254 {
return false
}
return emailRegex.MatchString(e)
}
// HashPassword hashes password with bcrypt
func HashPassword(pass string) ([]byte, error) {
return bcrypt.GenerateFromPassword([]byte(pass), bcrypt.DefaultCost)
}
// CheckPasswordHash checks hash of password
func CheckPasswordHash(password, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}