-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
57 lines (46 loc) · 1.07 KB
/
util.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package notary
import (
"fmt"
"regexp"
"golang.org/x/crypto/bcrypt"
)
type RegexUtil struct {
email *regexp.Regexp
}
type ErrRequired struct {
error
arg string
}
type ErrInvalidValue struct {
error
arg string
}
func (e *ErrRequired) Error() string {
return fmt.Sprintf("%s is required!", e.arg)
}
func (e *ErrInvalidValue) Error() string {
return fmt.Sprintf("%s is invalid!", e.arg)
}
func NewRegexUtil() *RegexUtil {
r := RegexUtil{}
if e, err := regexp.Compile("(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$)"); err != nil {
fmt.Errorf("Couldn't compile regex! %v", err.Error())
} else {
r.email = e
}
return &r
}
func (r *RegexUtil) MatchEmail(email string) bool {
return r.email.Match([]byte(email))
}
func HashPassword(password string) (string, error) {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
return string(bytes), err
}
func CheckPasswordHash(password, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
if err != nil {
fmt.Printf("bad password: %v", err)
}
return err == nil
}