add some utils

This commit is contained in:
yanghao05
2023-01-30 16:57:48 +08:00
parent 16f80cc297
commit 27bb527373
10 changed files with 384 additions and 6 deletions

17
utils/hash/bcrypt.go Normal file
View File

@@ -0,0 +1,17 @@
package hash
import (
"golang.org/x/crypto/bcrypt"
)
// BcryptHash 使用 bcrypt 对密码进行加密
func BcryptHash(password string) string {
bytes, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
return string(bytes)
}
// BcryptCheck 对比明文密码和数据库的哈希值
func BcryptCheck(password, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}