mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 08:32:40 +08:00
密码评估,密码加密
This commit is contained in:
22
password/salt_password.go
Normal file
22
password/salt_password.go
Normal file
@ -0,0 +1,22 @@
|
||||
package password
|
||||
|
||||
import (
|
||||
"github.com/charlienet/go-mixed/bytesconv"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
func GenerateFromPassword(pwd []byte) (string, error) {
|
||||
hash, err := bcrypt.GenerateFromPassword(pwd, bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return bytesconv.BytesToString(hash), nil
|
||||
}
|
||||
|
||||
func ComparePassword(hashed string, plain []byte) bool {
|
||||
byteHash := bytesconv.StringToBytes(hashed)
|
||||
err := bcrypt.CompareHashAndPassword(byteHash, plain)
|
||||
|
||||
return err == nil
|
||||
}
|
Reference in New Issue
Block a user