-
Notifications
You must be signed in to change notification settings - Fork 16
/
hash.go
59 lines (48 loc) · 1.19 KB
/
hash.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
package crc16
import "hash"
// This file contains the CRC16 implementation of the
// go standard library hash.Hash interface
type Hash16 interface {
hash.Hash
Sum16() uint16
}
type digest struct {
sum uint16
t *Table
}
// Write adds more data to the running digest.
// It never returns an error.
func (h *digest) Write(data []byte) (int, error) {
h.sum = Update(h.sum, data, h.t)
return len(data), nil
}
// Sum appends the current digest (leftmost byte first, big-endian)
// to b and returns the resulting slice.
// It does not change the underlying digest state.
func (h digest) Sum(b []byte) []byte {
s := h.Sum16()
return append(b, byte(s>>8), byte(s))
}
// Reset resets the Hash to its initial state.
func (h *digest) Reset() {
h.sum = h.t.params.Init
}
// Size returns the number of bytes Sum will return.
func (h digest) Size() int {
return 2
}
// BlockSize returns the undelying block size.
// See digest.Hash.BlockSize
func (h digest) BlockSize() int {
return 1
}
// Sum16 returns the CRC16 checksum.
func (h digest) Sum16() uint16 {
return Complete(h.sum, h.t)
}
// New creates a new CRC16 digest for the given table.
func New(t *Table) Hash16 {
h := digest{t: t}
h.Reset()
return &h
}