Class: Digest::CRC

Inherits:
Class
  • Object
show all
Includes:
Instance
Defined in:
lib/digest/crc.rb

Overview

Base class for all CRC algorithms.

Direct Known Subclasses

CRC1, CRC16, CRC24, CRC32, CRC5, CRC64, CRC8

Constant Summary collapse

INIT_CRC =

The initial value of the CRC checksum

0x00
XOR_MASK =

The XOR mask to apply to the resulting CRC checksum

0x00
WIDTH =

The bit width of the CRC checksum

0

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCRC

Initializes the CRC checksum.



49
50
51
# File 'lib/digest/crc.rb', line 49

def initialize
  @crc = self.class.const_get(:INIT_CRC)
end

Class Method Details

.checksum(data) ⇒ Integer

Calculates the CRC checksum.

Parameters:

  • data (String)

    The given data.

Returns:

  • (Integer)

    The CRC checksum.



29
30
31
32
33
34
# File 'lib/digest/crc.rb', line 29

def self.checksum(data)
  crc = self.new
  crc << data

  return crc.checksum
end

.pack(crc) ⇒ String

Packs the given CRC checksum.

Returns:

  • (String)

    The packed CRC checksum.



42
43
44
# File 'lib/digest/crc.rb', line 42

def self.pack(crc)
  ''
end

Instance Method Details

#<<(data) ⇒ Object

See Also:

  • Digest::CRC.{{#update}


84
85
86
87
# File 'lib/digest/crc.rb', line 84

def <<(data)
  update(data)
  return self
end

#block_length1

The input block length.

Returns:

  • (1)


58
59
60
# File 'lib/digest/crc.rb', line 58

def block_length
  1
end

#checksumInteger

The resulting CRC checksum.

Returns:

  • (Integer)

    The resulting CRC checksum.



105
106
107
# File 'lib/digest/crc.rb', line 105

def checksum
  @crc ^ self.class.const_get(:XOR_MASK)
end

#digest_lengthInteger

The length of the digest.

Returns:

  • (Integer)

    The length in bytes.



68
69
70
# File 'lib/digest/crc.rb', line 68

def digest_length
  (self.class.const_get(:WIDTH) / 8.0).ceil
end

#finishObject

Finishes the CRC checksum calculation.

See Also:

  • {pack}


114
115
116
# File 'lib/digest/crc.rb', line 114

def finish
  self.class.pack(checksum)
end

#resetInteger

Resets the CRC checksum.

Returns:

  • (Integer)

    The default value of the CRC checksum.



95
96
97
# File 'lib/digest/crc.rb', line 95

def reset
  @crc = self.class.const_get(:INIT_CRC)
end

#update(data) ⇒ Object

Updates the CRC checksum with the given data.

Parameters:

  • data (String)

    The data to update the CRC checksum with.



78
79
# File 'lib/digest/crc.rb', line 78

def update(data)
end