-
Notifications
You must be signed in to change notification settings - Fork 351
/
sha_256_reader.go
57 lines (48 loc) · 1022 Bytes
/
sha_256_reader.go
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
package sig
import (
"crypto/sha256"
"encoding/hex"
"hash"
"io"
"github.com/treeverse/lakefs/pkg/gateway/errors"
)
type Sha256Reader struct {
src io.ReadCloser
expectedHash []byte
hash hash.Hash
}
func NewSha265Reader(src io.ReadCloser, sha256Hex string) (io.ReadCloser, error) {
expectedHash, err := hex.DecodeString(sha256Hex)
if err != nil {
return nil, err
}
Sha256hash := sha256.New()
return &Sha256Reader{
expectedHash: expectedHash,
src: src,
hash: Sha256hash,
}, nil
}
func (r *Sha256Reader) Read(p []byte) (int, error) {
n, err := r.src.Read(p)
if n > 0 {
if _, err := r.hash.Write(p[:n]); err != nil {
return n, err
}
}
if err == io.EOF {
if err := r.Verify(); err != nil {
return n, err
}
}
return n, err
}
func (r *Sha256Reader) Verify() error {
if sum := r.hash.Sum(nil); !Equal(r.expectedHash, sum) {
return errors.ErrSignatureDoesNotMatch
}
return nil
}
func (r *Sha256Reader) Close() error {
return r.src.Close()
}