Digest CRC

Description

Adds support for calculating Cyclic Redundancy Check (CRC) to the Digest module.

Features

Install

gem install digest-crc

Examples

Calculate a CRC32:

require 'digest/crc32'

Digest::CRC32.hexdigest('hello')
# => "3610a686"

Calculate a CRC32 of a file:

Digest::CRC32.file('README.md')
# => #<Digest::CRC32: 127ad531>

Incrementally calculate a CRC32:

crc = Digest::CRC32.new
crc << 'one'
crc << 'two'
crc << 'three'
crc.hexdigest
# => "09e1c092"

Directly access the checksum:

crc.checksum
# => 165789842

Defining your own CRC class:

require 'digest/crc32'

module Digest
  class CRC3000 < CRC32

    WIDTH = 4

    INIT_CRC = 0xffffffff

    XOR_MASK = 0xffffffff

    TABLE = [
      # ....
    ].freeze

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

      return self
    end
  end
end

Thanks

Special thanks go out to the pycrc library which is able to generate C source-code for all of the CRC algorithms, including their CRC Tables.

License

Copyright (c) 2010-2014 Hal Brodigan

See LICENSE.txt for license information.