hmac.go 318 B

123456789101112131415161718
  1. package codec
  2. import (
  3. "crypto/hmac"
  4. "crypto/sha256"
  5. "encoding/base64"
  6. "io"
  7. )
  8. func Hmac(key []byte, body string) []byte {
  9. h := hmac.New(sha256.New, key)
  10. io.WriteString(h, body)
  11. return h.Sum(nil)
  12. }
  13. func HmacBase64(key []byte, body string) string {
  14. return base64.StdEncoding.EncodeToString(Hmac(key, body))
  15. }