-
Notifications
You must be signed in to change notification settings - Fork 674
/
writer.go
60 lines (50 loc) · 1002 Bytes
/
writer.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
58
59
60
// _ _
// __ _____ __ ___ ___ __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
// \ V V / __/ (_| |\ V /| | (_| | || __/
// \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
// Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
// CONTACT: hello@weaviate.io
//
package rwhasher
import (
"hash"
"hash/crc32"
"io"
)
type WriterHasher interface {
io.Writer
N() int
Hash() []byte
Reset()
}
var _ WriterHasher = (*CRC32Writer)(nil)
type CRC32Writer struct {
w io.Writer
n int
hash hash.Hash32
}
func NewCRC32Writer(w io.Writer) *CRC32Writer {
return &CRC32Writer{
w: w,
hash: crc32.NewIEEE(),
}
}
func (wc *CRC32Writer) Write(p []byte) (n int, err error) {
n, err = wc.w.Write(p)
wc.n += n
wc.hash.Write(p[:n])
return n, err
}
func (wc *CRC32Writer) N() int {
return wc.n
}
func (wc *CRC32Writer) Hash() []byte {
return wc.hash.Sum(nil)
}
func (wc *CRC32Writer) Reset() {
wc.n = 0
wc.hash.Reset()
}