123456789101112131415161718192021222324252627282930313233343536373839 |
- package hmac
- import (
- baseHmac "crypto/hmac"
- "crypto/sha256"
- "encoding/base64"
- "encoding/hex"
- )
- var _ HMAC = (*hmac)(nil)
- type HMAC interface {
- i()
- Sha256ToString(data string) string
- Sha256ToBase64String(data string) string
- }
- type hmac struct {
- secret string
- }
- func New(secret string) HMAC {
- return &hmac{secret: secret}
- }
- func (m *hmac) i() {}
- func (m *hmac) Sha256ToString(data string) string {
- h := baseHmac.New(sha256.New, []byte(m.secret))
- h.Write([]byte(data))
- return hex.EncodeToString(h.Sum(nil))
- }
- func (m *hmac) Sha256ToBase64String(data string) string {
- h := baseHmac.New(sha256.New, []byte(m.secret))
- h.Write([]byte(data))
- return base64.StdEncoding.EncodeToString(h.Sum(nil))
- }
|