pkg/types: add Compare function to password provider

pull/471/head
Michael Li 12 months ago
parent 8dc447dfde
commit 859da55cba
No known key found for this signature in database

@ -8,20 +8,25 @@ import (
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
) )
type PasswordGenerator interface { type PasswordProvider interface {
Generate([]byte) ([]byte, error) Generate(password []byte) ([]byte, error)
Compare(hashedPassword, password []byte) error
} }
func NewBcryptPasswordGenerator(cost int) PasswordGenerator { func NewBcryptPasswordProvider(cost int) PasswordProvider {
return &bcryptPasswordGenerator{ return &bcryptPasswordProvider{
cost: cost, cost: cost,
} }
} }
type bcryptPasswordGenerator struct { type bcryptPasswordProvider struct {
cost int cost int
} }
func (g *bcryptPasswordGenerator) Generate(password []byte) ([]byte, error) { func (p *bcryptPasswordProvider) Generate(password []byte) ([]byte, error) {
return bcrypt.GenerateFromPassword(password, g.cost) return bcrypt.GenerateFromPassword(password, p.cost)
}
func (p *bcryptPasswordProvider) Compare(hashedPassword, password []byte) error {
return bcrypt.CompareHashAndPassword(hashedPassword, password)
} }

Loading…
Cancel
Save