Class: Omnizip::Checksums::Crc64

Inherits:
CrcBase
  • Object
show all
Defined in:
lib/omnizip/checksums/crc64.rb

Overview

CRC64 checksum implementation using ECMA-182 polynomial.

This implementation uses the ECMA-182 polynomial for CRC64 calculation as used in the XZ file format and 7-Zip archives. This is the standard 64-bit CRC polynomial for data integrity verification in compressed archives.

The algorithm uses a 256-entry pre-computed lookup table with 64-bit values for efficient O(1) per-byte processing.

Polynomial: 0xC96C5795D7870F42 (ECMA-182) Initial value: 0xFFFFFFFFFFFFFFFF Final XOR: 0xFFFFFFFFFFFFFFFF Bit width: 64

Examples:

One-shot calculation

checksum = Omnizip::Checksums::Crc64.calculate("123456789")
# => 0x995DC9BBDF1939FA

Incremental calculation

crc = Omnizip::Checksums::Crc64.new
crc.update("Hello, ")
crc.update("world!")
result = crc.finalize

Constant Summary collapse

POLYNOMIAL =

ECMA-182 polynomial (reversed representation) This is the standard polynomial used in XZ format

0xC96C5795D7870F42
MASK_64 =

64-bit mask for truncating values

0xFFFFFFFFFFFFFFFF
TABLE =

Pre-computed lookup table for CRC64 calculation Generated using the ECMA-182 polynomial This table is computed once at class load time for performance

generate_table(POLYNOMIAL, 64).freeze

Instance Attribute Summary

Attributes inherited from CrcBase

#value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CrcBase

calculate, #finalize, generate_table, #initialize, #reset, #update

Constructor Details

This class inherits a constructor from Omnizip::Checksums::CrcBase

Class Method Details

.lookup_tableArray<Integer>

Get the lookup table for CRC64.

Returns:

  • (Array<Integer>)

    256-entry lookup table



94
95
96
# File 'lib/omnizip/checksums/crc64.rb', line 94

def self.lookup_table
  TABLE
end

Instance Method Details

#final_xorInteger

Get the final XOR value for CRC64.

The final result is XORed with all 1s to invert the bits, which is part of the CRC64 standard.

Returns:

  • (Integer)

    0xFFFFFFFFFFFFFFFF



87
88
89
# File 'lib/omnizip/checksums/crc64.rb', line 87

def final_xor
  MASK_64
end

#initial_valueInteger

Get the initial CRC64 value.

CRC64 starts with all bits set to 1, which helps detect leading zeros in the data stream.

Returns:

  • (Integer)

    0xFFFFFFFFFFFFFFFF



77
78
79
# File 'lib/omnizip/checksums/crc64.rb', line 77

def initial_value
  MASK_64
end

#process_byte(crc, byte) ⇒ Integer

Process a single byte through the CRC64 algorithm.

Uses the lookup table for efficient computation: new_crc = (old_crc >> 8) ^ table[(old_crc ^ byte) & 0xFF]

Parameters:

  • crc (Integer)

    current CRC value

  • byte (Integer)

    byte to process (0-255)

Returns:

  • (Integer)

    updated CRC value



66
67
68
69
# File 'lib/omnizip/checksums/crc64.rb', line 66

def process_byte(crc, byte)
  index = (crc ^ byte) & 0xFF
  (crc >> 8) ^ TABLE[index]
end