Sha256: dbcbdfe4b9aac46c4c5dfd5b7e081b61a33b9ee6c9d4d0ab74bbfd9c806e4ac0

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 KB

Contents

require 'digest'

module Digest
  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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
digest-crc-0.1.0 lib/digest/crc.rb