Skip to content

Commit f9b482e

Browse files
feat: use hmac for computing checksum
1 parent b05c7f3 commit f9b482e

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

shieldcsrf/token.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package shieldcsrf
22

33
import (
4+
"crypto/hmac"
45
"crypto/sha256"
56
"crypto/subtle"
67
"encoding/base64"
@@ -10,6 +11,7 @@ import (
1011
"net/http"
1112
"strings"
1213

14+
"go.inout.gg/foundations/must"
1315
"go.inout.gg/shield/internal/random"
1416
)
1517

@@ -165,8 +167,9 @@ func decodeCookieValue(val string) (string, string, error) {
165167
return parts[0], parts[1], nil
166168
}
167169

168-
// computeChecksum return the sha256 checksum of the given value and secret.
170+
// computeChecksum return the sha256 checksum of the given value with secret.
169171
func computeChecksum(val, secret string) string {
170-
cs := sha256.Sum256([]byte(fmt.Sprintf("%s%s", val, secret)))
171-
return hex.EncodeToString(cs[:])
172+
h := hmac.New(sha256.New, []byte(secret))
173+
_ = must.Must(h.Write([]byte(val)))
174+
return hex.EncodeToString(h.Sum(nil))
172175
}

0 commit comments

Comments
 (0)