init
This commit is contained in:
commit
b15b95781c
108 changed files with 14802 additions and 0 deletions
35
internal/auth/password.go
Normal file
35
internal/auth/password.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package auth
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
const bcryptCost = 12
|
||||
|
||||
func HashPassword(password string) (string, error) {
|
||||
if strings.TrimSpace(password) == "" {
|
||||
return "", fmt.Errorf("password is required")
|
||||
}
|
||||
|
||||
if len(password) > 72 {
|
||||
return "", fmt.Errorf("password must be 72 bytes or fewer")
|
||||
}
|
||||
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcryptCost)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("hash password: %w", err)
|
||||
}
|
||||
|
||||
return string(hashedPassword), nil
|
||||
}
|
||||
|
||||
func ComparePassword(hash, password string) error {
|
||||
if hash == "" {
|
||||
return fmt.Errorf("password hash is required")
|
||||
}
|
||||
|
||||
return bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue