-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
hash_io.go
52 lines (43 loc) · 970 Bytes
/
hash_io.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
package s3crypto
import (
"crypto/sha256"
"hash"
"io"
)
// lengthReader returns the content length
type lengthReader interface {
GetContentLength() int64
}
type contentLengthReader struct {
contentLength int64
body io.Reader
}
func newContentLengthReader(f io.Reader) *contentLengthReader {
return &contentLengthReader{body: f}
}
func (r *contentLengthReader) Read(b []byte) (int, error) {
n, err := r.body.Read(b)
if err != nil && err != io.EOF {
return n, err
}
r.contentLength += int64(n)
return n, err
}
func (r *contentLengthReader) GetContentLength() int64 {
return r.contentLength
}
type sha256Writer struct {
sha256 []byte
hash hash.Hash
out io.Writer
}
func newSHA256Writer(f io.Writer) *sha256Writer {
return &sha256Writer{hash: sha256.New(), out: f}
}
func (r *sha256Writer) Write(b []byte) (int, error) {
r.hash.Write(b)
return r.out.Write(b)
}
func (r *sha256Writer) GetValue() []byte {
return r.hash.Sum(nil)
}