forked from mlnoga/rct
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcrc.go
More file actions
44 lines (39 loc) · 713 Bytes
/
crc.go
File metadata and controls
44 lines (39 loc) · 713 Bytes
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
package rct
// Checksum for a RCT datagram
type CRC struct {
crc uint16
isOdd bool
}
// Returns a new CRC
func NewCRC() (c *CRC) {
return &CRC{
crc: 0xffff,
isOdd: false,
}
}
// Resets the CRC
func (c *CRC) Reset() {
c.crc = 0xffff
c.isOdd = false
}
// Updates CRC with the given byte
func (c *CRC) Update(b byte) {
crc := c.crc
for i := 0; i < 8; i++ {
bit := (b >> (7 - i) & 1) == 1
c15 := ((crc >> 15) & 1) == 1
crc <<= 1
if c15 != bit {
crc ^= 0x1021
}
}
c.crc = crc
c.isOdd = !c.isOdd
}
// Finalizes CRC, padding if required, and returns value
func (c *CRC) Get() uint16 {
if c.isOdd {
c.Update(0) // pad CRC stream (not byte stream) to even length
}
return c.crc
}