详解 golang 中如何分装和PHP一样的hash加密方式
package main
import (
."fmt"
"golang.org/x/crypto/bcrypt"
)
func PasswordHash(password string) (string, error) {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
return string(bytes), err
}
func PasswordVerify(password, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}
func main() {
password := "123456"
hash, _ := PasswordHash(password)
Println("密码:", password)
Println("hash:", hash)
match := PasswordVerify(password, hash) //返回值为 true 或者 false
Println("验证:", match)
}
分享到此为止~
本文来自博客园,作者:Carver大脸猫,转载请注明原文链接:https://www.cnblogs.com/carver/articles/16633219.html

浙公网安备 33010602011771号