| 12345678910111213141516171819202122232425262728 |
- // Package hash 哈希操作类
- package hash
- import (
- "github.com/runningwater/gohub/pkg/logger"
- "golang.org/x/crypto/bcrypt"
- )
- // BcryptHash bcrypt 哈希, 用于加密密码
- func BcryptHash(password string) string {
- bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
- logger.LogIf(err)
- return string(bytes)
- }
- // BcryptCheck 检查密码是否匹配
- func BcryptCheck(password, hash string) bool {
- err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
- return err == nil
- }
- // BcryptIsHashed 检查密码是否已经被哈希
- func BcryptIsHashed(str string) bool {
- return len(str) == 60
- }
|