tulip/utils/password.go

18 lines
445 B
Go
Raw Normal View History

2024-01-29 10:44:45 +00:00
package utils
2023-09-06 22:20:09 +01:00
2023-09-29 16:37:23 +01:00
import (
"golang.org/x/crypto/bcrypt"
)
2023-09-06 22:20:09 +01:00
2023-09-29 16:37:23 +01:00
// HashString is used to represent a string containing a password hash
type HashString string
func HashPassword(password string) (HashString, error) {
2023-09-06 22:20:09 +01:00
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
2023-09-29 16:37:23 +01:00
return HashString(bytes), err
2023-09-06 22:20:09 +01:00
}
2023-09-29 16:37:23 +01:00
func CheckPasswordHash(hash HashString, password string) error {
2023-09-06 22:20:09 +01:00
return bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
}