-
Notifications
You must be signed in to change notification settings - Fork 0
/
bcrypt.go
39 lines (31 loc) · 915 Bytes
/
bcrypt.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
package bcrypt_encryptor
import "golang.org/x/crypto/bcrypt"
// Config BcryptEncryptor config
type Config struct {
Cost int
}
// BcryptEncryptor BcryptEncryptor struct
type BcryptEncryptor struct {
Config *Config
}
// New initalize BcryptEncryptor
func New(config *Config) *BcryptEncryptor {
if config == nil {
config = &Config{}
}
if config.Cost == 0 {
config.Cost = bcrypt.DefaultCost
}
return &BcryptEncryptor{
Config: config,
}
}
// Digest generate encrypted password
func (bcryptEncryptor *BcryptEncryptor) Digest(password string) (string, error) {
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
return string(hashedPassword), err
}
// Compare check hashed password
func (bcryptEncryptor *BcryptEncryptor) Compare(hashedPassword string, password string) error {
return bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
}