/
crypto.go
36 lines (32 loc) · 1004 Bytes
/
crypto.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
package utils
import (
"golang.org/x/crypto/bcrypt"
)
//HashAndSalt password
func HashAndSalt(pwd []byte) string {
// Use GenerateFromPassword to hash & salt pwd.
// MinCost is just an integer constant provied by the bcrypt
// package along with DefaultCost & MaxCost.
// The cost can be any alue you want provided it isn't lower
// than the MinCost (4)
hash, err := bcrypt.GenerateFromPassword(pwd, bcrypt.DefaultCost)
if err != nil {
Logger.Error(err.Error())
return ""
}
// GenerateFromPassword returns a byte slice s we need to
// convert the byte to a string and return it
return string(hash)
}
//ComparePasswords password
func ComparePasswords(hashedPwd string, plainPwd []byte) bool {
// Since we'll be getting thehashed password from the DB it
// will be a string so we'll need to convert it to a byt slice
byteHash := []byte(hashedPwd)
err := bcrypt.CompareHashAndPassword(byteHash, plainPwd)
if err != nil {
Logger.Error(err.Error())
return false
}
return true
}