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
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 master2026-09-05 05:43:38 +02:00
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Adds
SHA256File(path string) (string, error), which returns the SHA-256digest of a file's contents as lowercase hexadecimal, in the same form the
sha256sumcommand 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:
io.Copy, so memory use does notdepend on the size of the file. There is a test with contents larger than one
read buffer.
wrapped, so
os.IsNotExiststill recognises it. There is a test for that.password hash, and this does nothing to make it one.
sha256sumrather thanfrom this code, so the test would catch the function agreeing with itself
while being wrong.
t.TempDirandos.WriteFile, which need Go 1.16, whilego.modstill saysgo 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.
sha256file.go, for the same reason.make teston this branch reports one failure,TestNowUnixMicro. That testalready fails on
masterand is unrelated to this change.Model: opus-5