Source: https://www.programmersought.com/article/53551785934/
[code]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)
fmt.Println("password:", password)
fmt.Println("hash:", hash)
match := PasswordVerify(password, hash)
fmt.Println("Verify:", match)
}[/code]
|