Add SHA256File to get the checksum of a file #20

Merged
sneak merged 1 commits from clawbot/util:proposal-sha256-file into master 2026-09-05 05:43:38 +02:00
Contributor

Adds SHA256File(path string) (string, error), which returns the SHA-256
digest of a file's contents as lowercase hexadecimal, in the same form the
sha256sum command prints.

This belongs here because checking a download against a published checksum,
noticing that a file has changed and comparing two files without reading both
into memory are all common, and the version written in a hurry reads the whole
file into a byte slice first, which is fine until someone points it at
something large.

Things to know:

  • The file is copied through the hash with io.Copy, so memory use does not
    depend on the size of the file. There is a test with contents larger than one
    read buffer.
  • The error from opening or reading is passed along unchanged rather than
    wrapped, so os.IsNotExist still recognises it. There is a test for that.
  • SHA-256 is right for integrity checking and for comparing files. It is not a
    password hash, and this does nothing to make it one.
  • The expected digests in the tests were taken from sha256sum rather than
    from this code, so the test would catch the function agreeing with itself
    while being wrong.
  • The test uses t.TempDir and os.WriteFile, which need Go 1.16, while
    go.mod still says go 1.14. Raising that line would make the file honest;
    it is left out of here so the ten proposal branches do not conflict over it.
  • The code is in a new file, sha256file.go, for the same reason.
  • make test on this branch reports one failure, TestNowUnixMicro. That test
    already fails on master and is unrelated to this change.

Model: opus-5

Adds `SHA256File(path string) (string, error)`, which returns the SHA-256 digest of a file's contents as lowercase hexadecimal, in the same form the `sha256sum` command prints. This belongs here because checking a download against a published checksum, noticing that a file has changed and comparing two files without reading both into memory are all common, and the version written in a hurry reads the whole file into a byte slice first, which is fine until someone points it at something large. Things to know: - The file is copied through the hash with `io.Copy`, so memory use does not depend on the size of the file. There is a test with contents larger than one read buffer. - The error from opening or reading is passed along unchanged rather than wrapped, so `os.IsNotExist` still recognises it. There is a test for that. - SHA-256 is right for integrity checking and for comparing files. It is not a password hash, and this does nothing to make it one. - The expected digests in the tests were taken from `sha256sum` rather than from this code, so the test would catch the function agreeing with itself while being wrong. - The test uses `t.TempDir` and `os.WriteFile`, which need Go 1.16, while `go.mod` still says `go 1.14`. Raising that line would make the file honest; it is left out of here so the ten proposal branches do not conflict over it. - The code is in a new file, `sha256file.go`, for the same reason. - `make test` on this branch reports one failure, `TestNowUnixMicro`. That test already fails on `master` and is unrelated to this change. Model: opus-5
clawbot self-assigned this 2026-09-05 05:32:02 +02:00
clawbot added 1 commit 2026-09-05 05:32:02 +02:00
SHA256File returns the SHA-256 digest of a file's contents as lowercase
hexadecimal, copying the file through the hash in chunks so its size does not
matter. Comes with a doc comment and table-driven tests. (closes #19)

Model: opus-5
sneak merged commit 96e904ffd1 into master 2026-09-05 05:43:38 +02:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: sneak/util#20