Class: Digest::CRC5

Inherits:
CRC
  • Object
show all
Defined in:
lib/digest/crc5.rb

Overview

Implements the CRC5 algorithm.

Constant Summary collapse

WIDTH =
5
INIT_CRC =
0x1f
XOR_MASK =
0x1f
CRC_MASK =
0x1f
TABLE =

Generated by ./pycrc.py --algorithm=table-driven --model=crc-5 --generate=c

[
  0x00, 0x0e, 0x1c, 0x12, 0x11, 0x1f, 0x0d, 0x03, 0x0b, 0x05, 0x17, 0x19, 0x1a, 0x14, 0x06, 0x08,
  0x16, 0x18, 0x0a, 0x04, 0x07, 0x09, 0x1b, 0x15, 0x1d, 0x13, 0x01, 0x0f, 0x0c, 0x02, 0x10, 0x1e,
  0x05, 0x0b, 0x19, 0x17, 0x14, 0x1a, 0x08, 0x06, 0x0e, 0x00, 0x12, 0x1c, 0x1f, 0x11, 0x03, 0x0d,
  0x13, 0x1d, 0x0f, 0x01, 0x02, 0x0c, 0x1e, 0x10, 0x18, 0x16, 0x04, 0x0a, 0x09, 0x07, 0x15, 0x1b,
  0x0a, 0x04, 0x16, 0x18, 0x1b, 0x15, 0x07, 0x09, 0x01, 0x0f, 0x1d, 0x13, 0x10, 0x1e, 0x0c, 0x02,
  0x1c, 0x12, 0x00, 0x0e, 0x0d, 0x03, 0x11, 0x1f, 0x17, 0x19, 0x0b, 0x05, 0x06, 0x08, 0x1a, 0x14,
  0x0f, 0x01, 0x13, 0x1d, 0x1e, 0x10, 0x02, 0x0c, 0x04, 0x0a, 0x18, 0x16, 0x15, 0x1b, 0x09, 0x07,
  0x19, 0x17, 0x05, 0x0b, 0x08, 0x06, 0x14, 0x1a, 0x12, 0x1c, 0x0e, 0x00, 0x03, 0x0d, 0x1f, 0x11,
  0x14, 0x1a, 0x08, 0x06, 0x05, 0x0b, 0x19, 0x17, 0x1f, 0x11, 0x03, 0x0d, 0x0e, 0x00, 0x12, 0x1c,
  0x02, 0x0c, 0x1e, 0x10, 0x13, 0x1d, 0x0f, 0x01, 0x09, 0x07, 0x15, 0x1b, 0x18, 0x16, 0x04, 0x0a,
  0x11, 0x1f, 0x0d, 0x03, 0x00, 0x0e, 0x1c, 0x12, 0x1a, 0x14, 0x06, 0x08, 0x0b, 0x05, 0x17, 0x19,
  0x07, 0x09, 0x1b, 0x15, 0x16, 0x18, 0x0a, 0x04, 0x0c, 0x02, 0x10, 0x1e, 0x1d, 0x13, 0x01, 0x0f,
  0x1e, 0x10, 0x02, 0x0c, 0x0f, 0x01, 0x13, 0x1d, 0x15, 0x1b, 0x09, 0x07, 0x04, 0x0a, 0x18, 0x16,
  0x08, 0x06, 0x14, 0x1a, 0x19, 0x17, 0x05, 0x0b, 0x03, 0x0d, 0x1f, 0x11, 0x12, 0x1c, 0x0e, 0x00,
  0x1b, 0x15, 0x07, 0x09, 0x0a, 0x04, 0x16, 0x18, 0x10, 0x1e, 0x0c, 0x02, 0x01, 0x0f, 0x1d, 0x13,
  0x0d, 0x03, 0x11, 0x1f, 0x1c, 0x12, 0x00, 0x0e, 0x06, 0x08, 0x1a, 0x14, 0x17, 0x19, 0x0b, 0x05
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CRC

#<<, #block_length, checksum, #checksum, #digest_length, #finish, #reset

Constructor Details

#initializeCRC5

Initializes the CRC5 instance.



40
41
42
43
44
# File 'lib/digest/crc5.rb', line 40

def initialize
  @crc_mask = self.class.const_get(:CRC_MASK)

  super
end

Class Method Details

.pack(crc) ⇒ String

Packs the CRC8 checksum.

Parameters:

  • crc (Integer)

    The checksum to pack.

Returns:

  • (String)

    The packed checksum.



55
56
57
# File 'lib/digest/crc5.rb', line 55

def self.pack(crc)
  (crc & CRC_MASK).chr
end

Instance Method Details

#update(data) ⇒ Object

Updates the CRC5 checksum.

Parameters:

  • data (String)

    The data to update the checksum with.



65
66
67
68
69
70
71
# File 'lib/digest/crc5.rb', line 65

def update(data)
  data.each_byte do |b|
    @crc = ((@table[(@crc ^ b) & 0xff] ^ (@crc >> 8)) & @crc_mask)
  end

  return self
end