github.com/germla/auth@v0.0.0-20230505173936-bc9a4159cf17/providers/password/encryptor/bcrypt_encryptor/bcrypt.go (about) 1 package bcrypt_encryptor 2 3 import "golang.org/x/crypto/bcrypt" 4 5 // Config BcryptEncryptor config 6 type Config struct { 7 Cost int 8 } 9 10 // BcryptEncryptor BcryptEncryptor struct 11 type BcryptEncryptor struct { 12 Config *Config 13 } 14 15 // New initalize BcryptEncryptor 16 func New(config *Config) *BcryptEncryptor { 17 if config == nil { 18 config = &Config{} 19 } 20 21 if config.Cost == 0 { 22 config.Cost = bcrypt.DefaultCost 23 } 24 25 return &BcryptEncryptor{ 26 Config: config, 27 } 28 } 29 30 // Digest generate encrypted password 31 func (bcryptEncryptor *BcryptEncryptor) Digest(password string) (string, error) { 32 hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) 33 return string(hashedPassword), err 34 } 35 36 // Compare check hashed password 37 func (bcryptEncryptor *BcryptEncryptor) Compare(hashedPassword string, password string) error { 38 return bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password)) 39 }