Skip to content

Commit

Permalink
pkg/types: add Compare function to password provider
Browse files Browse the repository at this point in the history
  • Loading branch information
alimy committed Feb 2, 2024
1 parent 8dc447d commit 859da55
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions pkg/types/password.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@ import (
"golang.org/x/crypto/bcrypt"
)

type PasswordGenerator interface {
Generate([]byte) ([]byte, error)
type PasswordProvider interface {
Generate(password []byte) ([]byte, error)
Compare(hashedPassword, password []byte) error
}

func NewBcryptPasswordGenerator(cost int) PasswordGenerator {
return &bcryptPasswordGenerator{
func NewBcryptPasswordProvider(cost int) PasswordProvider {
return &bcryptPasswordProvider{
cost: cost,
}
}

type bcryptPasswordGenerator struct {
type bcryptPasswordProvider struct {
cost int
}

func (g *bcryptPasswordGenerator) Generate(password []byte) ([]byte, error) {
return bcrypt.GenerateFromPassword(password, g.cost)
func (p *bcryptPasswordProvider) Generate(password []byte) ([]byte, error) {
return bcrypt.GenerateFromPassword(password, p.cost)
}

func (p *bcryptPasswordProvider) Compare(hashedPassword, password []byte) error {
return bcrypt.CompareHashAndPassword(hashedPassword, password)
}

0 comments on commit 859da55

Please sign in to comment.