mirror of
https://github.com/unti-io/go-utils.git
synced 2025-10-05 16:26:50 +08:00
28 lines
614 B
Go
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
|
|
}
|