class Digest::CRC
Base class for all CRC
algorithms.
Constants
Public Class Methods
checksum(data)
click to toggle source
Calculates the CRC
checksum.
@param [String] data
The given data.
@return [Integer]
The CRC checksum.
# File lib/digest/crc.rb, line 32 def self.checksum(data) crc = self.new crc << data return crc.checksum end
new()
click to toggle source
Initializes the CRC
checksum.
# File lib/digest/crc.rb, line 57 def initialize @init_crc = self.class.const_get(:INIT_CRC) @xor_mask = self.class.const_get(:XOR_MASK) @width = self.class.const_get(:WIDTH) @table = self.class.const_get(:TABLE) reset end
pack(crc)
click to toggle source
Packs the given CRC
checksum.
@param [Integer] crc
The raw CRC checksum.
@return [String]
The packed CRC checksum.
@abstract
# File lib/digest/crc.rb, line 50 def self.pack(crc) raise(NotImplementedError,"#{self.class}##{__method__} not implemented") end
Public Instance Methods
<<(data)
click to toggle source
@see update
# File lib/digest/crc.rb, line 100 def <<(data) update(data) return self end
block_length()
click to toggle source
The input block length.
@return [1]
# File lib/digest/crc.rb, line 71 def block_length 1 end
checksum()
click to toggle source
The resulting CRC
checksum.
@return [Integer]
The resulting CRC checksum.
# File lib/digest/crc.rb, line 121 def checksum @crc ^ @xor_mask end
digest_length()
click to toggle source
The length of the digest.
@return [Integer]
The length in bytes.
# File lib/digest/crc.rb, line 81 def digest_length (@width / 8.0).ceil end
finish()
click to toggle source
Finishes the CRC
checksum calculation.
@see pack
# File lib/digest/crc.rb, line 130 def finish self.class.pack(checksum) end
reset()
click to toggle source
Resets the CRC
checksum.
@return [Integer]
The default value of the CRC checksum.
# File lib/digest/crc.rb, line 111 def reset @crc = @init_crc end
update(data)
click to toggle source
Updates the CRC
checksum with the given data.
@param [String] data
The data to update the CRC checksum with.
@abstract
# File lib/digest/crc.rb, line 93 def update(data) raise(NotImplementedError,"#{self.class}##{__method__} not implemented") end