Class: CRC32

Inherits:
Object
  • Object
show all
Defined in:
RhoHubAccount.rb

Constant Summary

@@crc_t =
nil

Class Method Summary (collapse)

Class Method Details

+ (Object) calc_a(string)



36
37
38
# File 'RhoHubAccount.rb', line 36

def calc_a(string)
  [calc_i(string)].pack('l<')
end

+ (Object) calc_i(string, crc = 0)



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'RhoHubAccount.rb', line 22

def calc_i(string, crc = 0)
  if @@crc_t.nil?
    generate_table()
  end

  crc ^= 0xffff_ffff
  string.each_byte do |octet|
    remainder_1 = crc >> 8
    remainder_2 = @@crc_t[(crc & 0xff) ^ octet]
    crc = remainder_1 ^ remainder_2
  end
  crc ^ 0xffff_ffff
end

+ (Object) generate_table



9
10
11
12
13
14
15
16
17
18
19
20
# File 'RhoHubAccount.rb', line 9

def generate_table()
  divisor = [0, 1, 2, 4, 5, 7, 8, 10, 11, 12, 16, 22, 23, 26, 32].inject(0) {|sum, exponent| sum + (1 << (32 - exponent))}
  @@crc_t  = Array.new(256) do |octet|
    remainder = octet
    (0..7).each do |i|
      if !remainder[i].zero?
        remainder ^= (divisor << i)
      end
    end
    remainder >> 8
  end
end