Skip to content

Commit 2589f58

Browse files
Style: Merge hMACSHA256() into calculateSignature()
1 parent add499e commit 2589f58

File tree

1 file changed

+5
-15
lines changed

1 file changed

+5
-15
lines changed

signature/signature.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,6 @@ func stringToTime(s string) (time.Time, error) {
5252
return time.Unix(sec, 0), nil
5353
}
5454

55-
// HMACSHA256 generates HMACS enconded hashes using the provided Key and SHA256
56-
// encoding for the message.
57-
func hMACSHA256(message, key []byte) ([]byte, error) {
58-
mac := hmac.New(sha256.New, []byte(key))
59-
if _, err := mac.Write(message); err != nil {
60-
return nil, err
61-
}
62-
return mac.Sum(nil), nil
63-
}
64-
6555
// Validator type represents a MessageBird signature validator.
6656
type Validator struct {
6757
SigningKey string // Signing Key provided by MessageBird.
@@ -94,7 +84,11 @@ func (v *Validator) calculateSignature(ts, qp string, b []byte) ([]byte, error)
9484
var m bytes.Buffer
9585
bh := sha256.Sum256(b)
9686
fmt.Fprintf(&m, "%s\n%s\n%s", ts, qp, bh[:])
97-
return hMACSHA256(m.Bytes(), []byte(v.SigningKey))
87+
mac := hmac.New(sha256.New, []byte(v.SigningKey))
88+
if _, err := mac.Write(m.Bytes()); err != nil {
89+
return nil, err
90+
}
91+
return mac.Sum(nil), nil
9892
}
9993

10094
// validSignature takes the timestamp, query params and body from the request,
@@ -117,8 +111,6 @@ func (v *Validator) validSignature(ts, rqp string, b []byte, rs string) bool {
117111

118112
// ValidRequest is a method that takes care of the signature validation of
119113
// incoming requests.
120-
// To use just pass the request:
121-
// signature.Validate(request)
122114
func (v *Validator) ValidRequest(r *http.Request) error {
123115
ts := r.Header.Get(tsHeader)
124116
rs := r.Header.Get(sHeader)
@@ -136,8 +128,6 @@ func (v *Validator) ValidRequest(r *http.Request) error {
136128
// Validate is a handler wrapper that takes care of the signature validation of
137129
// incoming requests and rejects them if invalid or pass them on to your handler
138130
// otherwise.
139-
// To use just wrap your handler with it:
140-
// http.Handle("/path", signature.Validate(handleThing))
141131
func (v *Validator) Validate(h http.Handler) http.Handler {
142132
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
143133
if err := v.ValidRequest(r); err != nil {

0 commit comments

Comments
 (0)