feat: add checksum verification library (SHA-256, SHA-1, MD5)#324
feat: add checksum verification library (SHA-256, SHA-1, MD5)#324mvanhorn wants to merge 5 commits intoSurgeDM:mainfrom
Conversation
Add VerifyChecksum() to compute file hashes and compare against expected values, and ParseDigestHeader() to extract checksums from HTTP Digest response headers (RFC 3230). Supports hex and base64 encoded hashes. This is the core library for download integrity verification. Wiring into the download lifecycle and CLI flags will follow in a separate PR. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
Fixed the hex fallback - it now validates hash length before accepting, preventing wrong-length hashes from silently passing through. Also added support for unpadded base64 (RawStdEncoding/RawURLEncoding) since some servers omit padding in Digest headers. Pushed in 0ec5370. |
Remove dead code in ParseDigestHeader (hex fallback already handled by the earlier hex check). Strengthen base64 test assertion with exact expected hash. Add MD5 and SHA-1 happy-path tests with algorithm normalization verification. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
Addressed the greptile findings in 62ac7bd - removed the redundant hex fallback in ParseDigestHeader, strengthened the base64 test assertion with the exact expected hash, and added MD5/SHA-1 happy-path tests. The unpadded base64 and algorithm normalization findings were already handled in the existing implementation. |
Address greptile P1 findings: - Validate decoded hash byte length matches expected algorithm size to prevent wrong-length hashes from being silently accepted - Add base64.RawStdEncoding and RawURLEncoding fallbacks for services that return unpadded base64 digests - Return error from ParseDigestHeader on length mismatches - Add test for unpadded base64 and wrong-length hex detection - Fix deferred file close to handle error Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
Went through the greptile findings:
|
|



Summary
Add a checksum verification library that computes file hashes and compares them against expected values. Also parses HTTP Digest response headers (RFC 3230) for server-provided checksums.
Why this matters
aria2 has
--checksum=sha-256=HASH. wget verifies checksums. When downloading Linux ISOs or software releases, users expect integrity verification. Surge currently downloads files with no integrity check.Changes
VerifyChecksum(filepath, algorithm, expected)supporting MD5, SHA-1, SHA-256ParseDigestHeader(header)to extract checksums from RFC 3230 HTTP Digest headers (base64 and hex)internal/processing/checksum.go--checksumflag will follow in a separate PR.Testing
This contribution was developed with AI assistance (Codex + Claude Code).
Greptile Summary
This PR adds
internal/processing/checksum.gowithVerifyChecksum(MD5/SHA-1/SHA-256 file hashing) andParseDigestHeader(RFC 3230 Digest header parsing), along with a comprehensive test suite. Previously raised concerns around unpadded base64 support, parameter shadowing, algorithm normalization inconsistency, and missing MD5/SHA-1 tests have all been addressed in this revision. The remaining findings are all P2: a dead error branch in the hex fast-path, an incomplete doc comment, and two untested code paths (URL-safe base64, SHA-1) inParseDigestHeader.Confidence Score: 5/5
Safe to merge; all remaining findings are P2 style/cleanup items that don't affect correctness.
All previously flagged blocking issues have been resolved. The three remaining comments are P2: dead code that doesn't affect behavior, an incomplete doc string, and two untested code paths. None of these cause wrong results or silent failures in the changed code.
No files require special attention beyond the minor cleanup notes.
Important Files Changed
Flowchart
%%{init: {'theme': 'neutral'}}%% flowchart TD A[ParseDigestHeader] --> B{SplitN on '='} B -- "< 2 parts" --> C[return '', '', nil] B -- "2 parts" --> D{Normalize algo} D -- "unsupported" --> C D -- "sha-256/sha-1/md5" --> E[Compute expectedBytes & expectedHexLen] E --> F{len == expectedHexLen?} F -- "yes" --> G{hex.DecodeString ok?} G -- "yes" --> H[return algo, hex value] G -- "no" --> I[Try base64 variants] F -- "no" --> I I --> J{Any variant decodes ok?} J -- "yes" --> K{correct length?} K -- "yes" --> H K -- "no" --> Q[return error: length mismatch] J -- "no" --> O[return '', '', nil] R[VerifyChecksum] --> S{empty args?} S -- "yes" --> T[return error] S -- "no" --> U{switch algo} U -- "unsupported" --> T U -- "md5/sha1/sha256" --> V[os.Open file] V -- "error" --> T V -- "ok" --> W[io.Copy to hash] W --> X[hex.EncodeToString] X --> Y[return ChecksumResult]Prompt To Fix All With AI
Reviews (5): Last reviewed commit: "fix: validate hash length and support un..." | Re-trigger Greptile
Context used: