-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.go
47 lines (31 loc) · 951 Bytes
/
hash.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
package hash
import (
"log"
"golang.org/x/crypto/bcrypt"
)
func GenerateHashFromText(text string) (hash string, err error) {
if hash, err = generateHashFromText(text); err != nil {
log.Println("hash : GenerateHashFromText : ", err)
}
return
}
func CompareHashAndText(hashFromDB string, text string) (err error) {
if err = compareHashAndText(hashFromDB, text); err != nil {
log.Println("hash: CompareHashAndText : ", err)
}
return
}
func generateHashFromText(text string) (hash string, err error) {
var hashByte []byte
if hashByte, err = bcrypt.GenerateFromPassword([]byte(text), bcrypt.DefaultCost); err != nil {
log.Println("hash: generateHashFromText : ", err)
}
hash = string(hashByte[:])
return
}
func compareHashAndText(hashFromDB, text string) (err error) {
if err = bcrypt.CompareHashAndPassword([]byte(hashFromDB), []byte(text)); err != nil {
log.Println("hash : compareHashAndText : ", err)
}
return
}