mirror of
https://github.com/makayabou/asg-server.git
synced 2026-05-02 17:43:36 +02:00
28 lines
564 B
Go
28 lines
564 B
Go
package crypto
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
var (
|
|
ErrPasswordInvalid = errors.New("invalid password")
|
|
)
|
|
|
|
func MakeBCryptHash(password string) (string, error) {
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
return "", fmt.Errorf("can't hash password: %w", err)
|
|
}
|
|
return string(hash), nil
|
|
}
|
|
|
|
func CompareBCryptHash(hash, password string) error {
|
|
if err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)); err != nil {
|
|
return ErrPasswordInvalid
|
|
}
|
|
return nil
|
|
}
|