diff options
Diffstat (limited to 'player-server/internal/auth/auth.go')
| -rw-r--r-- | player-server/internal/auth/auth.go | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/player-server/internal/auth/auth.go b/player-server/internal/auth/auth.go new file mode 100644 index 0000000..55dea94 --- /dev/null +++ b/player-server/internal/auth/auth.go @@ -0,0 +1,41 @@ +// Package auth handles authentication and authorization. +package auth + +import ( + "fmt" + + "golang.org/x/crypto/bcrypt" +) + +// Hasher hashes passwords and compares plaintext against hashes. +type Hasher interface { + Hash(password string) (string, error) + Compare(hash, password string) error +} + +// BCryptHasher implements Hasher using bcrypt. +type BCryptHasher struct { + cost int +} + +// NewBCryptHasher creates a BCryptHasher with the given cost. +func NewBCryptHasher(cost int) *BCryptHasher { + return &BCryptHasher{cost: cost} +} + +// Hash returns a bcrypt hash of the password. +func (b *BCryptHasher) Hash(password string) (string, error) { + bytes, err := bcrypt.GenerateFromPassword([]byte(password), b.cost) + if err != nil { + return "", fmt.Errorf("hash password: %w", err) + } + return string(bytes), nil +} + +// Compare checks a password against a bcrypt hash. +func (b *BCryptHasher) Compare(hash, password string) error { + return bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) +} + +// Compile-time interface check. +var _ Hasher = (*BCryptHasher)(nil) |
