Module: RedisClusterCRC16

Defined in:
lib/em_redis_cluster/crc16.rb

Overview

Copyright © 2013 Salvatore Sanfilippo <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


This is the CRC16 algorithm used by Redis Cluster to hash keys. Implementation according to CCITT standards.

This is actually the XMODEM CRC 16 algorithm, using the following parameters:

Name : “XMODEM”, also known as “ZMODEM”, “CRC-16/ACORN” Width : 16 bit Poly : 1021 (That is actually x^16 + x^12 + x^5 + 1) Initialization : 0000 Reflect Input byte : False Reflect Output CRC : False Xor constant to output CRC : 0000 Output for “123456789” : 31C3

Class Method Summary collapse

Class Method Details

.crc16(bytes) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/em_redis_cluster/crc16.rb', line 41

def RedisClusterCRC16.crc16(bytes)
    crc = 0
    bytes.each_byte{|b|
        crc = ((crc<<8) & 0xffff) ^ XMODEMCRC16Lookup[((crc>>8)^b) & 0xff]
    }
    crc
end