crc32

Description

The

crc32

command calculates a 32-bit cyclic redundancy check value (CRC) for the bytes in a string using the well-known CRC-32 (Ethernet CRC) scheme. The polynomial is

0x04c11db7

, the CRC register is initialized with

0xffffffff

, the input bytes are taken msb-first, and the result is the complement of the final register value reflected. (

crc32
implements the scheme called “CRC-32” in this Catalogue of Parametrised CRC Algorithms.)
crc32

returns a number, or the empty string if an error occurs. CRC values with their most significant bits set are returned as negative numbers (due to sign-bit extension upon conversion to 64-bit iRules integers). To obtain a correct unsigned 32-bit CRC in every case apply a 32-bit mask like this:

set CRC [expr {0xffffffff & [crc32 $msg]}]

. (To display a CRC value, use something like

log local0.info [format "CRC = 0x%08x" [expr {0xffffffff & [crc32 $msg]}]]
.)

Syntax

crc32 <string>

crc32 <string>

  • Returns the crc32 check value for the specified string, or if an error occurs, an empty string.

Examples

when HTTP_REQUEST {
   # Create a hash value for the host based on crc32
   # This could also be based on md5 or any other implementation
   # of a hash like djb or something.
   set key [crc32 [HTTP::host]]

   # Modulo the hash value by 1 - odd goes to one member, even another
   set key [expr {$key & 1}]

   # Route the request to the pool member based on the modulus
   # of the hash value.
   switch $key {
   0 { pool my_pool member 1.2.3.4:80 }
   1 { pool my_pool member 5.6.7.8:80 }
   }
}