Files
go-utils/utils/password.go
2023-08-26 17:36:50 +08:00

28 lines
614 B
Go

package utils
import (
"github.com/spf13/cast"
"golang.org/x/crypto/bcrypt"
)
// Password - 密码
var Password *PasswordStruct
type PasswordStruct struct{}
// Create - 创建密码
func (this *PasswordStruct) Create(password any) (result string) {
item, _ := bcrypt.GenerateFromPassword([]byte(password.(string)), bcrypt.MinCost)
return string(item)
}
// Verify - 验证密码
func (this *PasswordStruct) Verify(encode any, password any) (ok bool) {
if err := bcrypt.CompareHashAndPassword([]byte(cast.ToString(encode)), []byte(cast.ToString(password))); err != nil {
return false
}
return true
}