MCPcopy Create free account
hub / github.com/cel-expr/cel-go / verifyString

Method verifyString

ext/security/hmac/hmac.go:322–363  ·  view source on GitHub ↗
(msgStr, sigStr, secretStr, alg string)

Source from the content-addressed store, hash-verified

320}
321
322func (l *hmacLib) verifyString(msgStr, sigStr, secretStr, alg string) bool {
323 sigStr = strings.TrimSpace(sigStr)
324 detectedAlg, cleanSig := l.parseSignaturePrefix(sigStr)
325 effectiveAlg := alg
326 if detectedAlg != "" {
327 effectiveAlg = detectedAlg
328 }
329
330 hType, err := l.resolveHash(effectiveAlg)
331 if err != nil {
332 return false
333 }
334
335 expectedMAC, err := computeHMAC([]byte(msgStr), []byte(secretStr), hType)
336 if err != nil {
337 return false
338 }
339
340 // Try hex decoding
341 if hexBytes, err := hex.DecodeString(cleanSig); err == nil && len(hexBytes) == len(expectedMAC) {
342 if hmac.Equal(expectedMAC, hexBytes) {
343 return true
344 }
345 }
346
347 // Try base64 standard decoding
348 if b64Bytes, err := decodeBase64StdSegment(cleanSig); err == nil && len(b64Bytes) == len(expectedMAC) {
349 if hmac.Equal(expectedMAC, b64Bytes) {
350 return true
351 }
352 }
353
354 // Try base64 URL decoding
355 if b64URLBytes, err := decodeBase64URLSegment(cleanSig); err == nil && len(b64URLBytes) == len(expectedMAC) {
356 if hmac.Equal(expectedMAC, b64URLBytes) {
357 return true
358 }
359 }
360
361 // Fallback raw string comparison
362 return hmac.Equal(expectedMAC, []byte(cleanSig))
363}
364
365func (l *hmacLib) parseSignaturePrefix(sig string) (string, string) {
366 sig = strings.TrimSpace(sig)

Callers 1

CompileOptionsMethod · 0.95

Calls 6

parseSignaturePrefixMethod · 0.95
resolveHashMethod · 0.95
computeHMACFunction · 0.85
decodeBase64StdSegmentFunction · 0.85
decodeBase64URLSegmentFunction · 0.85
EqualMethod · 0.65

Tested by

no test coverage detected