You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
21 lines
425 B
Go
21 lines
425 B
Go
3 years ago
|
package crypto
|
||
|
|
||
|
import (
|
||
|
"golang.org/x/crypto/bcrypt"
|
||
|
)
|
||
|
|
||
|
// EncryptPassword 加密密碼
|
||
|
func EncryptPassword(pwd string) (string, error) {
|
||
|
hash, err := bcrypt.GenerateFromPassword([]byte(pwd), bcrypt.DefaultCost)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
return string(hash), nil
|
||
|
}
|
||
|
|
||
|
// CheckPassword 檢查密碼
|
||
|
func CheckPassword(pwd, hash string) error {
|
||
|
return bcrypt.CompareHashAndPassword([]byte(hash), []byte(pwd))
|
||
|
}
|