1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 08:32:40 +08:00

密码评估,密码加密

This commit is contained in:
2022-06-22 11:23:47 +08:00
parent 2fd5c8e662
commit dce7e8cb9f
3 changed files with 89 additions and 0 deletions

22
password/salt_password.go Normal file
View 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
}