digest-crc 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/.specopts +1 -0
- data/.yardopts +1 -0
- data/ChangeLog.md +18 -0
- data/LICENSE.txt +22 -0
- data/README.md +73 -0
- data/Rakefile +39 -0
- data/VERSION +1 -0
- data/digest-crc.gemspec +101 -0
- data/lib/digest/crc.rb +116 -0
- data/lib/digest/crc1.rb +35 -0
- data/lib/digest/crc16.rb +79 -0
- data/lib/digest/crc16_ccitt.rb +59 -0
- data/lib/digest/crc16_dnp.rb +62 -0
- data/lib/digest/crc16_modbus.rb +59 -0
- data/lib/digest/crc16_usb.rb +11 -0
- data/lib/digest/crc16_xmodem.rb +57 -0
- data/lib/digest/crc16_zmodem.rb +57 -0
- data/lib/digest/crc24.rb +80 -0
- data/lib/digest/crc32.rb +115 -0
- data/lib/digest/crc32_mpeg.rb +90 -0
- data/lib/digest/crc5.rb +49 -0
- data/lib/digest/crc64.rb +119 -0
- data/lib/digest/crc8.rb +58 -0
- data/spec/crc16_ccitt_spec.rb +13 -0
- data/spec/crc16_modbus_spec.rb +13 -0
- data/spec/crc16_spec.rb +13 -0
- data/spec/crc16_usb_spec.rb +13 -0
- data/spec/crc16_xmodem_spec.rb +13 -0
- data/spec/crc16_zmodem_spec.rb +13 -0
- data/spec/crc1_spec.rb +13 -0
- data/spec/crc24_spec.rb +13 -0
- data/spec/crc32_mpeg_spec.rb +13 -0
- data/spec/crc32_spec.rb +13 -0
- data/spec/crc5_spec.rb +14 -0
- data/spec/crc64_spec.rb +13 -0
- data/spec/crc8_spec.rb +13 -0
- data/spec/crc_examples.rb +27 -0
- data/spec/crc_spec.rb +14 -0
- data/spec/spec_helper.rb +4 -0
- metadata +138 -0
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'digest/crc32'
|
2
|
+
|
3
|
+
module Digest
|
4
|
+
class CRC32Mpeg < CRC32
|
5
|
+
|
6
|
+
XOR_MASK = 0x00000000
|
7
|
+
|
8
|
+
TABLE = [
|
9
|
+
0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9,
|
10
|
+
0x130476dc, 0x17c56b6b, 0x1a864db2, 0x1e475005,
|
11
|
+
0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61,
|
12
|
+
0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd,
|
13
|
+
0x4c11db70, 0x48d0c6c7, 0x4593e01e, 0x4152fda9,
|
14
|
+
0x5f15adac, 0x5bd4b01b, 0x569796c2, 0x52568b75,
|
15
|
+
0x6a1936c8, 0x6ed82b7f, 0x639b0da6, 0x675a1011,
|
16
|
+
0x791d4014, 0x7ddc5da3, 0x709f7b7a, 0x745e66cd,
|
17
|
+
0x9823b6e0, 0x9ce2ab57, 0x91a18d8e, 0x95609039,
|
18
|
+
0x8b27c03c, 0x8fe6dd8b, 0x82a5fb52, 0x8664e6e5,
|
19
|
+
0xbe2b5b58, 0xbaea46ef, 0xb7a96036, 0xb3687d81,
|
20
|
+
0xad2f2d84, 0xa9ee3033, 0xa4ad16ea, 0xa06c0b5d,
|
21
|
+
0xd4326d90, 0xd0f37027, 0xddb056fe, 0xd9714b49,
|
22
|
+
0xc7361b4c, 0xc3f706fb, 0xceb42022, 0xca753d95,
|
23
|
+
0xf23a8028, 0xf6fb9d9f, 0xfbb8bb46, 0xff79a6f1,
|
24
|
+
0xe13ef6f4, 0xe5ffeb43, 0xe8bccd9a, 0xec7dd02d,
|
25
|
+
0x34867077, 0x30476dc0, 0x3d044b19, 0x39c556ae,
|
26
|
+
0x278206ab, 0x23431b1c, 0x2e003dc5, 0x2ac12072,
|
27
|
+
0x128e9dcf, 0x164f8078, 0x1b0ca6a1, 0x1fcdbb16,
|
28
|
+
0x018aeb13, 0x054bf6a4, 0x0808d07d, 0x0cc9cdca,
|
29
|
+
0x7897ab07, 0x7c56b6b0, 0x71159069, 0x75d48dde,
|
30
|
+
0x6b93dddb, 0x6f52c06c, 0x6211e6b5, 0x66d0fb02,
|
31
|
+
0x5e9f46bf, 0x5a5e5b08, 0x571d7dd1, 0x53dc6066,
|
32
|
+
0x4d9b3063, 0x495a2dd4, 0x44190b0d, 0x40d816ba,
|
33
|
+
0xaca5c697, 0xa864db20, 0xa527fdf9, 0xa1e6e04e,
|
34
|
+
0xbfa1b04b, 0xbb60adfc, 0xb6238b25, 0xb2e29692,
|
35
|
+
0x8aad2b2f, 0x8e6c3698, 0x832f1041, 0x87ee0df6,
|
36
|
+
0x99a95df3, 0x9d684044, 0x902b669d, 0x94ea7b2a,
|
37
|
+
0xe0b41de7, 0xe4750050, 0xe9362689, 0xedf73b3e,
|
38
|
+
0xf3b06b3b, 0xf771768c, 0xfa325055, 0xfef34de2,
|
39
|
+
0xc6bcf05f, 0xc27dede8, 0xcf3ecb31, 0xcbffd686,
|
40
|
+
0xd5b88683, 0xd1799b34, 0xdc3abded, 0xd8fba05a,
|
41
|
+
0x690ce0ee, 0x6dcdfd59, 0x608edb80, 0x644fc637,
|
42
|
+
0x7a089632, 0x7ec98b85, 0x738aad5c, 0x774bb0eb,
|
43
|
+
0x4f040d56, 0x4bc510e1, 0x46863638, 0x42472b8f,
|
44
|
+
0x5c007b8a, 0x58c1663d, 0x558240e4, 0x51435d53,
|
45
|
+
0x251d3b9e, 0x21dc2629, 0x2c9f00f0, 0x285e1d47,
|
46
|
+
0x36194d42, 0x32d850f5, 0x3f9b762c, 0x3b5a6b9b,
|
47
|
+
0x0315d626, 0x07d4cb91, 0x0a97ed48, 0x0e56f0ff,
|
48
|
+
0x1011a0fa, 0x14d0bd4d, 0x19939b94, 0x1d528623,
|
49
|
+
0xf12f560e, 0xf5ee4bb9, 0xf8ad6d60, 0xfc6c70d7,
|
50
|
+
0xe22b20d2, 0xe6ea3d65, 0xeba91bbc, 0xef68060b,
|
51
|
+
0xd727bbb6, 0xd3e6a601, 0xdea580d8, 0xda649d6f,
|
52
|
+
0xc423cd6a, 0xc0e2d0dd, 0xcda1f604, 0xc960ebb3,
|
53
|
+
0xbd3e8d7e, 0xb9ff90c9, 0xb4bcb610, 0xb07daba7,
|
54
|
+
0xae3afba2, 0xaafbe615, 0xa7b8c0cc, 0xa379dd7b,
|
55
|
+
0x9b3660c6, 0x9ff77d71, 0x92b45ba8, 0x9675461f,
|
56
|
+
0x8832161a, 0x8cf30bad, 0x81b02d74, 0x857130c3,
|
57
|
+
0x5d8a9099, 0x594b8d2e, 0x5408abf7, 0x50c9b640,
|
58
|
+
0x4e8ee645, 0x4a4ffbf2, 0x470cdd2b, 0x43cdc09c,
|
59
|
+
0x7b827d21, 0x7f436096, 0x7200464f, 0x76c15bf8,
|
60
|
+
0x68860bfd, 0x6c47164a, 0x61043093, 0x65c52d24,
|
61
|
+
0x119b4be9, 0x155a565e, 0x18197087, 0x1cd86d30,
|
62
|
+
0x029f3d35, 0x065e2082, 0x0b1d065b, 0x0fdc1bec,
|
63
|
+
0x3793a651, 0x3352bbe6, 0x3e119d3f, 0x3ad08088,
|
64
|
+
0x2497d08d, 0x2056cd3a, 0x2d15ebe3, 0x29d4f654,
|
65
|
+
0xc5a92679, 0xc1683bce, 0xcc2b1d17, 0xc8ea00a0,
|
66
|
+
0xd6ad50a5, 0xd26c4d12, 0xdf2f6bcb, 0xdbee767c,
|
67
|
+
0xe3a1cbc1, 0xe760d676, 0xea23f0af, 0xeee2ed18,
|
68
|
+
0xf0a5bd1d, 0xf464a0aa, 0xf9278673, 0xfde69bc4,
|
69
|
+
0x89b8fd09, 0x8d79e0be, 0x803ac667, 0x84fbdbd0,
|
70
|
+
0x9abc8bd5, 0x9e7d9662, 0x933eb0bb, 0x97ffad0c,
|
71
|
+
0xafb010b1, 0xab710d06, 0xa6322bdf, 0xa2f33668,
|
72
|
+
0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4
|
73
|
+
]
|
74
|
+
|
75
|
+
#
|
76
|
+
# Updates the CRC32 Mpeg checksum.
|
77
|
+
#
|
78
|
+
# @param [String] data
|
79
|
+
# The data to update the checksum with.
|
80
|
+
#
|
81
|
+
def update(data)
|
82
|
+
data.each_byte do |b|
|
83
|
+
@crc = ((TABLE[((@crc >> 24) ^ b) & 0xff] ^ (@crc << 8)) & 0xffffffff)
|
84
|
+
end
|
85
|
+
|
86
|
+
return self
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
end
|
data/lib/digest/crc5.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'digest/crc'
|
2
|
+
|
3
|
+
module Digest
|
4
|
+
class CRC5 < CRC
|
5
|
+
|
6
|
+
WIDTH = 5
|
7
|
+
|
8
|
+
INIT_CRC = 0x1f
|
9
|
+
|
10
|
+
XOR_MASK = 0x1f
|
11
|
+
|
12
|
+
CRC_MASK = (0x1f << 3)
|
13
|
+
|
14
|
+
# Generated by `./pycrc.py --algorithm=table-driven --model=crc-5 --generate=c`
|
15
|
+
TABLE = [
|
16
|
+
0x00, 0x70, 0xe0, 0x90, 0x88, 0xf8, 0x68, 0x18, 0x58, 0x28, 0xb8, 0xc8, 0xd0, 0xa0, 0x30, 0x40,
|
17
|
+
0xb0, 0xc0, 0x50, 0x20, 0x38, 0x48, 0xd8, 0xa8, 0xe8, 0x98, 0x08, 0x78, 0x60, 0x10, 0x80, 0xf0,
|
18
|
+
0x28, 0x58, 0xc8, 0xb8, 0xa0, 0xd0, 0x40, 0x30, 0x70, 0x00, 0x90, 0xe0, 0xf8, 0x88, 0x18, 0x68,
|
19
|
+
0x98, 0xe8, 0x78, 0x08, 0x10, 0x60, 0xf0, 0x80, 0xc0, 0xb0, 0x20, 0x50, 0x48, 0x38, 0xa8, 0xd8,
|
20
|
+
0x50, 0x20, 0xb0, 0xc0, 0xd8, 0xa8, 0x38, 0x48, 0x08, 0x78, 0xe8, 0x98, 0x80, 0xf0, 0x60, 0x10,
|
21
|
+
0xe0, 0x90, 0x00, 0x70, 0x68, 0x18, 0x88, 0xf8, 0xb8, 0xc8, 0x58, 0x28, 0x30, 0x40, 0xd0, 0xa0,
|
22
|
+
0x78, 0x08, 0x98, 0xe8, 0xf0, 0x80, 0x10, 0x60, 0x20, 0x50, 0xc0, 0xb0, 0xa8, 0xd8, 0x48, 0x38,
|
23
|
+
0xc8, 0xb8, 0x28, 0x58, 0x40, 0x30, 0xa0, 0xd0, 0x90, 0xe0, 0x70, 0x00, 0x18, 0x68, 0xf8, 0x88,
|
24
|
+
0xa0, 0xd0, 0x40, 0x30, 0x28, 0x58, 0xc8, 0xb8, 0xf8, 0x88, 0x18, 0x68, 0x70, 0x00, 0x90, 0xe0,
|
25
|
+
0x10, 0x60, 0xf0, 0x80, 0x98, 0xe8, 0x78, 0x08, 0x48, 0x38, 0xa8, 0xd8, 0xc0, 0xb0, 0x20, 0x50,
|
26
|
+
0x88, 0xf8, 0x68, 0x18, 0x00, 0x70, 0xe0, 0x90, 0xd0, 0xa0, 0x30, 0x40, 0x58, 0x28, 0xb8, 0xc8,
|
27
|
+
0x38, 0x48, 0xd8, 0xa8, 0xb0, 0xc0, 0x50, 0x20, 0x60, 0x10, 0x80, 0xf0, 0xe8, 0x98, 0x08, 0x78,
|
28
|
+
0xf0, 0x80, 0x10, 0x60, 0x78, 0x08, 0x98, 0xe8, 0xa8, 0xd8, 0x48, 0x38, 0x20, 0x50, 0xc0, 0xb0,
|
29
|
+
0x40, 0x30, 0xa0, 0xd0, 0xc8, 0xb8, 0x28, 0x58, 0x18, 0x68, 0xf8, 0x88, 0x90, 0xe0, 0x70, 0x00,
|
30
|
+
0xd8, 0xa8, 0x38, 0x48, 0x50, 0x20, 0xb0, 0xc0, 0x80, 0xf0, 0x60, 0x10, 0x08, 0x78, 0xe8, 0x98,
|
31
|
+
0x68, 0x18, 0x88, 0xf8, 0xe0, 0x90, 0x00, 0x70, 0x30, 0x40, 0xd0, 0xa0, 0xb8, 0xc8, 0x58, 0x28
|
32
|
+
]
|
33
|
+
|
34
|
+
#
|
35
|
+
# Updates the CRC5 checksum.
|
36
|
+
#
|
37
|
+
# @param [String] data
|
38
|
+
# The data to update the checksum with.
|
39
|
+
#
|
40
|
+
def update(data)
|
41
|
+
data.each_byte do |b|
|
42
|
+
@crc = ((TABLE[((@crc >> 3) ^ b) & 0xff] ^ (@crc >> 8)) & CRC_MASK)
|
43
|
+
end
|
44
|
+
|
45
|
+
return self
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
data/lib/digest/crc64.rb
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
require 'digest/crc'
|
2
|
+
|
3
|
+
module Digest
|
4
|
+
class CRC64 < CRC
|
5
|
+
|
6
|
+
WIDTH = 64
|
7
|
+
|
8
|
+
INIT_CRC = 0x0000000000000000
|
9
|
+
|
10
|
+
XOR_MASK = 0x0000000000000000
|
11
|
+
|
12
|
+
# Generated by `./pycrc.py --algorithm=table-driven --model=crc-64 --generate=c`
|
13
|
+
TABLE = [
|
14
|
+
0x0000000000000000, 0x01b0000000000000, 0x0360000000000000, 0x02d0000000000000,
|
15
|
+
0x06c0000000000000, 0x0770000000000000, 0x05a0000000000000, 0x0410000000000000,
|
16
|
+
0x0d80000000000000, 0x0c30000000000000, 0x0ee0000000000000, 0x0f50000000000000,
|
17
|
+
0x0b40000000000000, 0x0af0000000000000, 0x0820000000000000, 0x0990000000000000,
|
18
|
+
0x1b00000000000000, 0x1ab0000000000000, 0x1860000000000000, 0x19d0000000000000,
|
19
|
+
0x1dc0000000000000, 0x1c70000000000000, 0x1ea0000000000000, 0x1f10000000000000,
|
20
|
+
0x1680000000000000, 0x1730000000000000, 0x15e0000000000000, 0x1450000000000000,
|
21
|
+
0x1040000000000000, 0x11f0000000000000, 0x1320000000000000, 0x1290000000000000,
|
22
|
+
0x3600000000000000, 0x37b0000000000000, 0x3560000000000000, 0x34d0000000000000,
|
23
|
+
0x30c0000000000000, 0x3170000000000000, 0x33a0000000000000, 0x3210000000000000,
|
24
|
+
0x3b80000000000000, 0x3a30000000000000, 0x38e0000000000000, 0x3950000000000000,
|
25
|
+
0x3d40000000000000, 0x3cf0000000000000, 0x3e20000000000000, 0x3f90000000000000,
|
26
|
+
0x2d00000000000000, 0x2cb0000000000000, 0x2e60000000000000, 0x2fd0000000000000,
|
27
|
+
0x2bc0000000000000, 0x2a70000000000000, 0x28a0000000000000, 0x2910000000000000,
|
28
|
+
0x2080000000000000, 0x2130000000000000, 0x23e0000000000000, 0x2250000000000000,
|
29
|
+
0x2640000000000000, 0x27f0000000000000, 0x2520000000000000, 0x2490000000000000,
|
30
|
+
0x6c00000000000000, 0x6db0000000000000, 0x6f60000000000000, 0x6ed0000000000000,
|
31
|
+
0x6ac0000000000000, 0x6b70000000000000, 0x69a0000000000000, 0x6810000000000000,
|
32
|
+
0x6180000000000000, 0x6030000000000000, 0x62e0000000000000, 0x6350000000000000,
|
33
|
+
0x6740000000000000, 0x66f0000000000000, 0x6420000000000000, 0x6590000000000000,
|
34
|
+
0x7700000000000000, 0x76b0000000000000, 0x7460000000000000, 0x75d0000000000000,
|
35
|
+
0x71c0000000000000, 0x7070000000000000, 0x72a0000000000000, 0x7310000000000000,
|
36
|
+
0x7a80000000000000, 0x7b30000000000000, 0x79e0000000000000, 0x7850000000000000,
|
37
|
+
0x7c40000000000000, 0x7df0000000000000, 0x7f20000000000000, 0x7e90000000000000,
|
38
|
+
0x5a00000000000000, 0x5bb0000000000000, 0x5960000000000000, 0x58d0000000000000,
|
39
|
+
0x5cc0000000000000, 0x5d70000000000000, 0x5fa0000000000000, 0x5e10000000000000,
|
40
|
+
0x5780000000000000, 0x5630000000000000, 0x54e0000000000000, 0x5550000000000000,
|
41
|
+
0x5140000000000000, 0x50f0000000000000, 0x5220000000000000, 0x5390000000000000,
|
42
|
+
0x4100000000000000, 0x40b0000000000000, 0x4260000000000000, 0x43d0000000000000,
|
43
|
+
0x47c0000000000000, 0x4670000000000000, 0x44a0000000000000, 0x4510000000000000,
|
44
|
+
0x4c80000000000000, 0x4d30000000000000, 0x4fe0000000000000, 0x4e50000000000000,
|
45
|
+
0x4a40000000000000, 0x4bf0000000000000, 0x4920000000000000, 0x4890000000000000,
|
46
|
+
0xd800000000000000, 0xd9b0000000000000, 0xdb60000000000000, 0xdad0000000000000,
|
47
|
+
0xdec0000000000000, 0xdf70000000000000, 0xdda0000000000000, 0xdc10000000000000,
|
48
|
+
0xd580000000000000, 0xd430000000000000, 0xd6e0000000000000, 0xd750000000000000,
|
49
|
+
0xd340000000000000, 0xd2f0000000000000, 0xd020000000000000, 0xd190000000000000,
|
50
|
+
0xc300000000000000, 0xc2b0000000000000, 0xc060000000000000, 0xc1d0000000000000,
|
51
|
+
0xc5c0000000000000, 0xc470000000000000, 0xc6a0000000000000, 0xc710000000000000,
|
52
|
+
0xce80000000000000, 0xcf30000000000000, 0xcde0000000000000, 0xcc50000000000000,
|
53
|
+
0xc840000000000000, 0xc9f0000000000000, 0xcb20000000000000, 0xca90000000000000,
|
54
|
+
0xee00000000000000, 0xefb0000000000000, 0xed60000000000000, 0xecd0000000000000,
|
55
|
+
0xe8c0000000000000, 0xe970000000000000, 0xeba0000000000000, 0xea10000000000000,
|
56
|
+
0xe380000000000000, 0xe230000000000000, 0xe0e0000000000000, 0xe150000000000000,
|
57
|
+
0xe540000000000000, 0xe4f0000000000000, 0xe620000000000000, 0xe790000000000000,
|
58
|
+
0xf500000000000000, 0xf4b0000000000000, 0xf660000000000000, 0xf7d0000000000000,
|
59
|
+
0xf3c0000000000000, 0xf270000000000000, 0xf0a0000000000000, 0xf110000000000000,
|
60
|
+
0xf880000000000000, 0xf930000000000000, 0xfbe0000000000000, 0xfa50000000000000,
|
61
|
+
0xfe40000000000000, 0xfff0000000000000, 0xfd20000000000000, 0xfc90000000000000,
|
62
|
+
0xb400000000000000, 0xb5b0000000000000, 0xb760000000000000, 0xb6d0000000000000,
|
63
|
+
0xb2c0000000000000, 0xb370000000000000, 0xb1a0000000000000, 0xb010000000000000,
|
64
|
+
0xb980000000000000, 0xb830000000000000, 0xbae0000000000000, 0xbb50000000000000,
|
65
|
+
0xbf40000000000000, 0xbef0000000000000, 0xbc20000000000000, 0xbd90000000000000,
|
66
|
+
0xaf00000000000000, 0xaeb0000000000000, 0xac60000000000000, 0xadd0000000000000,
|
67
|
+
0xa9c0000000000000, 0xa870000000000000, 0xaaa0000000000000, 0xab10000000000000,
|
68
|
+
0xa280000000000000, 0xa330000000000000, 0xa1e0000000000000, 0xa050000000000000,
|
69
|
+
0xa440000000000000, 0xa5f0000000000000, 0xa720000000000000, 0xa690000000000000,
|
70
|
+
0x8200000000000000, 0x83b0000000000000, 0x8160000000000000, 0x80d0000000000000,
|
71
|
+
0x84c0000000000000, 0x8570000000000000, 0x87a0000000000000, 0x8610000000000000,
|
72
|
+
0x8f80000000000000, 0x8e30000000000000, 0x8ce0000000000000, 0x8d50000000000000,
|
73
|
+
0x8940000000000000, 0x88f0000000000000, 0x8a20000000000000, 0x8b90000000000000,
|
74
|
+
0x9900000000000000, 0x98b0000000000000, 0x9a60000000000000, 0x9bd0000000000000,
|
75
|
+
0x9fc0000000000000, 0x9e70000000000000, 0x9ca0000000000000, 0x9d10000000000000,
|
76
|
+
0x9480000000000000, 0x9530000000000000, 0x97e0000000000000, 0x9650000000000000,
|
77
|
+
0x9240000000000000, 0x93f0000000000000, 0x9120000000000000, 0x9090000000000000
|
78
|
+
]
|
79
|
+
|
80
|
+
#
|
81
|
+
# Packs the CRC64 checksum.
|
82
|
+
#
|
83
|
+
# @param [Integer] crc
|
84
|
+
# The checksum to pack.
|
85
|
+
#
|
86
|
+
# @return [String]
|
87
|
+
# The packed checksum.
|
88
|
+
#
|
89
|
+
def self.pack(crc)
|
90
|
+
buffer = ''
|
91
|
+
|
92
|
+
buffer << ((crc & 0xff00000000000000) >> 56).chr
|
93
|
+
buffer << ((crc & 0xff000000000000) >> 48).chr
|
94
|
+
buffer << ((crc & 0xff0000000000) >> 40).chr
|
95
|
+
buffer << ((crc & 0xff00000000) >> 32).chr
|
96
|
+
buffer << ((crc & 0xff000000) >> 24).chr
|
97
|
+
buffer << ((crc & 0xff0000) >> 16).chr
|
98
|
+
buffer << ((crc & 0xff00) >> 8).chr
|
99
|
+
buffer << (crc & 0xff).chr
|
100
|
+
|
101
|
+
buffer
|
102
|
+
end
|
103
|
+
|
104
|
+
#
|
105
|
+
# Updates the CRC64 checksum.
|
106
|
+
#
|
107
|
+
# @param [String] data
|
108
|
+
# The data to update the checksum with.
|
109
|
+
#
|
110
|
+
def update(data)
|
111
|
+
data.each_byte do |b|
|
112
|
+
@crc = ((TABLE[(@crc ^ b) & 0xff] ^ (@crc >> 8)) & 0xffffffffffffffff)
|
113
|
+
end
|
114
|
+
|
115
|
+
return self
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
119
|
+
end
|
data/lib/digest/crc8.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'digest/crc'
|
2
|
+
|
3
|
+
module Digest
|
4
|
+
class CRC8 < CRC
|
5
|
+
|
6
|
+
WIDTH = 8
|
7
|
+
|
8
|
+
INIT_CRC = 0x00
|
9
|
+
|
10
|
+
# Generated by `./pycrc.py --algorithm=table-driven --model=crc-8 --generate=c`
|
11
|
+
TABLE = [
|
12
|
+
0x00, 0x07, 0x0e, 0x09, 0x1c, 0x1b, 0x12, 0x15, 0x38, 0x3f, 0x36, 0x31, 0x24, 0x23, 0x2a, 0x2d,
|
13
|
+
0x70, 0x77, 0x7e, 0x79, 0x6c, 0x6b, 0x62, 0x65, 0x48, 0x4f, 0x46, 0x41, 0x54, 0x53, 0x5a, 0x5d,
|
14
|
+
0xe0, 0xe7, 0xee, 0xe9, 0xfc, 0xfb, 0xf2, 0xf5, 0xd8, 0xdf, 0xd6, 0xd1, 0xc4, 0xc3, 0xca, 0xcd,
|
15
|
+
0x90, 0x97, 0x9e, 0x99, 0x8c, 0x8b, 0x82, 0x85, 0xa8, 0xaf, 0xa6, 0xa1, 0xb4, 0xb3, 0xba, 0xbd,
|
16
|
+
0xc7, 0xc0, 0xc9, 0xce, 0xdb, 0xdc, 0xd5, 0xd2, 0xff, 0xf8, 0xf1, 0xf6, 0xe3, 0xe4, 0xed, 0xea,
|
17
|
+
0xb7, 0xb0, 0xb9, 0xbe, 0xab, 0xac, 0xa5, 0xa2, 0x8f, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9d, 0x9a,
|
18
|
+
0x27, 0x20, 0x29, 0x2e, 0x3b, 0x3c, 0x35, 0x32, 0x1f, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0d, 0x0a,
|
19
|
+
0x57, 0x50, 0x59, 0x5e, 0x4b, 0x4c, 0x45, 0x42, 0x6f, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7d, 0x7a,
|
20
|
+
0x89, 0x8e, 0x87, 0x80, 0x95, 0x92, 0x9b, 0x9c, 0xb1, 0xb6, 0xbf, 0xb8, 0xad, 0xaa, 0xa3, 0xa4,
|
21
|
+
0xf9, 0xfe, 0xf7, 0xf0, 0xe5, 0xe2, 0xeb, 0xec, 0xc1, 0xc6, 0xcf, 0xc8, 0xdd, 0xda, 0xd3, 0xd4,
|
22
|
+
0x69, 0x6e, 0x67, 0x60, 0x75, 0x72, 0x7b, 0x7c, 0x51, 0x56, 0x5f, 0x58, 0x4d, 0x4a, 0x43, 0x44,
|
23
|
+
0x19, 0x1e, 0x17, 0x10, 0x05, 0x02, 0x0b, 0x0c, 0x21, 0x26, 0x2f, 0x28, 0x3d, 0x3a, 0x33, 0x34,
|
24
|
+
0x4e, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5c, 0x5b, 0x76, 0x71, 0x78, 0x7f, 0x6a, 0x6d, 0x64, 0x63,
|
25
|
+
0x3e, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2c, 0x2b, 0x06, 0x01, 0x08, 0x0f, 0x1a, 0x1d, 0x14, 0x13,
|
26
|
+
0xae, 0xa9, 0xa0, 0xa7, 0xb2, 0xb5, 0xbc, 0xbb, 0x96, 0x91, 0x98, 0x9f, 0x8a, 0x8d, 0x84, 0x83,
|
27
|
+
0xde, 0xd9, 0xd0, 0xd7, 0xc2, 0xc5, 0xcc, 0xcb, 0xe6, 0xe1, 0xe8, 0xef, 0xfa, 0xfd, 0xf4, 0xf3
|
28
|
+
]
|
29
|
+
|
30
|
+
#
|
31
|
+
# Packs the CRC8 checksum.
|
32
|
+
#
|
33
|
+
# @param [Integer] crc
|
34
|
+
# The checksum to pack.
|
35
|
+
#
|
36
|
+
# @return [String]
|
37
|
+
# The packed checksum.
|
38
|
+
#
|
39
|
+
def self.pack(crc)
|
40
|
+
(crc & 0xff).chr
|
41
|
+
end
|
42
|
+
|
43
|
+
#
|
44
|
+
# Updates the CRC8 checksum.
|
45
|
+
#
|
46
|
+
# @param [String] data
|
47
|
+
# The data to update the checksum with.
|
48
|
+
#
|
49
|
+
def update(data)
|
50
|
+
data.each_byte do |b|
|
51
|
+
@crc = ((TABLE[(@crc ^ b) & 0xff] ^ (@crc << 8)) & 0xff)
|
52
|
+
end
|
53
|
+
|
54
|
+
return self
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'crc_examples'
|
3
|
+
require 'digest/crc16_ccitt'
|
4
|
+
|
5
|
+
describe Digest::CRC16CCITT do
|
6
|
+
before(:all) do
|
7
|
+
@crc_class = Digest::CRC16CCITT
|
8
|
+
@string = '1234567890'
|
9
|
+
@expected = '3218'
|
10
|
+
end
|
11
|
+
|
12
|
+
it_should_behave_like "CRC"
|
13
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'crc_examples'
|
3
|
+
require 'digest/crc16_modbus'
|
4
|
+
|
5
|
+
describe Digest::CRC16Modbus do
|
6
|
+
before(:all) do
|
7
|
+
@crc_class = Digest::CRC16Modbus
|
8
|
+
@string = '1234567890'
|
9
|
+
@expected = 'c20a'
|
10
|
+
end
|
11
|
+
|
12
|
+
it_should_behave_like "CRC"
|
13
|
+
end
|
data/spec/crc16_spec.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'crc_examples'
|
3
|
+
require 'digest/crc16_xmodem'
|
4
|
+
|
5
|
+
describe Digest::CRC16XModem do
|
6
|
+
before(:all) do
|
7
|
+
@crc_class = Digest::CRC16XModem
|
8
|
+
@string = '1234567890'
|
9
|
+
@expected = 'd321'
|
10
|
+
end
|
11
|
+
|
12
|
+
it_should_behave_like "CRC"
|
13
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'crc_examples'
|
3
|
+
require 'digest/crc16_zmodem'
|
4
|
+
|
5
|
+
describe Digest::CRC16ZModem do
|
6
|
+
before(:all) do
|
7
|
+
@crc_class = Digest::CRC16ZModem
|
8
|
+
@string = '1234567890'
|
9
|
+
@expected = 'd321'
|
10
|
+
end
|
11
|
+
|
12
|
+
it_should_behave_like "CRC"
|
13
|
+
end
|
data/spec/crc1_spec.rb
ADDED
data/spec/crc24_spec.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'crc_examples'
|
3
|
+
require 'digest/crc32_mpeg'
|
4
|
+
|
5
|
+
describe Digest::CRC32Mpeg do
|
6
|
+
before(:all) do
|
7
|
+
@crc_class = Digest::CRC32Mpeg
|
8
|
+
@string = '1234567890'
|
9
|
+
@expected = 'af97ac49'
|
10
|
+
end
|
11
|
+
|
12
|
+
it_should_behave_like "CRC"
|
13
|
+
end
|