30 lines
932 B
Go
30 lines
932 B
Go
package security
|
|
|
|
import "golang.org/x/crypto/bcrypt"
|
|
|
|
// PasswordService interface defines the contract for password operations
|
|
type PasswordService interface {
|
|
HashPassword(password string) (string, error)
|
|
CheckPasswordHash(password, hash string) bool
|
|
}
|
|
|
|
// PasswordServiceImpl implements PasswordService using bcrypt
|
|
type PasswordServiceImpl struct{}
|
|
|
|
// NewPasswordService creates a new password service
|
|
func NewPasswordService() PasswordService {
|
|
return &PasswordServiceImpl{}
|
|
}
|
|
|
|
// HashPassword hashes a password using bcrypt
|
|
func (p *PasswordServiceImpl) HashPassword(password string) (string, error) {
|
|
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
|
|
return string(bytes), err
|
|
}
|
|
|
|
// CheckPasswordHash compares a password with its hash
|
|
func (p *PasswordServiceImpl) CheckPasswordHash(password, hash string) bool {
|
|
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
|
return err == nil
|
|
}
|