digest-crc 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.yardopts +1 -0
- data/ChangeLog.md +6 -0
- data/LICENSE.txt +1 -1
- data/README.md +55 -16
- data/Rakefile +7 -3
- data/gemspec.yml +1 -1
- data/lib/digest/crc.rb +12 -4
- data/lib/digest/crc1.rb +0 -3
- data/lib/digest/crc16.rb +2 -2
- data/lib/digest/crc16_ccitt.rb +2 -2
- data/lib/digest/crc16_dnp.rb +2 -2
- data/lib/digest/crc16_modbus.rb +1 -15
- data/lib/digest/crc16_qt.rb +1 -1
- data/lib/digest/crc16_xmodem.rb +2 -2
- data/lib/digest/crc16_zmodem.rb +2 -2
- data/lib/digest/crc24.rb +2 -2
- data/lib/digest/crc32.rb +2 -2
- data/lib/digest/crc32_mpeg.rb +2 -2
- data/lib/digest/crc32c.rb +1 -15
- data/lib/digest/crc5.rb +11 -2
- data/lib/digest/crc64.rb +2 -2
- data/lib/digest/crc8.rb +2 -2
- data/lib/digest/crc8_1wire.rb +2 -33
- data/spec/crc_spec.rb +50 -0
- metadata +7 -15
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5090c17bdfb58a94270e81bab8753095324379c5
|
4
|
+
data.tar.gz: ec891e2886a61cd48e28f2a119ebb48bd01f68b7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5539def533acad46a8a1ac25c602bee61832c9912598975884c25388e369658ce59245f6160ef1341b216a8b3e6aceb59c23f9b8b1e62dece990dad3ac3d2742
|
7
|
+
data.tar.gz: bf09d57ef56303bd70d0df44fa20a1e2d2fd107bf052fa31ba6191a827cbaa81659eb7aa2fb795f694f0f036abd653b425cd60388106df30275aadb343eaeded
|
data/.yardopts
CHANGED
data/ChangeLog.md
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
### 0.4.1 / 2014-04-16
|
2
|
+
|
3
|
+
* Allow Digest CRC classes to be extended and their constants overriden.
|
4
|
+
* Allow {Digest::CRC5::CRC_MASK} to be overriden by subclasses.
|
5
|
+
* {Digest::CRC81Wire} now inherites from {Digest::CRC8}.
|
6
|
+
|
1
7
|
### 0.4.0 / 2013-02-13
|
2
8
|
|
3
9
|
* Added {Digest::CRC16QT}.
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -35,35 +35,74 @@ module.
|
|
35
35
|
|
36
36
|
## Install
|
37
37
|
|
38
|
-
|
38
|
+
```
|
39
|
+
gem install digest-crc
|
40
|
+
```
|
39
41
|
|
40
42
|
## Examples
|
41
43
|
|
42
44
|
Calculate a CRC32:
|
43
45
|
|
44
|
-
|
46
|
+
```ruby
|
47
|
+
require 'digest/crc32'
|
45
48
|
|
46
|
-
|
47
|
-
|
49
|
+
Digest::CRC32.hexdigest('hello')
|
50
|
+
# => "3610a686"
|
51
|
+
```
|
48
52
|
|
49
53
|
Calculate a CRC32 of a file:
|
50
54
|
|
51
|
-
|
52
|
-
|
55
|
+
```ruby
|
56
|
+
Digest::CRC32.file('README.md')
|
57
|
+
# => #<Digest::CRC32: 127ad531>
|
58
|
+
```
|
53
59
|
|
54
60
|
Incrementally calculate a CRC32:
|
55
61
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
+
```ruby
|
63
|
+
crc = Digest::CRC32.new
|
64
|
+
crc << 'one'
|
65
|
+
crc << 'two'
|
66
|
+
crc << 'three'
|
67
|
+
crc.hexdigest
|
68
|
+
# => "09e1c092"
|
69
|
+
```
|
62
70
|
|
63
71
|
Directly access the checksum:
|
64
72
|
|
65
|
-
|
66
|
-
|
73
|
+
```ruby
|
74
|
+
crc.checksum
|
75
|
+
# => 165789842
|
76
|
+
```
|
77
|
+
|
78
|
+
Defining your own CRC class:
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
require 'digest/crc32'
|
82
|
+
|
83
|
+
module Digest
|
84
|
+
class CRC3000 < CRC32
|
85
|
+
|
86
|
+
WIDTH = 4
|
87
|
+
|
88
|
+
INIT_CRC = 0xffffffff
|
89
|
+
|
90
|
+
XOR_MASK = 0xffffffff
|
91
|
+
|
92
|
+
TABLE = [
|
93
|
+
# ....
|
94
|
+
].freeze
|
95
|
+
|
96
|
+
def update(data)
|
97
|
+
data.each_byte do |b|
|
98
|
+
@crc = (((@crc >> 8) & 0x00ffffff) ^ @table[(@crc ^ b) & 0xff])
|
99
|
+
end
|
100
|
+
|
101
|
+
return self
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
```
|
67
106
|
|
68
107
|
## Thanks
|
69
108
|
|
@@ -73,6 +112,6 @@ including their CRC Tables.
|
|
73
112
|
|
74
113
|
## License
|
75
114
|
|
76
|
-
Copyright (c) 2010-
|
115
|
+
Copyright (c) 2010-2014 Hal Brodigan
|
77
116
|
|
78
|
-
See
|
117
|
+
See [LICENSE.txt](LICENSE.txt) for license information.
|
data/Rakefile
CHANGED
@@ -18,7 +18,7 @@ begin
|
|
18
18
|
RSpec::Core::RakeTask.new
|
19
19
|
rescue LoadError => e
|
20
20
|
task :spec do
|
21
|
-
|
21
|
+
puts e.message
|
22
22
|
end
|
23
23
|
end
|
24
24
|
task :test => :spec
|
@@ -26,11 +26,15 @@ task :default => :spec
|
|
26
26
|
|
27
27
|
begin
|
28
28
|
gem 'yard', '~> 0.8'
|
29
|
+
gem 'redcarpet'
|
30
|
+
gem 'github-markup'
|
29
31
|
require 'yard'
|
32
|
+
require 'redcarpet'
|
33
|
+
require 'github-markup'
|
30
34
|
|
31
|
-
YARD::Rake::YardocTask.new
|
35
|
+
YARD::Rake::YardocTask.new
|
32
36
|
rescue LoadError => e
|
33
37
|
task :yard do
|
34
|
-
|
38
|
+
puts e.message
|
35
39
|
end
|
36
40
|
end
|
data/gemspec.yml
CHANGED
data/lib/digest/crc.rb
CHANGED
@@ -17,6 +17,9 @@ module Digest
|
|
17
17
|
# The bit width of the CRC checksum
|
18
18
|
WIDTH = 0
|
19
19
|
|
20
|
+
# Default place holder CRC table
|
21
|
+
TABLE = [].freeze
|
22
|
+
|
20
23
|
#
|
21
24
|
# Calculates the CRC checksum.
|
22
25
|
#
|
@@ -47,7 +50,12 @@ module Digest
|
|
47
50
|
# Initializes the CRC checksum.
|
48
51
|
#
|
49
52
|
def initialize
|
50
|
-
@
|
53
|
+
@init_crc = self.class.const_get(:INIT_CRC)
|
54
|
+
@xor_mask = self.class.const_get(:XOR_MASK)
|
55
|
+
@width = self.class.const_get(:WIDTH)
|
56
|
+
@table = self.class.const_get(:TABLE)
|
57
|
+
|
58
|
+
reset
|
51
59
|
end
|
52
60
|
|
53
61
|
#
|
@@ -66,7 +74,7 @@ module Digest
|
|
66
74
|
# The length in bytes.
|
67
75
|
#
|
68
76
|
def digest_length
|
69
|
-
(
|
77
|
+
(@width / 8.0).ceil
|
70
78
|
end
|
71
79
|
|
72
80
|
#
|
@@ -93,7 +101,7 @@ module Digest
|
|
93
101
|
# The default value of the CRC checksum.
|
94
102
|
#
|
95
103
|
def reset
|
96
|
-
@crc =
|
104
|
+
@crc = @init_crc
|
97
105
|
end
|
98
106
|
|
99
107
|
#
|
@@ -103,7 +111,7 @@ module Digest
|
|
103
111
|
# The resulting CRC checksum.
|
104
112
|
#
|
105
113
|
def checksum
|
106
|
-
@crc ^
|
114
|
+
@crc ^ @xor_mask
|
107
115
|
end
|
108
116
|
|
109
117
|
#
|
data/lib/digest/crc1.rb
CHANGED
data/lib/digest/crc16.rb
CHANGED
@@ -44,7 +44,7 @@ module Digest
|
|
44
44
|
0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41,
|
45
45
|
0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641,
|
46
46
|
0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040
|
47
|
-
]
|
47
|
+
].freeze
|
48
48
|
|
49
49
|
#
|
50
50
|
# Packs the CRC16 checksum.
|
@@ -72,7 +72,7 @@ module Digest
|
|
72
72
|
#
|
73
73
|
def update(data)
|
74
74
|
data.each_byte do |b|
|
75
|
-
@crc = ((
|
75
|
+
@crc = ((@table[(@crc ^ b) & 0xff] ^ (@crc >> 8)) & 0xffff)
|
76
76
|
end
|
77
77
|
|
78
78
|
return self
|
data/lib/digest/crc16_ccitt.rb
CHANGED
@@ -42,7 +42,7 @@ module Digest
|
|
42
42
|
0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
|
43
43
|
0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
|
44
44
|
0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
|
45
|
-
]
|
45
|
+
].freeze
|
46
46
|
|
47
47
|
#
|
48
48
|
# Updates the CRC16 CCITT checksum.
|
@@ -52,7 +52,7 @@ module Digest
|
|
52
52
|
#
|
53
53
|
def update(data)
|
54
54
|
data.each_byte do |b|
|
55
|
-
@crc = ((
|
55
|
+
@crc = ((@table[((@crc >> 8) ^ b) & 0xff] ^ (@crc << 8)) & 0xffff)
|
56
56
|
end
|
57
57
|
|
58
58
|
return self
|
data/lib/digest/crc16_dnp.rb
CHANGED
@@ -41,7 +41,7 @@ module Digest
|
|
41
41
|
0x23c4, 0x159a, 0x4f78, 0x7926, 0xfabc, 0xcce2, 0x9600, 0xa05e,
|
42
42
|
0x6e26, 0x5878, 0x029a, 0x34c4, 0xb75e, 0x8100, 0xdbe2, 0xedbc,
|
43
43
|
0x91af, 0xa7f1, 0xfd13, 0xcb4d, 0x48d7, 0x7e89, 0x246b, 0x1235
|
44
|
-
]
|
44
|
+
].freeze
|
45
45
|
|
46
46
|
#
|
47
47
|
# Updates the CRC16 DNP checksum.
|
@@ -51,7 +51,7 @@ module Digest
|
|
51
51
|
#
|
52
52
|
def update(data)
|
53
53
|
data.each_byte do |b|
|
54
|
-
@crc = ((@crc >> 8) ^
|
54
|
+
@crc = ((@crc >> 8) ^ @table[(@crc ^ b) & 0xff])
|
55
55
|
end
|
56
56
|
|
57
57
|
return self
|
data/lib/digest/crc16_modbus.rb
CHANGED
@@ -42,21 +42,7 @@ module Digest
|
|
42
42
|
0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41,
|
43
43
|
0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641,
|
44
44
|
0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040
|
45
|
-
]
|
46
|
-
|
47
|
-
#
|
48
|
-
# Updates the CRC16 Modbus checksum.
|
49
|
-
#
|
50
|
-
# @param [String] data
|
51
|
-
# The data to update the checksum with.
|
52
|
-
#
|
53
|
-
def update(data)
|
54
|
-
data.each_byte do |b|
|
55
|
-
@crc = (((@crc >> 8) ^ TABLE[(b ^ @crc) & 0xff]) & 0xffff)
|
56
|
-
end
|
57
|
-
|
58
|
-
return self
|
59
|
-
end
|
45
|
+
].freeze
|
60
46
|
|
61
47
|
end
|
62
48
|
end
|
data/lib/digest/crc16_qt.rb
CHANGED
@@ -22,7 +22,7 @@ module Digest
|
|
22
22
|
def update(data)
|
23
23
|
data.each_byte do |b|
|
24
24
|
b = revert_byte(b) if REVERSE_DATA
|
25
|
-
@crc = ((
|
25
|
+
@crc = ((@table[((@crc >> 8) ^ b) & 0xff] ^ (@crc << 8)) & 0xffff)
|
26
26
|
end
|
27
27
|
|
28
28
|
return self
|
data/lib/digest/crc16_xmodem.rb
CHANGED
@@ -40,7 +40,7 @@ module Digest
|
|
40
40
|
0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
|
41
41
|
0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
|
42
42
|
0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
|
43
|
-
]
|
43
|
+
].freeze
|
44
44
|
|
45
45
|
#
|
46
46
|
# Updates the CRC16 XModem checksum.
|
@@ -50,7 +50,7 @@ module Digest
|
|
50
50
|
#
|
51
51
|
def update(data)
|
52
52
|
data.each_byte do |b|
|
53
|
-
@crc = ((
|
53
|
+
@crc = ((@table[((@crc >> 8) ^ b) & 0xff] ^ (@crc << 8)) & 0xffff)
|
54
54
|
end
|
55
55
|
|
56
56
|
return self
|
data/lib/digest/crc16_zmodem.rb
CHANGED
@@ -40,7 +40,7 @@ module Digest
|
|
40
40
|
0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
|
41
41
|
0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
|
42
42
|
0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
|
43
|
-
]
|
43
|
+
].freeze
|
44
44
|
|
45
45
|
#
|
46
46
|
# Updates the CRC16 checksum.
|
@@ -50,7 +50,7 @@ module Digest
|
|
50
50
|
#
|
51
51
|
def update(data)
|
52
52
|
data.each_byte do |b|
|
53
|
-
@crc = ((
|
53
|
+
@crc = ((@table[((@crc >> 8) ^ b) & 0xff] ^ (@crc << 8)) & 0xffff)
|
54
54
|
end
|
55
55
|
|
56
56
|
return self
|
data/lib/digest/crc24.rb
CHANGED
@@ -44,7 +44,7 @@ module Digest
|
|
44
44
|
0x87b4a6, 0x01f85d, 0x0d61ab, 0x8b2d50, 0x145247, 0x921ebc, 0x9e874a, 0x18cbb1,
|
45
45
|
0xe37b16, 0x6537ed, 0x69ae1b, 0xefe2e0, 0x709df7, 0xf6d10c, 0xfa48fa, 0x7c0401,
|
46
46
|
0x42fa2f, 0xc4b6d4, 0xc82f22, 0x4e63d9, 0xd11cce, 0x575035, 0x5bc9c3, 0xdd8538
|
47
|
-
]
|
47
|
+
].freeze
|
48
48
|
|
49
49
|
#
|
50
50
|
# Packs the CRC24 checksum.
|
@@ -73,7 +73,7 @@ module Digest
|
|
73
73
|
#
|
74
74
|
def update(data)
|
75
75
|
data.each_byte do |b|
|
76
|
-
@crc = ((
|
76
|
+
@crc = ((@table[((@crc >> 16) ^ b) & 0xff] ^ (@crc << 8)) & 0xffffff)
|
77
77
|
end
|
78
78
|
|
79
79
|
return self
|
data/lib/digest/crc32.rb
CHANGED
@@ -78,7 +78,7 @@ module Digest
|
|
78
78
|
0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
|
79
79
|
0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
|
80
80
|
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
|
81
|
-
]
|
81
|
+
].freeze
|
82
82
|
|
83
83
|
#
|
84
84
|
# Packs the CRC32 checksum.
|
@@ -108,7 +108,7 @@ module Digest
|
|
108
108
|
#
|
109
109
|
def update(data)
|
110
110
|
data.each_byte do |b|
|
111
|
-
@crc = (((@crc >> 8) & 0x00ffffff) ^
|
111
|
+
@crc = (((@crc >> 8) & 0x00ffffff) ^ @table[(@crc ^ b) & 0xff])
|
112
112
|
end
|
113
113
|
|
114
114
|
return self
|
data/lib/digest/crc32_mpeg.rb
CHANGED
@@ -73,7 +73,7 @@ module Digest
|
|
73
73
|
0x9abc8bd5, 0x9e7d9662, 0x933eb0bb, 0x97ffad0c,
|
74
74
|
0xafb010b1, 0xab710d06, 0xa6322bdf, 0xa2f33668,
|
75
75
|
0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4
|
76
|
-
]
|
76
|
+
].freeze
|
77
77
|
|
78
78
|
#
|
79
79
|
# Updates the CRC32 Mpeg checksum.
|
@@ -83,7 +83,7 @@ module Digest
|
|
83
83
|
#
|
84
84
|
def update(data)
|
85
85
|
data.each_byte do |b|
|
86
|
-
@crc = ((
|
86
|
+
@crc = ((@table[((@crc >> 24) ^ b) & 0xff] ^ (@crc << 8)) & 0xffffffff)
|
87
87
|
end
|
88
88
|
|
89
89
|
return self
|
data/lib/digest/crc32c.rb
CHANGED
@@ -72,21 +72,7 @@ module Digest
|
|
72
72
|
0x34f4f86a, 0xc69f7b69, 0xd5cf889d, 0x27a40b9e,
|
73
73
|
0x79b737ba, 0x8bdcb4b9, 0x988c474d, 0x6ae7c44e,
|
74
74
|
0xbe2da0a5, 0x4c4623a6, 0x5f16d052, 0xad7d5351
|
75
|
-
]
|
76
|
-
|
77
|
-
#
|
78
|
-
# Updates the CRC32 checksum.
|
79
|
-
#
|
80
|
-
# @param [String] data
|
81
|
-
# The data to update the checksum with.
|
82
|
-
#
|
83
|
-
def update(data)
|
84
|
-
data.each_byte do |b|
|
85
|
-
@crc = (((@crc >> 8) & 0x00ffffff) ^ TABLE[(@crc ^ b) & 0xff])
|
86
|
-
end
|
87
|
-
|
88
|
-
return self
|
89
|
-
end
|
75
|
+
].freeze
|
90
76
|
|
91
77
|
end
|
92
78
|
end
|
data/lib/digest/crc5.rb
CHANGED
@@ -32,7 +32,16 @@ module Digest
|
|
32
32
|
0x40, 0x30, 0xa0, 0xd0, 0xc8, 0xb8, 0x28, 0x58, 0x18, 0x68, 0xf8, 0x88, 0x90, 0xe0, 0x70, 0x00,
|
33
33
|
0xd8, 0xa8, 0x38, 0x48, 0x50, 0x20, 0xb0, 0xc0, 0x80, 0xf0, 0x60, 0x10, 0x08, 0x78, 0xe8, 0x98,
|
34
34
|
0x68, 0x18, 0x88, 0xf8, 0xe0, 0x90, 0x00, 0x70, 0x30, 0x40, 0xd0, 0xa0, 0xb8, 0xc8, 0x58, 0x28
|
35
|
-
]
|
35
|
+
].freeze
|
36
|
+
|
37
|
+
#
|
38
|
+
# Initializes the CRC5 instance.
|
39
|
+
#
|
40
|
+
def initialize
|
41
|
+
@crc_mask = self.class.const_get(:CRC_MASK)
|
42
|
+
|
43
|
+
super
|
44
|
+
end
|
36
45
|
|
37
46
|
#
|
38
47
|
# Updates the CRC5 checksum.
|
@@ -42,7 +51,7 @@ module Digest
|
|
42
51
|
#
|
43
52
|
def update(data)
|
44
53
|
data.each_byte do |b|
|
45
|
-
@crc = ((
|
54
|
+
@crc = ((@table[((@crc >> 3) ^ b) & 0xff] ^ (@crc >> 8)) & @crc_mask)
|
46
55
|
end
|
47
56
|
|
48
57
|
return self
|
data/lib/digest/crc64.rb
CHANGED
@@ -78,7 +78,7 @@ module Digest
|
|
78
78
|
0x9fc0000000000000, 0x9e70000000000000, 0x9ca0000000000000, 0x9d10000000000000,
|
79
79
|
0x9480000000000000, 0x9530000000000000, 0x97e0000000000000, 0x9650000000000000,
|
80
80
|
0x9240000000000000, 0x93f0000000000000, 0x9120000000000000, 0x9090000000000000
|
81
|
-
]
|
81
|
+
].freeze
|
82
82
|
|
83
83
|
#
|
84
84
|
# Packs the CRC64 checksum.
|
@@ -112,7 +112,7 @@ module Digest
|
|
112
112
|
#
|
113
113
|
def update(data)
|
114
114
|
data.each_byte do |b|
|
115
|
-
@crc = ((
|
115
|
+
@crc = ((@table[(@crc ^ b) & 0xff] ^ (@crc >> 8)) & 0xffffffffffffffff)
|
116
116
|
end
|
117
117
|
|
118
118
|
return self
|
data/lib/digest/crc8.rb
CHANGED
@@ -28,7 +28,7 @@ module Digest
|
|
28
28
|
0x3e, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2c, 0x2b, 0x06, 0x01, 0x08, 0x0f, 0x1a, 0x1d, 0x14, 0x13,
|
29
29
|
0xae, 0xa9, 0xa0, 0xa7, 0xb2, 0xb5, 0xbc, 0xbb, 0x96, 0x91, 0x98, 0x9f, 0x8a, 0x8d, 0x84, 0x83,
|
30
30
|
0xde, 0xd9, 0xd0, 0xd7, 0xc2, 0xc5, 0xcc, 0xcb, 0xe6, 0xe1, 0xe8, 0xef, 0xfa, 0xfd, 0xf4, 0xf3
|
31
|
-
]
|
31
|
+
].freeze
|
32
32
|
|
33
33
|
#
|
34
34
|
# Packs the CRC8 checksum.
|
@@ -51,7 +51,7 @@ module Digest
|
|
51
51
|
#
|
52
52
|
def update(data)
|
53
53
|
data.each_byte do |b|
|
54
|
-
@crc = ((
|
54
|
+
@crc = ((@table[(@crc ^ b) & 0xff] ^ (@crc << 8)) & 0xff)
|
55
55
|
end
|
56
56
|
|
57
57
|
return self
|
data/lib/digest/crc8_1wire.rb
CHANGED
@@ -4,11 +4,7 @@ module Digest
|
|
4
4
|
#
|
5
5
|
# Implements the CRC8 1-Wire algorithm.
|
6
6
|
#
|
7
|
-
class CRC81Wire <
|
8
|
-
|
9
|
-
WIDTH = 8
|
10
|
-
|
11
|
-
INIT_CRC = 0x00
|
7
|
+
class CRC81Wire < CRC8
|
12
8
|
|
13
9
|
# Generated by `./pycrc.py --algorithm=table-driven --model=dallas-1-wire --generate=c`
|
14
10
|
TABLE = [
|
@@ -28,34 +24,7 @@ module Digest
|
|
28
24
|
0x57, 0x09, 0xeb, 0xb5, 0x36, 0x68, 0x8a, 0xd4, 0x95, 0xcb, 0x29, 0x77, 0xf4, 0xaa, 0x48, 0x16,
|
29
25
|
0xe9, 0xb7, 0x55, 0x0b, 0x88, 0xd6, 0x34, 0x6a, 0x2b, 0x75, 0x97, 0xc9, 0x4a, 0x14, 0xf6, 0xa8,
|
30
26
|
0x74, 0x2a, 0xc8, 0x96, 0x15, 0x4b, 0xa9, 0xf7, 0xb6, 0xe8, 0x0a, 0x54, 0xd7, 0x89, 0x6b, 0x35
|
31
|
-
]
|
32
|
-
|
33
|
-
#
|
34
|
-
# Packs the CRC8 checksum.
|
35
|
-
#
|
36
|
-
# @param [Integer] crc
|
37
|
-
# The checksum to pack.
|
38
|
-
#
|
39
|
-
# @return [String]
|
40
|
-
# The packed checksum.
|
41
|
-
#
|
42
|
-
def self.pack(crc)
|
43
|
-
(crc & 0xff).chr
|
44
|
-
end
|
45
|
-
|
46
|
-
#
|
47
|
-
# Updates the CRC8 checksum.
|
48
|
-
#
|
49
|
-
# @param [String] data
|
50
|
-
# The data to update the checksum with.
|
51
|
-
#
|
52
|
-
def update(data)
|
53
|
-
data.each_byte do |b|
|
54
|
-
@crc = ((TABLE[(@crc ^ b) & 0xff] ^ (@crc << 8)) & 0xff)
|
55
|
-
end
|
56
|
-
|
57
|
-
return self
|
58
|
-
end
|
27
|
+
].freeze
|
59
28
|
|
60
29
|
end
|
61
30
|
end
|
data/spec/crc_spec.rb
CHANGED
@@ -11,4 +11,54 @@ describe Digest::CRC do
|
|
11
11
|
it "should pack to an empty String by default" do
|
12
12
|
described_class.pack(0).should be_empty
|
13
13
|
end
|
14
|
+
|
15
|
+
context "when inherited" do
|
16
|
+
subject do
|
17
|
+
Class.new(described_class).tap do |klass|
|
18
|
+
klass::WIDTH = 16
|
19
|
+
|
20
|
+
klass::INIT_CRC = 0x01
|
21
|
+
|
22
|
+
klass::XOR_MASK = 0x02
|
23
|
+
|
24
|
+
klass::TABLE = [0x01, 0x02, 0x03, 0x04].freeze
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should override WIDTH" do
|
29
|
+
subject::WIDTH.should_not == described_class::WIDTH
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should override INIT_CRC" do
|
33
|
+
subject::INIT_CRC.should_not == described_class::INIT_CRC
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should override XOR_MASK" do
|
37
|
+
subject::XOR_MASK.should_not == described_class::XOR_MASK
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should override TABLE" do
|
41
|
+
subject::TABLE.should_not == described_class::TABLE
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "#initialize" do
|
45
|
+
let(:instance) { subject.new }
|
46
|
+
|
47
|
+
it "should initialize @init_crc" do
|
48
|
+
instance.instance_variable_get("@init_crc").should == subject::INIT_CRC
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should initialize @xor_mask" do
|
52
|
+
instance.instance_variable_get("@xor_mask").should == subject::XOR_MASK
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should initialize @width" do
|
56
|
+
instance.instance_variable_get("@width").should == subject::WIDTH
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should initialize @table" do
|
60
|
+
instance.instance_variable_get("@table").should == subject::TABLE
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
14
64
|
end
|
metadata
CHANGED
@@ -1,20 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: digest-crc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
5
|
-
prerelease:
|
4
|
+
version: 0.4.1
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Postmodern
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-04-17 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rubygems-tasks
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
17
|
- - ~>
|
20
18
|
- !ruby/object:Gem::Version
|
@@ -22,7 +20,6 @@ dependencies:
|
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
24
|
- - ~>
|
28
25
|
- !ruby/object:Gem::Version
|
@@ -30,7 +27,6 @@ dependencies:
|
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rspec
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
31
|
- - ~>
|
36
32
|
- !ruby/object:Gem::Version
|
@@ -38,7 +34,6 @@ dependencies:
|
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
38
|
- - ~>
|
44
39
|
- !ruby/object:Gem::Version
|
@@ -46,7 +41,6 @@ dependencies:
|
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: yard
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
45
|
- - ~>
|
52
46
|
- !ruby/object:Gem::Version
|
@@ -54,7 +48,6 @@ dependencies:
|
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
52
|
- - ~>
|
60
53
|
- !ruby/object:Gem::Version
|
@@ -119,26 +112,25 @@ files:
|
|
119
112
|
homepage: https://github.com/postmodern/digest-crc#readme
|
120
113
|
licenses:
|
121
114
|
- MIT
|
115
|
+
metadata: {}
|
122
116
|
post_install_message:
|
123
117
|
rdoc_options: []
|
124
118
|
require_paths:
|
125
119
|
- lib
|
126
120
|
required_ruby_version: !ruby/object:Gem::Requirement
|
127
|
-
none: false
|
128
121
|
requirements:
|
129
|
-
- -
|
122
|
+
- - '>='
|
130
123
|
- !ruby/object:Gem::Version
|
131
124
|
version: '0'
|
132
125
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
133
|
-
none: false
|
134
126
|
requirements:
|
135
|
-
- -
|
127
|
+
- - '>='
|
136
128
|
- !ruby/object:Gem::Version
|
137
129
|
version: '0'
|
138
130
|
requirements: []
|
139
131
|
rubyforge_project:
|
140
|
-
rubygems_version:
|
132
|
+
rubygems_version: 2.0.14
|
141
133
|
signing_key:
|
142
|
-
specification_version:
|
134
|
+
specification_version: 4
|
143
135
|
summary: A Cyclic Redundancy Check (CRC) library for Ruby.
|
144
136
|
test_files: []
|