login feature

This commit is contained in:
Simon Ding
2024-07-12 10:06:26 +08:00
parent a172ca0361
commit 60c3c8822e
16 changed files with 751 additions and 16 deletions

View File

@@ -1,6 +1,10 @@
package utils
import "unicode"
import (
"unicode"
"golang.org/x/crypto/bcrypt"
)
func isASCII(s string) bool {
for _, c := range s {
@@ -11,3 +15,14 @@ func isASCII(s string) bool {
return true
}
// HashPassword generates a bcrypt hash for the given password.
func HashPassword(password string) (string, error) {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
return string(bytes), err
}
// VerifyPassword verifies if the given password matches the stored hash.
func VerifyPassword(password, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}