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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
require 'digest'
module Digest
#
# Base class for all CRC algorithms.
#
class CRC < Digest::Class
include Digest::Instance
# The initial value of the CRC checksum
INIT_CRC = 0x00
# The XOR mask to apply to the resulting CRC checksum
XOR_MASK = 0x00
# The bit width of the CRC checksum
WIDTH = 0
#
# Calculates the CRC checksum.
#
# @param [String] data
# The given data.
#
# @return [Integer]
# The CRC checksum.
#
def self.checksum(data)
crc = self.new
crc << data
return crc.checksum
end
#
# Packs the given CRC checksum.
#
# @return [String]
# The packed CRC checksum.
#
def self.pack(crc)
''
end
#
# Initializes the CRC checksum.
#
def initialize
@crc = self.class.const_get(:INIT_CRC)
end
#
# The input block length.
#
# @return [1]
#
def block_length
1
end
#
# The length of the digest.
#
# @return [Integer]
# The length in bytes.
#
def digest_length
(self.class.const_get(:WIDTH) / 8.0).ceil
end
#
# Updates the CRC checksum with the given data.
#
# @param [String] data
# The data to update the CRC checksum with.
#
def update(data)
end
#
# @see {#update}
#
def <<(data)
update(data)
return self
end
#
# Resets the CRC checksum.
#
# @return [Integer]
# The default value of the CRC checksum.
#
def reset
@crc = self.class.const_get(:INIT_CRC)
end
#
# The resulting CRC checksum.
#
# @return [Integer]
# The resulting CRC checksum.
#
def checksum
@crc ^ self.class.const_get(:XOR_MASK)
end
#
# Finishes the CRC checksum calculation.
#
# @see {pack}
#
def finish
self.class.pack(checksum)
end
end
end
|