tank/code/tool/util/util_encode.go
2019-05-06 02:18:08 +08:00

31 lines
526 B
Go

package util
import (
"crypto/md5"
"fmt"
"golang.org/x/crypto/bcrypt"
)
//md5
func GetMd5(raw string) string {
return fmt.Sprintf("%x", md5.Sum([]byte(raw)))
}
func GetBcrypt(raw string) string {
password := []byte(raw)
hashedPassword, err := bcrypt.GenerateFromPassword(password, bcrypt.DefaultCost)
if err != nil {
panic(err)
}
return string(hashedPassword)
}
func MatchBcrypt(raw string, bcryptStr string) bool {
err := bcrypt.CompareHashAndPassword([]byte(bcryptStr), []byte(raw))
return err == nil
}