package knotserver import ( "crypto/hmac" "crypto/sha256" "encoding/hex" "net/http" ) func (h *Handle) VerifySignature(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { signature := r.Header.Get("X-Signature") if signature == "" || !h.verifyHMAC(signature, r) { writeError(w, "signature verification failed", http.StatusForbidden) return } next.ServeHTTP(w, r) }) } func (h *Handle) verifyHMAC(signature string, r *http.Request) bool { secret := h.c.Secret message := r.Method + r.URL.Path + r.URL.RawQuery mac := hmac.New(sha256.New, []byte(secret)) mac.Write([]byte(message)) expectedMAC := mac.Sum(nil) signatureBytes, err := hex.DecodeString(signature) if err != nil { return false } return hmac.Equal(signatureBytes, expectedMAC) }