-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.go
More file actions
85 lines (77 loc) · 1.85 KB
/
reader.go
File metadata and controls
85 lines (77 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package digest
import (
"errors"
"hash"
"io"
)
// Reader is used to calculate a digest using a [io.Reader].
type Reader struct {
r io.Reader
alg Algorithm
hash hash.Hash
}
// NewReader creates a [Reader].
// If the the reader is not provided, other requests to the returned reader will fail.
// If Algorithm is the zero value, the [Canonical] value will be used.
func NewReader(r io.Reader, alg Algorithm) Reader {
ret := Reader{
r: r,
alg: alg,
}
ai, _, err := algorithmInfoLookup(alg.name)
if err != nil {
ret.alg = Canonical
ai = aiCanonical
}
ret.hash = ai.newFn()
return ret
}
// Digest returns the current digest value.
func (r Reader) Digest() (Digest, error) {
if r.hash == nil {
return Digest{}, ErrReaderInvalid
}
return NewDigest(r.alg, r.hash)
}
// Hash returns the underlying [hash.Hash].
// Direct writes to this hash will affect the returned digest.
func (r Reader) Hash() hash.Hash {
return r.hash
}
// Read will pass through the read requests to the underlying reader.
// All read data is included in the digest computation.
func (r Reader) Read(p []byte) (int, error) {
if r.r == nil {
return 0, ErrReaderInvalid
}
n, err := r.r.Read(p)
if n <= 0 {
return n, err
}
_, hErr := r.hash.Write(p[:n])
if hErr != nil {
if err != nil {
err = errors.Join(err, hErr)
} else {
err = hErr
}
}
return n, err
}
// ReadAll reads everything from the underlying reader, computing the digest, and then discarding the read value.
func (r Reader) ReadAll() error {
if r.r == nil {
return ErrReaderInvalid
}
_, err := io.Copy(r.hash, r.r)
return err
}
// Verify returns true if the compared digest matches the current digest.
// Any errors in computing the digest will also return false.
func (r Reader) Verify(cmp Digest) bool {
d, err := r.Digest()
if err != nil {
return false
}
return !cmp.IsZero() && d.Equal(cmp)
}