You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add Hash method to Reader for computing archive checksums (#9)
* Add Hash method to Reader for computing archive checksums from buffered raw bytes
* Make OpenBytes zero-copy by dispatching directly on the caller's slice
* Add OpenBytesWithPrefix for zero-copy prefix-stripped opens
Copy file name to clipboardExpand all lines: README.md
+20Lines changed: 20 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -46,6 +46,26 @@ func main() {
46
46
}
47
47
```
48
48
49
+
### Hashing
50
+
51
+
`Open` buffers the raw archive bytes in memory, so the reader can compute checksums of the original artifact without re-reading from the source. This is useful for verifying a downloaded package against the digest published by its registry.
52
+
53
+
```go
54
+
reader, _:= archives.Open("rails-7.1.0.gem", f)
55
+
defer reader.Close()
56
+
57
+
sha, _:= reader.Hash(archives.SHA256)
58
+
fmt.Println(sha) // hex-encoded sha256 of the .gem file
59
+
60
+
// also available: archives.SHA512, archives.SHA1, archives.MD5
61
+
```
62
+
63
+
The hash is computed over the archive as it was passed to `Open`, not the decompressed contents. For nested formats like gems this means the outer `.gem` file, which is what rubygems.org publishes. If you already have the bytes in hand, `OpenBytes` skips the extra read:
64
+
65
+
```go
66
+
reader, _:= archives.OpenBytes("pkg.tgz", data)
67
+
```
68
+
49
69
### Prefix stripping
50
70
51
71
Some package formats wrap content in a directory (npm uses `package/`). `OpenWithPrefix` strips a path prefix from all entries:
0 commit comments