github.com/lingyao2333/mo-zero@v1.4.1/core/codec/hmac.go (about)

     1  package codec
     2  
     3  import (
     4  	"crypto/hmac"
     5  	"crypto/sha256"
     6  	"encoding/base64"
     7  	"io"
     8  )
     9  
    10  // Hmac returns HMAC bytes for body with the given key.
    11  func Hmac(key []byte, body string) []byte {
    12  	h := hmac.New(sha256.New, key)
    13  	io.WriteString(h, body)
    14  	return h.Sum(nil)
    15  }
    16  
    17  // HmacBase64 returns the base64 encoded string of HMAC for body with the given key.
    18  func HmacBase64(key []byte, body string) string {
    19  	return base64.StdEncoding.EncodeToString(Hmac(key, body))
    20  }