forked from josephspurrier/gowebapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
passhash.go
45 lines (38 loc) · 1010 Bytes
/
passhash.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
package passhash
import (
"golang.org/x/crypto/bcrypt"
)
// HashString returns a hashed string and an error
func HashString(password string) (string, error) {
key, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return "", err
}
return string(key), nil
}
// HashBytes returns a hashed byte array and an error
func HashBytes(password []byte) ([]byte, error) {
key, err := bcrypt.GenerateFromPassword(password, bcrypt.DefaultCost)
if err != nil {
return nil, err
}
return key, nil
}
// MatchString returns true if the hash matches the password
func MatchString(hash, password string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
if err == nil {
return true
} else {
return false
}
}
// MatchBytes returns true if the hash matches the password
func MatchBytes(hash, password []byte) bool {
err := bcrypt.CompareHashAndPassword(hash, []byte(password))
if err == nil {
return true
} else {
return false
}
}