-
Notifications
You must be signed in to change notification settings - Fork 0
/
blake2b.go
40 lines (36 loc) · 878 Bytes
/
blake2b.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
package pwd
import (
"fmt"
"strings"
"github.com/palp1tate/go-crypto-guard"
"golang.org/x/crypto/blake2b"
)
func GenBlake2b(password string) (encryptedPassword string, err error) {
salt, err := pwd.GenerateSalt(16)
if err != nil {
return
}
h, err := blake2b.New256([]byte(salt))
if err != nil {
return
}
h.Write([]byte(password))
encryptedPassword = fmt.Sprintf("%s$%s$%s", pwd.Blake2b, salt, pwd.Encode2string(h.Sum(nil)))
return
}
func VerifyBlake2b(password, encryptedPassword string) (isValid bool, err error) {
parts := strings.Split(encryptedPassword, "$")
if len(parts) != 3 {
err = fmt.Errorf("invalid encrypted password format")
return
}
salt := parts[1]
storedHash := parts[2]
h, err := blake2b.New256([]byte(salt))
if err != nil {
return
}
h.Write([]byte(password))
isValid = storedHash == pwd.Encode2string(h.Sum(nil))
return
}