github.com/Anderson-Lu/gobox@v0.0.0-20191127065433-3e6c4c2da420/crypto/crypto_helper.go (about)

     1  package crypto
     2  
     3  import (
     4  	"crypto/hmac"
     5  	"crypto/md5"
     6  	"crypto/sha256"
     7  	"encoding/hex"
     8  	"fmt"
     9  	"io"
    10  )
    11  
    12  //HMACSHA256加密算法
    13  func HmacSha256(data string, key string) string {
    14  	h := hmac.New(sha256.New, []byte(key))
    15  	io.WriteString(h, data)
    16  	return fmt.Sprintf("%x", h.Sum(nil))
    17  }
    18  
    19  //SHA256加密算法
    20  func Sha256(data string) string {
    21  	h := sha256.New()
    22  	h.Write([]byte(data))
    23  	return fmt.Sprintf("%x", h.Sum(nil))
    24  }
    25  
    26  //Md5加密
    27  func GetMD5Hex(content string) string {
    28  	h := md5.New()
    29  	h.Write([]byte(content))
    30  	hexstr := hex.EncodeToString(h.Sum(nil))
    31  	return hexstr
    32  }