package utils

import (
	"crypto/md5"
	"encoding/hex"
	"github.com/kataras/golog"
	"golang.org/x/crypto/bcrypt"
)

// Md5 returns the MD5 checksum string of the data.
func Md5(b []byte) string {
	checksum := md5.Sum(b)
	return hex.EncodeToString(checksum[:])
}

//加密
func  Encrypt(password string) string {
	if password == "" {
		return ""
	}
	if hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost); err != nil {
		return ""
	} else {
		password = string(hash)
		return password
	}
}

func CompareHashAndPassword(e string, p string) (bool, error) {
	err := bcrypt.CompareHashAndPassword([]byte(e), []byte(p))
	if err != nil {
		golog.Println(err.Error())
		return false, err
	}
	return true, nil
}