Class: FastCache::CRC32

Inherits:
Object
  • Object
show all
Defined in:
lib/fastcache/hash/crc32.rb

Instance Method Summary collapse

Constructor Details

#initialize(string = nil) ⇒ CRC32

Returns a new instance of CRC32.



3
4
5
6
# File 'lib/fastcache/hash/crc32.rb', line 3

def initialize(string = nil)
  @crc = 0xFFFFFFFF
  self << string if string
end

Instance Method Details

#<<(string) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/fastcache/hash/crc32.rb', line 8

def <<(string)
  string.size.times do |i|
    @crc ^= string[i]
    8.times do
      if (@crc & 1).zero?
        @crc >>= 1
      else
        @crc = (@crc >> 1) ^ 0xEDB88320
      end
    end
  end
  self
end

#digestObject



22
23
24
# File 'lib/fastcache/hash/crc32.rb', line 22

def digest
  [@crc ^ 0xFFFFFFFF].pack('N')
end

#hexdigestObject



26
27
28
# File 'lib/fastcache/hash/crc32.rb', line 26

def hexdigest
  (@crc ^ 0xFFFFFFFF).to_s(16)
end