(msgStr, sigStr, secretStr, alg string)
| 320 | } |
| 321 | |
| 322 | func (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 | |
| 365 | func (l *hmacLib) parseSignaturePrefix(sig string) (string, string) { |
| 366 | sig = strings.TrimSpace(sig) |
no test coverage detected