From 859da55cba65b107e47142694a2642de86dae062 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Fri, 2 Feb 2024 10:59:42 +0800 Subject: [PATCH] pkg/types: add Compare function to password provider --- pkg/types/password.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/pkg/types/password.go b/pkg/types/password.go index bb7505a5..1caf7aaf 100644 --- a/pkg/types/password.go +++ b/pkg/types/password.go @@ -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) }