-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder.go
30 lines (25 loc) · 863 Bytes
/
encoder.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
// Copyright 2021 Kévin José. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package easyapi
import (
"gitlab.com/kjose/jgmc/api/internal/easyapi/layer"
"golang.org/x/crypto/bcrypt"
)
// Strongly encode a password based on the resource ID
func EncodePassword(p layer.PasswordEncoderAware) error {
pwdWithSalt := p.GetIdentifier() + p.GetPlainPassword()
pwd, err := bcrypt.GenerateFromPassword([]byte(pwdWithSalt), 14)
if err != nil {
return err
}
p.SetEncodedPassword(string(pwd))
return nil
}
// Check the strong password
func CheckPassword(password string, p layer.PasswordEncoderAware) bool {
hash := p.GetEncodedPassword()
pwdWithSalt := p.GetIdentifier() + password
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(pwdWithSalt))
return err == nil
}