常用的加密和密码验证
gopackage hash
import (
"fmt"
"golang.org/x/crypto/bcrypt"
)
func BcryptHash(password string) string {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
if err != nil {
fmt.Println("加密失败")
panic(err)
}
return string(bytes)
}
func BcryptCheck(password, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}
func BcryptIsHashed(str string) bool {
// bcrypt 加密后的长度等于 60
return len(str) == 60
}
本文作者:yowayimono
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!